首页 > 解决方案 > 如何在wordpress woocommerce上双重计算产品?

问题描述

我需要一次重复计算产品。例如,当我打开一个产品时,它需要显示 0 个选择的项目。当我单击加号(+)时,它需要一次添加 2 个。我怎样才能在 wordpress woocommerce 上做到这一点?

标签: wordpresswoocommerce

解决方案


美好的一天,帖木儿。
据我了解,您想将产品两次添加到购物车并显示数字“2”?
你可以使用这个 WC 钩子:

add_action('woocommerce_add_to_cart', 'custome_add_to_cart');
function custome_add_to_cart() {
global $woocommerce;

$product_id = $_POST['assessories'];

$found = false;

//check if product already in cart
if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
    foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
        $_product = $values['data'];
        if ( $_product->id == $product_id )
            $found = true;
    }
    // if product not found, add it
    if ( ! $found )
        WC()->cart->add_to_cart( $product_id );
} else {
    // if no products in cart, add it
    WC()->cart->add_to_cart( $product_id );
}
}

当它返回结果时,用 JS 更新订购产品的数量。

请告诉它是否可以帮助您?


推荐阅读