嗨,伙计们需要帮助...
<ul>
<li class="product">product details + add to cart button</li>
<li class="product">product details + add to cart button</li>
<li class="product">product details + add to cart button</li>
</ul>
我需要的是,当我成功地将产品添加到购物车时,它将自动向<li>
元素添加一个类(添加),以便实现样式。
<ul>
<li class="product added">product details + add to cart button</li>
<li class="product">product details + add to cart button</li>
<li class="product">product details + add to cart button</li>
</ul>
从网上获得了此功能,我不知道该怎么办
add_action( 'wp_footer', 'trigger_for_ajax_add_to_cart' );
function trigger_for_ajax_add_to_cart() {
?>
<script type="text/javascript">
(function($){
$('body').on( 'added_to_cart', function(){
//code here
});
})(jQuery);
</script>
<?php
}
答案 0 :(得分:1)
$('.product').click(function() {
$(this).addClass('added');
// Adding product to cart...etc
});
答案 1 :(得分:0)
请尝试以下代码
add_action( 'wp_footer', 'trigger_for_ajax_add_to_cart' );
function trigger_for_ajax_add_to_cart() {
foreach( WC()->cart->get_cart() as $cart_item ){
$product_id = $cart_item['product_id'];
?>
<script type="text/javascript">
(function($){
alert('dsfdssdfsdsd')
$(".post-<?php echo $product_id; ?>").addClass('cart-added-class');
})(jQuery);
</script>
<?php
}
}