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.