简体   繁体   中英

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. If product 2 is in cart then product 1 should not allow.

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. 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.

This code checks the first group that the user is associated with (in my site's case they only ever have 1 group). The disallowed_product_skus is the list of skus that the group for the user isn't allowed to purchase.

$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>';
                }
        }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM