这不适用于单个产品页面正常添加到购物车。它用于 Ajax 在产品循环中添加到购物车,例如商店页面、存档页面、相关产品……
请注意,通过 Ajax 添加到购物车的产品不会重新加载页面。
该代码在上一个 WooCommerce 版本上仍然可以完美运行:
这是一个“甜蜜警报 2”示例,在 ajax 添加到购物车上显示产品名称:
// Add some product data to "add to cart" button for 'added_to_cart' js event
add_action( 'woocommerce_loop_add_to_cart_link', 'filter_wc_loop_add_to_cart_link', 10, 3 );
function filter_wc_loop_add_to_cart_link( $button_html, $product, $args ) {
if( $product->supports( 'ajax_add_to_cart' ) ) {
$search_string = 'data-product_sku';
// Insert custom product data as data tags
$replace_string = sprintf(
'data-product_name="%s" data-product_price="%s" data-currency="%s" %s',
$product->get_name(), // product name
wc_get_price_to_display( $product ), // Displayed price
get_woocommerce_currency(), // currency
$search_string
);
$button_html = str_replace($search_string, $replace_string, $button_html);
}
return $button_html;
}
// The jQuery code that will handle the event getting the required product data
add_action( 'wp_footer', 'added_to_cart_js_event' );
function added_to_cart_js_event(){
?>
<script src="https://unpkg.com/sweetalert2@7.20.1/dist/sweetalert2.all.js"></script>
<script type="text/javascript">
(function($){
$(document.body).on('added_to_cart', function( event, fragments, cart_hash, button ) {
var product_id = button.data('product_id'), // Get the product id
product_qty = button.data('quantity'), // Get the quantity
product_sku = button.data('product_sku'), // Get the product sku
product_name = button.data('product_name'), // Get the product name
product_price = button.data('product_price'), // Get the product price
currency = button.data('currency'); // Get the currency
// For testing: View all product available data on console log (to be removed)
console.log( button.data() );
const toast = swal.mixin({
toast: true,
showConfirmButton: false,
timer: 10000
});
toast({
type: 'success',
title: 'Product "'+product_name+'" added to Cart'
});
});
})(jQuery);
</script>
<?php
}
代码在您的活动子主题(或活动主题)的functions.php 文件中。经过测试并且可以工作。