• We run a marketplace where each vendor has a minimum cart value for free shipping. We are facing an issue when a customer is placing an order with products from multiple vendors.

    Is there a PHP code snippet which’ll help hide Vendor shipping cost if their product basket (within the larger total cart value) is above their free shipping threshold. If not, flat shipping fee will be added to this vendor product basket.

    WooCommerce shipping doesn’t have this feature. Please guide.

Viewing 4 replies - 1 through 4 (of 4 total)
  • To hide the vendor shipping cost if their product basket is above their free shipping threshold and add a flat shipping fee to their product basket, you can use the following PHP code snippet:

    add_filter('woocommerce_cart_shipping_packages', 'hide_vendor_shipping_cost', 10, 1);
    function hide_vendor_shipping_cost($packages) {
        global $woocommerce;
        $cart_items = $woocommerce->cart->get_cart();
        $vendors_shipping = array();
    
        foreach($cart_items as $cart_item) {
            $vendor_id = get_post_field('post_author', $cart_item['product_id']);
            $vendor_min_cart = get_user_meta($vendor_id, 'vendor_min_cart', true);
            $vendor_shipping_cost = get_user_meta($vendor_id, 'vendor_shipping_cost', true);
    
            if($vendor_min_cart && $vendor_shipping_cost) {
                if(!array_key_exists($vendor_id, $vendors_shipping)) {
                    $vendors_shipping[$vendor_id] = array(
                        'vendor_min_cart' => $vendor_min_cart,
                        'vendor_shipping_cost' => $vendor_shipping_cost,
                        'cart_total' => 0
                    );
                }
    
                $vendors_shipping[$vendor_id]['cart_total'] += $cart_item['line_total'];
            }
        }
    
        foreach($vendors_shipping as $vendor_id => $vendor_shipping) {
            if($vendor_shipping['cart_total'] >= $vendor_shipping['vendor_min_cart']) {
                $packages[0]['contents_cost'] -= $vendor_shipping['vendor_shipping_cost'];
            } else {
                $packages[0]['contents_cost'] += $vendor_shipping['vendor_shipping_cost'];
            }
        }
    
        return $packages;
    }
    

    This code uses the woocommerce_cart_shipping_packages filter to modify the shipping packages. It first gets all the cart items and checks if they belong to a vendor with a minimum cart value for free shipping and a shipping cost. If so, it adds the product line total to the vendor’s cart total.

    After that, it checks if the cart total for each vendor is above or below the minimum cart value for free shipping. If it’s above, it subtracts the shipping cost from the contents_cost of the shipping package. If it’s below, it adds the shipping cost to the contents_cost.

    You will need to modify this code to use the correct meta keys for the vendor’s minimum cart value and shipping cost. Also, this code assumes that all products in the cart belong to a vendor. If there are products that do not belong to a vendor, you may need to modify the code to handle them correctly.

    Thread Starter tianlong123

    (@tianlong123)

    Thanks Linards. Since it is a multi-vendor marketplace, usually customers place an order with goods from multiple vendors. The cart will have multiple vendors’ products; some vendors with their basket above their free shipping threshold and some below their threshold. We’ll have to modify the code.

    This might help.

    add_filter('woocommerce_cart_shipping_packages', 'split_shipping_packages_by_vendor', 10, 1);
    function split_shipping_packages_by_vendor($packages) {
        global $woocommerce;
        $cart_items = $woocommerce->cart->get_cart();
        $packages = array();
    
        foreach($cart_items as $cart_item_key => $cart_item) {
            $vendor_id = get_post_field('post_author', $cart_item['product_id']);
            $vendor_min_cart = get_user_meta($vendor_id, 'vendor_min_cart', true);
            $vendor_shipping_cost = get_user_meta($vendor_id, 'vendor_shipping_cost', true);
    
            if(!array_key_exists($vendor_id, $packages)) {
                $packages[$vendor_id] = array(
                    'contents' => array(),
                    'contents_cost' => 0,
                    'vendor_min_cart' => $vendor_min_cart,
                    'vendor_shipping_cost' => $vendor_shipping_cost,
                    'user' => array(
                        'ID' => $vendor_id,
                    ),
                );
            }
    
            $packages[$vendor_id]['contents'][$cart_item_key] = $cart_item;
            $packages[$vendor_id]['contents_cost'] += $cart_item['line_total'];
        }
    
        foreach($packages as $vendor_id => &$package) {
            if($package['contents_cost'] >= $package['vendor_min_cart']) {
                $package['vendor_shipping_cost'] = 0;
            }
        }
    
        return array_values($packages);
    }
    
    add_filter('woocommerce_package_rates', 'apply_vendor_shipping_cost', 10, 2);
    function apply_vendor_shipping_cost($rates, $package) {
        if (isset($package['vendor_shipping_cost'])) {
            foreach ($rates as $rate_key => &$rate) {
                $rate->cost = $package['vendor_shipping_cost'];
            }
        }
    
        return $rates;
    }
    

    This updated code snippet splits the shipping packages by vendor and applies the shipping cost based on the cart total for each vendor. The woocommerce_cart_shipping_packages filter is used to split the packages and calculate the cart total for each vendor. The woocommerce_package_rates filter is used to apply the vendor-specific shipping cost to the rates.

    Please note that this code assumes that you have correctly set up the user meta keys for ‘vendor_min_cart’ and ‘vendor_shipping_cost’. Also, this code does not handle shipping zones or additional shipping methods. If you use any additional shipping methods or shipping zones, you will need to adjust the code accordingly.

    Thread Starter tianlong123

    (@tianlong123)

    Thanks a Zillion Linards!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘[Multi Vendor Marketplace] Auto Free Shipping for each vendor,beyond a threshold’ is closed to new replies.