简体   繁体   English

如何在WooCommerce中改变购物车内容?

[英]How to Alter Cart Contents in WooCommerce?

I am trying to remove particular product from cart via code. 我试图通过代码从购物车中删除特定产品。 I only see the empty cart option which is clear all the product in cart, but i want to clear particular product in cart page. 我只看到空车选项,它清除了购物车中的所有产品,但我想在购物车页面中清除特定产品。 For example: Let say I have added two products to cart but i want cart behavior should be neither or nor mean only one product should be in cart. 例如:假设我已经添加了两个产品到购物车,但我想购物车行为应该既不是也不意味着只有一个产品应该在购物车中。 If product 1 is in cart then product 2 should not allow to add in cart. 如果产品1在购物车中,则产品2不应允许添加到购物车中。 If product 2 is in cart then product 1 should not allow. 如果产品2在购物车中,那么产品1不应该允许。

I tried little set of code but i can't find the exact hook to do this actual behavior. 我尝试了很少的代码,但我找不到确切的钩子来做这个实际的行为。 What i am trying is instead of empty entire cart, I load the cart content which is in array of values just unset particular array using cart item key, and to load remaining content to cart. 我正在尝试的是而不是空的整个购物车,我加载购物车内容,这是使用购物车项目键取消特定数组的值数组,并将剩余的内容加载到购物车。 But looks like is not works for me. 但看起来不适合我。

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);

May be is there any easy way to achieve this? 可能有任何简单的方法来实现这一目标吗? Not sure any suggestion would be great. 不确定任何建议会很棒。

I'm using the woocommerce_before_cart filter for a similar setup where people in certain groups aren't allowed to order specific product skus. 我正在使用woocommerce_before_cart过滤器进行类似设置,其中某些组中的人员不允许订购特定产品skus。 I hope this helps. 我希望这有帮助。 You would likely want to create a custom field in each product that would be something like a comma delineated list of other skus/post_ids that it wouldn't be allowed to be ordered with. 您可能希望在每个产品中创建一个自定义字段,类似于逗号描述的其他skus / post_id列表,不允许与其一起订购。

This code checks the first group that the user is associated with (in my site's case they only ever have 1 group). 此代码检查用户所关联的第一个组(在我的站点中,他们只有1个组)。 The disallowed_product_skus is the list of skus that the group for the user isn't allowed to purchase. 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>';
                }
        }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 更改WooCommerce中特定产品类型的购物车内容? - Alter Cart Contents for Specific Product Type in WooCommerce? WooCommerce检查购物车中的内容添加到购物车 - WooCommerce check cart contents on add to cart 哪个挂钩可以更改WooCommerce购物车页面中的数量更新? - Which Hook to alter quantity update in WooCommerce cart page? 更改Woocommerce中特定运输类别的计算出的购物车总重量 - Alter calculated cart items total weight for a specific shipping class in Woocommerce WooCommerce 购物车挂钩“woocommerce_cart_contents”显示内容错误 position - WooCommerce cart hook 'woocommerce_cart_contents' displaying content in the wrong position 如何清除废弃的woocommerce购物车 - How to clear an abandoned woocommerce cart 如何删除 Woocommerce 购物车通知? - How to remove Woocommerce cart notifications? 如何在 WooCommerce 中更改“添加到购物车”? - How to change "add to cart" in WooCommerce? 如何调试 WooCommerce 中的购物车内容? - How to debug cart content in WooCommerce? WooCommerce-使用add_to_cart后显示具有更新的购物车内容/总计的更新的迷你购物车小部件 - WooCommerce - Display updated mini cart widget with updated cart contents/totals after using add_to_cart
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM