1
我想添加免費送貨,如果一個特定的產品在購物車。如果它是購物車中的唯一商品,我有一種方法可行,但只要向購物車中添加其他商品,它就不會起作用。該產品包含航運類免費送貨。 tl; dr即使將其他物品添加到購物車,我如何獲得此功能。Woocommerce所有購物車項目免費送貨,如果航運類是在購物車
來源:https://www.speakinginbytes.com/2014/12/enable-free-shipping-per-product/
免費送貨某些類,如果
if (! class_exists('WC_Enable_Free_Shipping')) :
class WC_Enable_Free_Shipping {
protected static $instance = null;
private function __construct() {
// add our check
add_filter('woocommerce_shipping_free_shipping_is_available', array($this, 'patricks_enable_free_shipping'), 20);
}
/**
* Enable free shipping for orders with products that have the free-shipping shipping class slug
*/
public function patricks_enable_free_shipping($is_available) {
global $woocommerce;
// set the shipping classes that are eligible
$eligible = array('free-shipping');
// get cart contents
$cart_items = $woocommerce->cart->get_cart();
// loop through the items checking to make sure they all have the right class
foreach ($cart_items as $key => $item) {
if (! in_array($item['data']->get_shipping_class(), $eligible)) {
// this item doesn't have the right class. return default availability
return $is_available;
}
}
// nothing out of the ordinary return true
return true;
}
/**
* Return an instance of this class.
*/
public static function get_instance() {
// If the single instance hasn't been set, set it now.
if (null == self::$instance) {
self::$instance = new self;
}
return self::$instance;
}
}
add_action('init', array('WC_Enable_Free_Shipping', 'get_instance'), 0);
endif;
隱藏其他運送方式免費送貨,可
function my_hide_shipping_when_free_is_available($rates) {
$free = array();
foreach ($rates as $rate_id => $rate) {
if ('free_shipping' === $rate->method_id) {
$free[ $rate_id ] = $rate;
break;
}
}
return ! empty($free) ? $free : $rates;
}
add_filter('woocommerce_package_rates', 'my_hide_shipping_when_free_is_available', 100);