| |
| <?php
|
|
|
| if (!defined('ABSPATH')) {
|
| exit;
|
| }
|
|
|
|
|
| if (!in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_option('active_plugins')))) {
|
| add_action('admin_notices', 'superwp_wc_not_active_notice');
|
|
|
| function superwp_wc_not_active_notice() {
|
| ?>
|
| <div class="notice notice-error">
|
| <p><?php _e('SuperWP WhatsApp Checkout requires WooCommerce to be installed and active. Please install and activate WooCommerce to use this plugin.', 'superwp'); ?></p>
|
| </div>
|
| <?php
|
| }
|
|
|
| return;
|
| }
|
|
|
| class SuperWP_WhatsApp_Checkout {
|
|
|
| public static function init() {
|
|
|
| add_filter('woocommerce_settings_tabs_array', array(__CLASS__, 'add_settings_tab'), 50);
|
| add_action('woocommerce_settings_tabs_superwp_whatsapp_checkout', array(__CLASS__, 'settings_tab'));
|
| add_action('woocommerce_update_options_superwp_whatsapp_checkout', array(__CLASS__, 'update_settings'));
|
|
|
|
|
| add_action('woocommerce_admin_field_superwp_enable_checkbox', array(__CLASS__, 'superwp_enable_checkbox_field'));
|
|
|
|
|
| add_action('woocommerce_review_order_before_submit', array(__CLASS__, 'add_whatsapp_checkout_button'));
|
| add_action('woocommerce_checkout_update_order_meta', array(__CLASS__, 'save_checkout_method'));
|
| add_action('woocommerce_thankyou', array(__CLASS__, 'redirect_to_whatsapp_if_selected'));
|
| }
|
|
|
| public static function add_settings_tab($settings_tabs) {
|
| $settings_tabs['superwp_whatsapp_checkout'] = __('WhatsApp Checkout', 'superwp');
|
| return $settings_tabs;
|
| }
|
|
|
| public static function settings_tab() {
|
| woocommerce_admin_fields(self::get_settings());
|
| }
|
|
|
| public static function update_settings() {
|
| woocommerce_update_options(self::get_settings());
|
| }
|
|
|
| public static function get_settings() {
|
| return array(
|
| 'section_title' => array(
|
| 'name' => __('WhatsApp Checkout Settings', 'superwp'),
|
| 'type' => 'title',
|
| 'desc' => __('Configure settings for WhatsApp Checkout.', 'superwp'),
|
| 'id' => 'superwp_whatsapp_checkout_section_title'
|
| ),
|
| 'enable_whatsapp_checkout' => array(
|
| 'name' => __('Enable WhatsApp Checkout', 'superwp'),
|
| 'type' => 'checkbox',
|
| 'desc' => __('Check this box to enable WhatsApp checkout functionality.', 'superwp'),
|
| 'id' => 'superwp_enable_whatsapp_checkout',
|
| 'default' => 'no'
|
| ),
|
| 'whatsapp_number' => array(
|
| 'name' => __('WhatsApp Number', 'superwp'),
|
| 'type' => 'text',
|
| 'desc' => __('Enter the WhatsApp number to receive orders.', 'superwp'),
|
| 'id' => 'superwp_whatsapp_number',
|
| 'placeholder' => '+1234567890'
|
| ),
|
| 'opening_message' => array(
|
| 'name' => __('WhatsApp Opening Message', 'superwp'),
|
| 'type' => 'textarea',
|
| 'desc' => __('Enter the opening message for WhatsApp orders.', 'superwp'),
|
| 'id' => 'superwp_opening_message',
|
| 'placeholder' => __('Hi! Here is your order details:', 'superwp')
|
| ),
|
| 'signature' => array(
|
| 'name' => __('WhatsApp Message Signature', 'superwp'),
|
| 'type' => 'textarea',
|
| 'desc' => __('Enter your signature for WhatsApp orders.', 'superwp'),
|
| 'id' => 'superwp_signature',
|
| 'placeholder' => __('Best regards,', 'superwp')
|
| ),
|
| 'website_link' => array(
|
| 'name' => __('Website Link', 'superwp'),
|
| 'type' => 'text',
|
| 'desc' => __('Enter your website link to include in the WhatsApp message.', 'superwp'),
|
| 'id' => 'superwp_website_link',
|
| 'placeholder' => __('https://yourwebsite.com', 'superwp')
|
| ),
|
| 'include_order_number' => array(
|
| 'name' => __('Include Order Number', 'superwp'),
|
| 'type' => 'checkbox',
|
| 'desc' => __('Include the order number in the WhatsApp message.', 'superwp'),
|
| 'id' => 'superwp_include_order_number',
|
| 'default' => 'yes'
|
| ),
|
| 'include_total' => array(
|
| 'name' => __('Include Total', 'superwp'),
|
| 'type' => 'checkbox',
|
| 'desc' => __('Include the total amount in the WhatsApp message.', 'superwp'),
|
| 'id' => 'superwp_include_total',
|
| 'default' => 'yes'
|
| ),
|
| 'include_items' => array(
|
| 'name' => __('Include Items', 'superwp'),
|
| 'type' => 'checkbox',
|
| 'desc' => __('Include the items in the WhatsApp message.', 'superwp'),
|
| 'id' => 'superwp_include_items',
|
| 'default' => 'yes'
|
| ),
|
| 'include_quantity' => array(
|
| 'name' => __('Include Quantity', 'superwp'),
|
| 'type' => 'checkbox',
|
| 'desc' => __('Include the quantity of each item in the WhatsApp message.', 'superwp'),
|
| 'id' => 'superwp_include_quantity',
|
| 'default' => 'yes'
|
| ),
|
| 'include_price' => array(
|
| 'name' => __('Include Price', 'superwp'),
|
| 'type' => 'checkbox',
|
| 'desc' => __('Include the price of each item in the WhatsApp message.', 'superwp'),
|
| 'id' => 'superwp_include_price',
|
| 'default' => 'yes'
|
| ),
|
| 'include_product_link' => array(
|
| 'name' => __('Include Product Link', 'superwp'),
|
| 'type' => 'checkbox',
|
| 'desc' => __('Include the product link in the WhatsApp message.', 'superwp'),
|
| 'id' => 'superwp_include_product_link',
|
| 'default' => 'yes'
|
| ),
|
| 'include_billing_address' => array(
|
| 'name' => __('Include Billing Address', 'superwp'),
|
| 'type' => 'checkbox',
|
| 'desc' => __('Include the billing address in the WhatsApp message.', 'superwp'),
|
| 'id' => 'superwp_include_billing_address',
|
| 'default' => 'yes'
|
| ),
|
| 'include_order_status' => array(
|
| 'name' => __('Include Order Status', 'superwp'),
|
| 'type' => 'checkbox',
|
| 'desc' => __('Include the order status in the WhatsApp message.', 'superwp'),
|
| 'id' => 'superwp_include_order_status',
|
| 'default' => 'yes'
|
| ),
|
| 'include_payment_method' => array(
|
| 'name' => __('Include Payment Method', 'superwp'),
|
| 'type' => 'checkbox',
|
| 'desc' => __('Include the payment method in the WhatsApp message.', 'superwp'),
|
| 'id' => 'superwp_include_payment_method',
|
| 'default' => 'yes'
|
| ),
|
| 'include_order_link' => array(
|
| 'name' => __('Include Order Link', 'superwp'),
|
| 'type' => 'checkbox',
|
| 'desc' => __('Include the order link in the WhatsApp message.', 'superwp'),
|
| 'id' => 'superwp_include_order_link',
|
| 'default' => 'yes'
|
| ),
|
| 'include_website_link' => array(
|
| 'name' => __('Include Website Link', 'superwp'),
|
| 'type' => 'checkbox',
|
| 'desc' => __('Include the website link in the WhatsApp message.', 'superwp'),
|
| 'id' => 'superwp_include_website_link',
|
| 'default' => 'yes'
|
| ),
|
| 'fields_to_include_end' => array(
|
| 'type' => 'sectionend',
|
| 'id' => 'superwp_fields_to_include_end'
|
| )
|
| );
|
| }
|
|
|
| public static function superwp_enable_checkbox_field() {
|
| woocommerce_wp_checkbox(array(
|
| 'id' => 'superwp_enable_whatsapp_checkout',
|
| 'label' => __('Enable WhatsApp Checkout', 'superwp'),
|
| 'description' => __('Check this box to enable WhatsApp checkout functionality.', 'superwp'),
|
| 'desc_tip' => true,
|
| ));
|
| }
|
|
|
| public static function add_whatsapp_checkout_button() {
|
| $enable_whatsapp_checkout = get_option('superwp_enable_whatsapp_checkout', 'no');
|
|
|
| if ($enable_whatsapp_checkout === 'yes') {
|
| ?>
|
| <div id="whatsapp_checkout_option">
|
| <input type="radio" name="checkout_method" value="default" checked>
|
| <label for="default_checkout"><?php _e('Default Checkout', 'superwp'); ?></label>
|
| <input type="radio" name="checkout_method" value="whatsapp">
|
| <label for="whatsapp_checkout"><?php _e('WhatsApp Checkout', 'superwp'); ?></label>
|
| </div>
|
| <?php
|
| }
|
| }
|
|
|
| public static function save_checkout_method($order_id) {
|
| if (isset($_POST['checkout_method'])) {
|
| update_post_meta($order_id, '_checkout_method', sanitize_text_field($_POST['checkout_method']));
|
| }
|
| }
|
|
|
| public static function redirect_to_whatsapp_if_selected($order_id) {
|
| $order = wc_get_order($order_id);
|
| $checkout_method = get_post_meta($order_id, '_checkout_method', true);
|
|
|
| if ($order && $checkout_method === 'whatsapp') {
|
| $enable_whatsapp_checkout = get_option('superwp_enable_whatsapp_checkout', 'no');
|
|
|
| if ($enable_whatsapp_checkout === 'yes') {
|
| $whatsapp_number = get_option('superwp_whatsapp_number');
|
|
|
| if ($whatsapp_number) {
|
| $order_data = $order->get_data();
|
| $items = $order->get_items();
|
| $item_details = '';
|
|
|
| foreach ($items as $item) {
|
| $product = $item->get_product();
|
| $quantity = $item->get_quantity();
|
| $unit = $quantity > 1 ? 'pcs' : 'pc';
|
| $product_name = $item->get_name();
|
| $product_link = $product->get_permalink();
|
| $product_price = html_entity_decode(strip_tags(wc_price($item->get_total())));
|
|
|
| $item_details .= $product_name;
|
| if (get_option('superwp_include_quantity') === 'yes') {
|
| $item_details .= ' x ' . $quantity . ' ' . $unit;
|
| }
|
| $item_details .= "\n";
|
| if (get_option('superwp_include_price') === 'yes') {
|
| $item_details .= __('Price: ', 'superwp') . $product_price . "\n";
|
| }
|
| if (get_option('superwp_include_product_link') === 'yes') {
|
| $item_details .= __('Link: ', 'superwp') . $product_link . "\n";
|
| }
|
| $item_details .= "\n";
|
| }
|
|
|
| $order_url = $order->get_view_order_url();
|
| $order_status = ucfirst($order->get_status());
|
| $payment_method = $order->get_payment_method_title();
|
| $currency = get_woocommerce_currency_symbol();
|
|
|
| $opening_message = get_option('superwp_opening_message', __('Hi! Here is your order details:', 'superwp'));
|
| $signature = get_option('superwp_signature', __('Best regards,', 'superwp'));
|
| $website_link = get_option('superwp_website_link', __('https://yourwebsite.com', 'superwp'));
|
|
|
| $message_parts = array($opening_message);
|
|
|
| if (get_option('superwp_include_order_number') === 'yes') {
|
| $message_parts[] = __('Order No: #', 'superwp') . $order->get_order_number();
|
| }
|
| if (get_option('superwp_include_order_link') === 'yes') {
|
| $message_parts[] = __('Track Your Order Using This Link: ', 'superwp') . $order_url;
|
| }
|
| if (get_option('superwp_include_items') === 'yes') {
|
| $message_parts[] = __('Items List: ', 'superwp') . "\n" . $item_details;
|
| }
|
| if (get_option('superwp_include_total') === 'yes') {
|
| $message_parts[] = __('Total Order Amount: ', 'superwp') . $currency . ' ' . $order->get_total();
|
| }
|
| if (get_option('superwp_include_billing_address') === 'yes') {
|
| $message_parts[] = __('Billing Address: ', 'superwp') . $order_data['billing']['address_1'];
|
| }
|
| if (get_option('superwp_include_order_status') === 'yes') {
|
| $message_parts[] = __('Current Order Status: ', 'superwp') . $order_status;
|
| }
|
| if (get_option('superwp_include_payment_method') === 'yes') {
|
| $message_parts[] = __('Payment Method: ', 'superwp') . $payment_method;
|
| }
|
| if (get_option('superwp_include_website_link') === 'yes') {
|
| $message_parts[] = __('Website: ', 'superwp') . $website_link;
|
| }
|
|
|
| $message_parts[] = $signature;
|
| $message = implode("\n\n", $message_parts);
|
|
|
| $whatsapp_url = 'https://wa.me/' . $whatsapp_number . '?text=' . urlencode($message);
|
|
|
|
|
| echo "<script type='text/javascript'>
|
| window.location.href = '{$whatsapp_url}';
|
| </script>";
|
| exit;
|
| }
|
| }
|
| }
|
| }
|
| }
|
|
|
|
|
| add_action('plugins_loaded', array('SuperWP_WhatsApp_Checkout', 'init'));
|
| |
| |
Comments