【问题标题】:Removing auto added product from WooCommerce从 WooCommerce 中删除自动添加的产品
【发布时间】:2020-04-28 23:54:58
【问题描述】:

所以我可以使用 woocommerce 文档中的这个 sn-p,它可以在达到购物车总数时添加免费产品。但是,如果我们的客户不想要产品,他们需要能够将其从购物车中移除(重复客户每次都不想要相同的礼物)。使用此 sn-p,从购物车中删除按钮不适用于免费产品。

/**
 * Add another product depending on the cart total
 */
add_action( 'template_redirect', 'add_product_to_cart' );
function add_product_to_cart() {
  if ( ! is_admin() ) {
        global $woocommerce;
        $product_id = 2831; //replace with your product id
        $found = false;
        $cart_total = 30; //replace with your cart total needed to add above item

        if( $woocommerce->cart->total >= $cart_total ) {
            //check if product already in cart
            if ( sizeof( $woocommerce->cart->get_cart() ) > 0 ) {
                foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
                    $_product = $values['data'];
                    if ( $_product->get_id() == $product_id )
                        $found = true;
                }
                // if product not found, add it
                if ( ! $found )
                    $woocommerce->cart->add_to_cart( $product_id );
            } else {
                // if no products in cart, add it
                $woocommerce->cart->add_to_cart( $product_id );
            }
        }
    }
}
``
code above is from here: https://docs.woocommerce.com/document/automatically-add-product-to-cart-on-visit/

【问题讨论】:

标签: php wordpress woocommerce


【解决方案1】:

从购物车中删除产品实际上会起作用,但问题是代码也会自动重新添加产品。

解决此问题的一种方法是记住该产品已经添加过一次,如果该产品将再次添加(例如从购物车中删除之后),则不要重新添加该产品。

一个简单的方法是使用WC()->session将该数据保存在 WooCommerce 会话中。修改代码如下:

add_action( 'template_redirect', 'add_product_to_cart' );
function add_product_to_cart() {
  if ( ! is_admin() ) {
        global $woocommerce;
        $product_id = 2831; //replace with your product id
        $found = false;
        $cart_total = 10; //replace with your cart total needed to add above item

        if( $woocommerce->cart->total >= $cart_total && ! WC()->session->get( 'free_item_given', false ) ) { // <--- ADDED CHECK FOR free_item_given HERE
            //check if product already in cart
            if ( sizeof( $woocommerce->cart->get_cart() ) > 0 ) {
                foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
                    $_product = $values['data'];
                    if ( $_product->get_id() == $product_id )
                        $found = true;
                }
                // if product not found, add it
                if ( ! $found ) {
                    $woocommerce->cart->add_to_cart( $product_id );
                    WC()->session->set( 'free_item_given', true ); // <--- REMEMBER free_item_given HERE
                }
            } else {
                // if no products in cart, add it
                $woocommerce->cart->add_to_cart( $product_id );
                WC()->session->set( 'free_item_given', true ); // <--- REMEMBER free_item_given HERE
            }
        }
    }
}

希望能解决你的问题!

【讨论】:

  • 是的。这行得通! WC()->session 是完美的。谢谢!
猜你喜欢
  • 1970-01-01
  • 2017-01-07
  • 2021-05-09
  • 1970-01-01
  • 2017-06-26
  • 2016-01-03
  • 1970-01-01
  • 2022-06-22
  • 2021-06-30
相关资源
最近更新 更多