【问题标题】:How to Alter Cart Contents in WooCommerce?如何在 WooCommerce 中更改购物车内容?
【发布时间】:2014-05-29 14:18:50
【问题描述】:

我正在尝试通过代码从购物车中删除特定产品。我只看到清除购物车中所有产品的空购物车选项,但我想清除购物车页面中的特定产品。 例如: 假设我已将两种产品添加到购物车,但我希望购物车行为既不或也不意味着只有一种产品应该在购物车中。 如果产品 1 在购物车中,则产品 2 不应允许添加到购物车中。如果产品 2 在购物车中,则产品 1 不应允许。

我尝试了少量代码,但找不到执行此实际行为的确切钩子。我正在尝试的不是清空整个购物车,而是加载购物车内容,该内容位于值数组中,只是使用购物车项目键取消设置特定数组,并将剩余内容加载到购物车。但看起来对我不起作用。

function cf_alter_cart_content($value) {
    global $woocommerce;
    $cart_contents = $woocommerce->cart->get_cart();
    foreach ($woocommerce->cart->get_cart() as $cart_item_key => $value) {
        if ($value['product_id'] == '77') {
            unset($cart_contents[$cart_item_key]);
            unset($value['data']);
        }
        return $value['data'];
    }
}

//add_action('wp_head', 'cf_alter_cart_content');
add_filter('woocommerce_cart_item_product', 'cf_alter_cart_content', 10, 1);

可能有什么简单的方法可以实现这一点吗?不确定有什么建议会很棒。

【问题讨论】:

    标签: php woocommerce wordpress


    【解决方案1】:

    我正在使用woocommerce_before_cart 过滤器进行类似的设置,其中不允许某些组中的人订购特定的产品 sku。我希望这有帮助。您可能希望在每个产品中创建一个自定义字段,该字段类似于逗号分隔的其他 skus/post_id 列表,不允许与其一起订购。

    此代码检查用户关联的第一个组(在我的网站中,他们只有一个组)。 disallowed_product_skus 是不允许该用户组购买的 skus 列表。

    $disallowed_product_skus  =  array (
      <group_num>  =>  array (
            '<sku>',
      )
    );
    add_filter ( 'woocommerce_before_cart' , 'cart_check_disallowed_skus' );
    function cart_check_disallowed_skus() {
            global $woocommerce;
            global $disallowed_product_skus;
    
            $assigned_group  =  GroupOperations::get_current_user_first_group();
    
            $cart_contents = $woocommerce->cart->get_cart();
            $keys = array_keys ( $cart_contents );
    
            if ( array_key_exists ( $assigned_group , $disallowed_product_skus ) ) {
                    $disallowed_products_in_cart = false;
                    foreach ( $keys as $key ) {
                            $cart_item_product_id = $cart_contents[$key]['product_id'];
                            $cart_product_meta = get_post_meta ( $cart_item_product_id );
                            $cart_product_sku = $cart_product_meta['_sku'][0];
    
                            if ( in_array ( $cart_product_sku , $disallowed_product_skus[$assigned_group] ) ) {
                                    $woocommerce->cart->set_quantity ( $key , 0 , true );
                                    $disallowed_products_in_cart = true;
                            }
                    }
    
                    if ( $disallowed_products_in_cart ) {
                            echo '<p class="woocommerce-error">Non-approved products have been automatically removed from cart.</p>';
                    }
            }
    }
    

    【讨论】:

    • +1 感谢您的回答,但组选项是我们必须做的吗?还是已经在 woocommerce 中?
    • Groups 是一个单独的插件,我用来将用户放入其中。我直接从我的代码库中复制/粘贴,为您提供一个起始模板。重要的部分是您对要从购物车中删除的商品使用woocommerce_before_cart 过滤器和$woocommerce-&gt;cart-&gt;set_quantity 方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-25
    • 1970-01-01
    • 1970-01-01
    • 2017-09-10
    • 1970-01-01
    • 2020-04-23
    • 2017-08-07
    相关资源
    最近更新 更多