Home / Admin / How to Allow WordPress WooCommerce Direct Order to WhatsApp Free.
Duplicate Snippet

Embed Snippet on Your Site

How to Allow WordPress WooCommerce Direct Order to WhatsApp Free.

Post Intro
In this tutorial you will learn How to Allow WordPress WhatsApp Direct Order to WhatsApp For Free use code snippet.

Many businesses are leveraging messaging apps like WhatsApp for customer engagement. This tutorial focuses on enhancing your WooCommerce product pages by adding a “WhatsApp Order” button. This button, triggered by a custom checkbox, allows customers to initiate orders via WhatsApp for selected products.

https://www.youtube.com/watch?v=Z7YgtDyTzuk&t=97s

Code Preview
php
<?php
// Add custom checkbox and WhatsApp number fields to product edit page
add_action('woocommerce_product_options_general_product_data', 'add_whatsapp_order_fields');
function add_whatsapp_order_fields() {
    woocommerce_wp_checkbox(array(
        'id'            => 'allow_whatsapp_order',
        'label'         => __('Allow WhatsApp Order', 'text-domain'),
        'description'   => __('Check this box to enable WhatsApp ordering for this product.', 'text-domain'),
    ));
    woocommerce_wp_text_input(array(
        'id'          => '_whatsapp_number',
        'label'       => __('WhatsApp Number', 'text-domain'),
        'placeholder' => __('Enter WhatsApp Number', 'text-domain'),
        'desc_tip'    => 'true',
        'description' => __('Enter the WhatsApp number for this product. Include the country code without spaces or special characters (e.g., +123456789).', 'text-domain'),
    ));
}
// Save the checkbox and WhatsApp number field values
add_action('woocommerce_process_product_meta', 'save_whatsapp_order_fields');
function save_whatsapp_order_fields($post_id) {
    $allow_whatsapp_order = isset($_POST['allow_whatsapp_order']) ? 'yes' : 'no';
    update_post_meta($post_id, 'allow_whatsapp_order', $allow_whatsapp_order);
    $whatsapp_number = sanitize_text_field($_POST['_whatsapp_number']);
    update_post_meta($post_id, '_whatsapp_number', $whatsapp_number);
}
// Modify the product template to display WhatsApp Order button if checkbox is checked
add_action('woocommerce_single_product_summary', 'display_whatsapp_order_button', 30);
function display_whatsapp_order_button() {
    global $product;
    // Check if the product has the checkbox checked
    $allow_whatsapp_order = get_post_meta($product->get_id(), 'allow_whatsapp_order', true);
    if ($allow_whatsapp_order !== 'yes') {
        // If checkbox is not checked, display the default Add to Cart button
        return;
    }
    // Get the product details
    $product_title = get_the_title();
    $product_price = $product->get_price();
    $stock_quantity = $product->get_stock_quantity();
    $product_link = get_permalink($product->get_id());
    // Display WhatsApp Order button with the specified WhatsApp number
    $whatsapp_number = get_post_meta($product->get_id(), '_whatsapp_number', true);
    $whatsapp_number = !empty($whatsapp_number) ? preg_replace('/\D/', '', $whatsapp_number) : 'YOUR_DEFAULT_WHATSAPP_NUMBER';
    // Prepare the WhatsApp message with product details and link
    $whatsapp_message = urlencode(
        "I would like to place an order for product: $product_title\n" .
        "Price: $product_price\n" .
        "Current Stock Quantity: $stock_quantity\n" .
        "Product Link: $product_link"
    );
    // WhatsApp button styles
    echo '<style>
        a.whatsapp-button {
            display: inline-block;
            padding: 10px 15px;
            background-color: #25D366; /* WhatsApp green color */
            color: #fff;
            text-decoration: none;
            border-radius: 5px;
            font-size: 16px;
        }
        a.whatsapp-button i {
            margin-right: 5px; /* Adjust the spacing between icon and text */
        }
        a.whatsapp-button:hover {
            background-color: #128C7E; /* Darker shade for hover effect */
        }
    </style>';
    // Display WhatsApp Order button
    echo '<a href="https://api.whatsapp.com/send?phone=' . esc_attr($whatsapp_number) . '&text=' . $whatsapp_message . '" class="whatsapp-button" target="_blank" rel="noopener"><i class="fa fa-whatsapp"></i> WhatsApp Order</a>';
    // Remove the default Add to Cart button
    remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30);
}

Comments

Add a Comment