重新加载购物车页面增加项目

问题描述 投票:0回答:1

如果您执行add to cart on this page然后重新加载页面,它将添加该项目两次。我们可以以某种方式避免在购物车页面上重新加载两次吗?

注意:我没有添加代码,因为它的WooCommerce核心特定的东西。

wordpress woocommerce
1个回答
0
投票

// define the woocommerce_add_to_cart callback 
function action_woocommerce_add_to_cart( $array, $int, $int ) { 
    // make action magic happen here... 
	global $woocommerce;
	//echo '<pre>'; print_r($woocommerce->cart->get_cart()); echo '</pre>'; die;
	$product_id = 37; //current product id
	foreach($woocommerce->cart->get_cart() as $key => $val ) {
        $_product = $val['data'];
 
        if($product_id == $_product->id ) {
            return false; // Already added
        }
    } 
}; 
         
// add the action 
add_action( 'woocommerce_add_to_cart', 'action_woocommerce_add_to_cart', 10, 3 );
© www.soinside.com 2019 - 2024. All rights reserved.