对于可变产品,您需要使用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文件中.经过测试,效果良好.