我在WooCommerce的产品页面中添加了一个"在WhatsApp上订购"按钮,当点击它时,它会重定向到WhatsApp,并预填好消息,比如"我想购买:产品名称和产品URL".

以下是我的代码:

function ecommercehints_content_after_add_to_cart_form() {
    // Get the ID of the current product
    $product_id = get_the_ID();

    // Get the product's title
    $product_title = get_the_title($product_id);

    // Get the product's URL
    $product_url = get_permalink($product_id);
    ?>
    <style>
    .order_on_whatsapp {
        display: flex;
        justify-content: center;
        align-items: center;
        height: 60px;
        border-radius: 3px;
        background-color: #fff;
        color: #131313;
        padding: 10px;
        vtext-decoration: none;
        font-weight: 700;
        border: 2px solid #5e51b6;
        font-size: 115%;
    }
    .order_on_whatsapp:hover{
        background-color: #6051BA;
        color: #fff;  
    }
    .order_on_whatsapp img {
        margin-right: 10px;
        width: 14px;
    }
    </style>
    <?php
    // Output the WhatsApp button with the product's title and URL in the pre-filled message
    echo '<a href="https://api.whatsapp.com/send?phone=+918488806776&text=Hello,%20I%20want%20to%20buy:%20' . 
    $product_title . ' (' . $product_url . ')" class="order_on_whatsapp">
    <img src="https://woo.cdesksolutions.com/wp-content/uploads/2022/11/whatsapp.webp" width="14px">
         Enquire on Whatsapp
    </a>';
}
add_action('woocommerce_after_add_to_cart_button', 'ecommercehints_content_after_add_to_cart_form', 30);

可以在消息中显示所选的变种及其价格吗?

推荐答案

对于可变产品,您需要使用JavaScript进行一些不同的设置,以便在WhatsApp Custom Button链接数据中设置产品名称、产品价格和产品URL数据.

以下是简单和可变产品的完整代码:

add_action('woocommerce_after_add_to_cart_button', 'ecommercehints_content_after_add_to_cart_form', 30);
function ecommercehints_content_after_add_to_cart_form() {
    global $product;

    if( ! is_a('WC_Product', $product) ) {
        $product = wc_get_product( get_the_id() );
    }
    // Not for variable products
    if( ! $product || 'variable' === $product->get_type() ) return;

    // Get the product name
    $product_title = $product->get_name();

    // Get the product URL
    $product_url = $product->get_permalink();

    // Get the product price
    $product_price = strip_tags( wc_price( wc_get_price_to_display( $product, array( 'price' => $product->get_price() ) ) ) );
    
    // Button CSS
    ?>
    <style>
    .order_on_whatsapp {
        display: flex;
        justify-content: center;
        align-items: center;
        height: 60px;
        border-radius: 3px;
        background-color: #fff;
        color: #131313;
        padding: 10px;
        vtext-decoration: none;
        font-weight: 700;
        border: 2px solid #5e51b6;
        font-size: 115%;
    }
    .order_on_whatsapp:hover{
        background-color: #6051BA;
        color: #fff;  
    }
    .order_on_whatsapp img {
        margin-right: 10px;
        width: 14px;
    }
    </style>
    <?php
    // Output the WhatsApp button with the product's title and URL in the pre-filled message
    echo '<a href="https://api.whatsapp.com/send?phone=+918488806776&text=Hello,%20I%20want%20to%20buy:%20' . 
    $product_title . '%20-%20' . 
    $product_price . '%20(' . $product_url . ')" class="order_on_whatsapp">
    <img src="https://woo.cdesksolutions.com/wp-content/uploads/2022/11/whatsapp.webp" width="14px">
         Enquire on Whatsapp
    </a>';
}

// Add variation custom field data to single variable product form
add_filter( 'woocommerce_available_variation', 'add_variation_custom_field_to_variable_form', 10, 3 );
function add_variation_custom_field_to_variable_form( $variation_data, $product, $variation ) {
    $variation_data['name']  = $variation->get_name(); // Product Variation name
    $variation_data['url']   = $variation->get_permalink(); // Product Variation URL
    $variation_data['main_url'] = $product->get_permalink(); // Parent Variable Product URL

    return $variation_data;
}

// For variable products
add_action( 'woocommerce_after_variations_form', 'display_selected_variation_custom_field_js' );
function display_selected_variation_custom_field_js(){
    // Button CSS
    ?>
    <style>
    .order_on_whatsapp {
        display: flex;
        justify-content: center;
        align-items: center;
        height: 60px;
        border-radius: 3px;
        background-color: #fff;
        color: #131313;
        padding: 10px;
        vtext-decoration: none;
        font-weight: 700;
        border: 2px solid #5e51b6;
        font-size: 115%;
    }
    .order_on_whatsapp:hover{
        background-color: #6051BA;
        color: #fff;  
    }
    .order_on_whatsapp img {
        margin-right: 10px;
        width: 14px;
    }
    .order_on_whatsapp.disabled {
        opacity: .5 !important;
        cursor: not-allowed;
    }
    .order_on_whatsapp.disabled:hover {
        background-color: #fff;
        color: #131313;  
    }
    </style>
    <?php
    
    // Javascript
    ?>
    <script type="text/javascript">
    (function($){
        var href = 'https://api.whatsapp.com/send?phone=+918488806776&text=Hello,%20I%20want%20to%20buy:%20';
        $('form.cart').on('show_variation', function(event, data) {
            $('.order_on_whatsapp').removeClass('disabled').attr( 'href', href + data.name + '%20-%20₹' + data.display_price + '%20(' +  data.url + ')' );
        }).on('hide_variation', function(event) {
            $('.order_on_whatsapp').removeAttr('href').addClass('disabled');
        });
    })(jQuery);
    </script>
    <?php
    // Display button
    echo '<a class="order_on_whatsapp">
    <img src="https://woo.cdesksolutions.com/wp-content/uploads/2022/11/whatsapp.webp" width="14px">
         Enquire on Whatsapp
    </a>';
}

代码放在活动子主题(或活动主题)的函数.php文件中.经过测试,效果良好.

Php相关问答推荐

SQLSTATE [42S02]:找不到基表或视图:1146 Table eshop. sessions不存在'''

在Woocommerce变量产品中的标签附近显示所选属性选项名称

如何让PHP变量跨越多个脚本调用?

PHP日期操作,将多个日期输出为格式化字符串

在AJAX请求后,Laravel重定向.(不允许使用方法)

PHP从响应应用程序json获取文件代码和URL

如何实现对数据库表中多列的唯一约束?

只收取WooCommerce中最高的运费

Laravel处理belongToMany和额外字段

用ajax获取文件 - CORS问题&;设置缓存?

更改特定产品标签的 Woocommerce 产品名称

扩展 WooCommerce 类触发未捕获错误:未找到WC_Coupon类

如何使用 PHP 创建简化的 twig 替换?

使用 PHP,我可以将 foreach 语句放入瞬态中吗?

如何将文件上传添加到 WooCommerce 结帐?

优化后的标题:如何在WooCommerce我的帐户编辑表单中添加账单字段

Laravel Factory - 在单列中生成具有随机迭代量的假 json 数组数据

Laravel 10 单元测试看不到来自 Artisan Command 的数据库更改

WooCommerce 按 ID 限制产品

如何将文件直接上传到 Cloudinary 而不先保存在 public 文件夹中