【问题标题】:Deleting specific products from cart page and checkout page从购物车页面和结帐页面中删除特定产品
【发布时间】:2017-06-05 18:16:49
【问题描述】:

我的 WooCommerce 网上商店由多步骤添加到购物车流程组成,当跳过一个步骤时,它会将免费产品添加到购物车中。

所以我想在结帐页面上从购物车中删除免费产品,一旦选择过程完成并且客户将支付他的订单。

我知道我必须在某个钩子中使用 WC_Cart 方法remove_cart_item( $cart_item_key )。目前我已经尝试了一些没有成功的钩子。

我的免费产品 ID 是:

$free_products_ids = array(10,52,63,85);

我怎样才能做到这一点?

谢谢

【问题讨论】:

    标签: php wordpress woocommerce cart product


    【解决方案1】:

    要删除购物车和结帐页面上的购物车项目,我在购物车和结帐页面挂钩中使用自定义挂钩函数,这样:

    add_action( 'woocommerce_before_cart', 'removing_the_free_cart_items');
    add_action( 'woocommerce_before_checkout_form', 'removing_the_free_cart_items');
    function removing_the_free_cart_items() {
    
        if ( !WC()->cart->is_empty() ):
    
            // Here your free products IDs
            $free_products = array(10,52,63,85);
    
            // iterating throught each cart item
            foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item){
    
                $cart_item_id = $cart_item['data']->id;
    
                // If free product is in cart, it's removed
                if( in_array($cart_item_id, $free_products) )
                    WC()->cart->remove_cart_item($cart_item_key);
            }
    
        endif;
    }
    

    此代码位于您的活动子主题(或主题)的 function.php 文件中或任何插件文件中。

    此代码已经过测试并且可以工作。

    【讨论】:

      猜你喜欢
      • 2018-06-17
      • 2014-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-30
      • 2020-10-27
      • 1970-01-01
      • 2021-08-04
      相关资源
      最近更新 更多