Php 如果装运类别在购物车中,则所有购物车项目均免费装运
我想添加免费送货,如果一个特定的产品是在购物车。我有一个方法,如果它是购物车中的唯一项目,它就可以工作,但当我向购物车中添加其他项目时,它就不工作了。该产品包含免费配送的配送类别。tl;dr即使我将其他项目添加到购物车中,我如何使其工作 资料来源: 特定类别的免费配送Php 如果装运类别在购物车中,则所有购物车项目均免费装运,php,wordpress,woocommerce,shipping,Php,Wordpress,Woocommerce,Shipping,我想添加免费送货,如果一个特定的产品是在购物车。我有一个方法,如果它是购物车中的唯一项目,它就可以工作,但当我向购物车中添加其他项目时,它就不工作了。该产品包含免费配送的配送类别。tl;dr即使我将其他项目添加到购物车中,我如何使其工作 资料来源: 特定类别的免费配送 if ( ! class_exists( 'WC_Enable_Free_Shipping' ) ) : class WC_Enable_Free_Shipping { protected static $instance
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 );
您可以尝试“woocommerce\u available\u shipping\u method”挂钩。此代码应该可以帮助您:
add_filter( 'woocommerce_available_shipping_methods', 'custom_shipping_methods' );
function custom_shipping_methods( $available_methods ) {
// Check your cart product's logic here
foreach ( WC()->cart->cart_contents AS $item ) {
// If something in cart then just keep free shipping in available method
// That logic goes here
}
// return available methods
return $available_methods;
}
您可以尝试“woocommerce\u available\u shipping\u method”挂钩。此代码应该可以帮助您:
add_filter( 'woocommerce_available_shipping_methods', 'custom_shipping_methods' );
function custom_shipping_methods( $available_methods ) {
// Check your cart product's logic here
foreach ( WC()->cart->cart_contents AS $item ) {
// If something in cart then just keep free shipping in available method
// That logic goes here
}
// return available methods
return $available_methods;
}