允许删除Woocommerce中从购物车自动添加的产品

时间:2018-09-19 06:57:57

标签: php wordpress woocommerce product cart

在Woocommerce中,当客户访问我的网站时,我使用this official code snippet自动向购物车添加商品:

add_action( 'template_redirect', 'add_product_to_cart' );
function add_product_to_cart() {
    if ( ! is_admin() ) {
        $product_id = 1234; //replace with your own product id
        $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->get_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 );
        }
    }
}

它工作正常,并将产品添加到购物车。

但是我希望客户提供不想要的选项,从购物车中删除该商品。但是实际上不能删除产品。

如何使产品自动添加到购物车中可移动?

1 个答案:

答案 0 :(得分:0)

尝试此代码。

plotly.offline.init_notebook_mode(connected=True)
相关问题