Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hooks around add to cart for Woocommerce

I am running a WooCommerce store with WC Marketplace. What I am trying to achieve with the hook below is to prevent a new item being added to a basket if there is already a product in the basket from a different vendor. e.g. If a shopper adds product x from vendor y to his basket, if they were to add product a from vendor b, then the item will not be added and the user will be informed of the error.

I have 2 questions:
- firstly when does a hook run, is it before the main function fired or after? I have a hook for the function woocommerce_add_to_cart. So I want to know will the hook fire after the function woocommerce_add_to_cart runs or before.
- My second question is, I have attached the hook below, in your opinion would this work?

function action_woocommerce_add_to_cart( $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data ) { 
    $same_vendor = 1;

    $empty = WC_Cart::is_empty();

    //If there is an item in the cart then,
    if (!$empty) {
        //Get the VendorId of the product being added to the cart.
        $vendor = get_wcmp_product_vendors($product_id);
        $vendor_id = $vendor->id;

        foreach( WC()->cart->get_cart() as $cart_item ) {
            //Get the vendor Id of the item
            $cart_product_id = $cart_item['product_id'];
            $cart_vendor = get_wcmp_product_vendors($product_id);
            $cart_vendor_id = $cart_vendor->id;

            //If two products do not have the same Vendor then set $same_vendor to 0
            if($vendor_id !== $cart_vendor_id) {
                $same_vendor = 0;
            }
        }

        if ($same_vendor === 0) {
            WC()->cart->remove_cart_item( $cart_item_key );
            //How do I show a message to tell the customer.
        }
    }
}

Regards

like image 432
Rhys Gottwald Avatar asked Jan 19 '26 15:01

Rhys Gottwald


1 Answers

Here below are the hooks involved in WC_Cart add_to_cart() method:

A) Before an item is added to cart:

  1. Validation filter hook woocommerce_add_to_cart_validation
  2. Item Quantity change filter hook woocommerce_add_to_cart_quantity (not with ajax)
  3. Item Data change filter hook woocommerce_add_cart_item_data (not with ajax)
  4. and some others related to "sold individually" products (see here)

A) After an item is added to cart:

  1. Change cart Item filter hook woocommerce_add_cart_item
  2. Add an event, action hook woocommerce_add_to_cart

To be clear in your case:

  1. As you can see now woocommerce_add_to_cart is not a function but only an action hook.
  2. Hook location: It's located inside WC_Cart add_to_cart() method (at the end of the source code).
  3. When the hook is fired: It's fired once the WC_Cart add_to_cart() method is executed.
  4. What is the purpose: To execute some custom code when this method is executed (event).

Regarding your code:
It should be better to use the dedicated filter hook woocommerce_add_to_cart_validation that will stop customer that want to add a new item to the cart if there is already a product in cart from a different vendor, displaying optionally a custom message:

add_filter( 'woocommerce_add_to_cart_validation', 'filter_add_to_cart_validation', 10, 3 );
function filter_add_to_cart_validation( $passed, $product_id, $quantity ) { 
    if ( WC()->cart->is_empty() ) return $passed;

    // Get the VendorId of the product being added to the cart.
    $current_vendor = get_wcmp_product_vendors($product_id);

    foreach( WC()->cart->get_cart() as $cart_item ) {
        // Get the vendor Id of the item
        $cart_vendor = get_wcmp_product_vendors($cart_item['product_id']);

        // If two products do not have the same Vendor
        if( $current_vendor->id != $cart_vendor->id ) {
            // We set 'passed' argument to false
            $passed = false ;

            // Displaying a custom message
            $message = __( "This is your custom message", "woocommerce" );
            wc_add_notice( $message, 'error' );
            // We stop the loop
            break; 
        }
    }
    return $passed;
}

Code goes in function.php file of your active child theme (or active theme) or in any plugin file.

Tested and works.


like image 141
LoicTheAztec Avatar answered Jan 22 '26 08:01

LoicTheAztec



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!