PHP snippet to allow mixed carts for certain products, but not all

37 views Asked by At

I am pretty new to PHP, but I am attempting to modify a code snippet created by a previous employee. I use wordpress to host a woocommerce site. Within that site, we have 3 store locations, which we separate using product vendors. To prevent customers from adding products to their cart from multiple store locations, we have the below snippet.

We are trying to run a fundraising campagin where a product that is a donation amount can be added to anyone's cart, but disallowing multiple vendors is not letting it add. My thought was to create a new vendor and modify this code with if/elseif statements, but nothing I have tried works.

For reference, the ID of my fundraising vendor (which I want to be able to add to cart with any vendor) is 345

The vendor IDs for the store products I don't want to mix with eachother are 278, 277, and 279.

function so_validate_add_cart_item( $passed, $product_id, $quantity, $variation_id = '', $variations= '' ) {
    
    try {
 
          global $woocommerce;
    $items = $woocommerce->cart->get_cart(); //getting cart items
    $_product = array();
    foreach($items as $item => $values) {
    $_product[] = $values['data']->post;
    }
     $product_added_vendor_id = wp_get_object_terms( $product_id, WC_PRODUCT_VENDORS_TAXONOMY, array( 'fields' => 'ids' ) );  //get product id being added to cart      
        
    foreach($_product as $productlal){
        if(isset($productlal)){ 
            $product_in_cart_vendor_id =  wp_get_object_terms( $productlal->ID, WC_PRODUCT_VENDORS_TAXONOMY, array( 'fields' => 'ids' ) );
            if( $product_in_cart_vendor_id[0] != $product_added_vendor_id[0] ){
                wc_add_notice(  __("Product not added to cart as it is from another location. Please complete the purchase from one store before buying from another. " . "<a class='button wc-forward' href='" . wc_get_cart_url() . "'>Go to cart to complete purchase ></a>"), $notice_type = 'error' );
                return false;                                                    
            }
         }
    }
     return $passed; 
    }
        
 catch (Exception $e) {
    wc_add_notice(  __("EXCEPTION: " .  $e->getMessage()));
}
}
add_filter( 'woocommerce_add_to_cart_validation', 'so_validate_add_cart_item', 10, 5 );
    

I attempted to change

if( $product_in_cart_vendor_id[0] != $product_added_vendor_id[0] ){
                wc_add_notice(  __("Product not added to cart as it is from another location. Please complete the purchase from one store before buying from another. " . "<a class='button wc-forward' href='" . wc_get_cart_url() . "'>Go to cart to complete purchase ></a>"), $notice_type = 'error' );

to an elseif statment and add

if($product_in_cart_vendor_id[0]==="345") 

I also tested substituting "345" with the vendor name "fundraising" and added a wc_add_notice saying "thank you for your donation" after each try.

0

There are 0 answers