我有一个woocommerce网站,其中有2个产品,一个产品有免费送货但其他已支付运费,如果他们在购物车中单独添加。现在我有一个场景,如果某个客户在购物车中同时添加免费和付费的运输产品,那么整个订单的运费应该是免费的。我怎样才能做到这一点?
谢谢, 穆罕默德
答案 0 :(得分:1)
试试这个...只需将其粘贴到主题的functions.php中,然后替换下面$products_to_look = array( 34 );
中的ID。
add_filter( 'woocommerce_package_rates', 'woocommerce_package_rates' );
function woocommerce_package_rates( $rates ) {
if ( ! WC()->cart->is_empty() ) {
$products_to_look = array( 34 ); // ids of products separated by comma.
$found = false;
foreach ( WC()->cart->get_cart() as $cart_item ) {
if ( $found = in_array( $cart_item['product_id'], $products_to_look ) ) {
break;
}
}
}
if ( $found ) {
foreach($rates as $key => $rate ) {
$rates[$key]->label = 'Free shipping'; // change label to Free shipping...
$rates[$key]->cost = 0; // cost is set to 0.
}
}
return $rates;
}
进一步阅读:WooCommerce shipping fee that changes when condition is met