WooCommerce - 为某些产品添加免费送货以及当购物车达到一定数量时
WooCommerce - Add free shipping for certain products and when the cart which reaches an amount
我 运行 是一家使用 WooCommerce 的在线商店,销售普通产品和书籍。我想达到以下目标:
- 如果某人的购物车价值 < 50,他应该支付运费
- 如果某人的购物车价值 > 50 应免运费
- 如果有人将书添加到购物车,运费始终免费
我尝试使用以下代码:
function custom_free_per_class( $return, $package ) {
// Setup an array of shipping classes that allow Flat Rate Shipping
$shippingclass_array = array( 'bookshipping');
// loop through the cart checking the shipping classes
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
$shipping_class = get_the_terms( $values['product_id'], 'product_shipping_class' );
if ( isset( $shipping_class[0]->slug ) && in_array( $shipping_class[0]->slug, $shippingclass_array ) ) {
return true;
break;
}//if
}//foreach
}//function
add_filter( 'woocommerce_shipping_free_shipping_is_available', 'custom_free_per_class', 10, 2 );
add_filter( 'woocommerce_package_rates', 'hide_shipping_when_free_is_available', 10, 2 );
/**
* Hide shipping rates when free shipping is available
*
* @param array $rates Array of rates found for the package
* @param array $package The package array/object being shipped
* @return array of modified rates
*/
function hide_shipping_when_free_is_available( $rates, $package ) {
// Only modify rates if free_shipping is present
if ( isset( $rates['free_shipping'] ) ) {
// To unset all methods except for free_shipping, do the following
$free_shipping = $rates['free_shipping'];
$rates = array();
$rates['free_shipping'] = $free_shipping;
}
return $rates;
}
我在 WooCommerce 后端设置了在购物车价值达到 50 后应该可以免费送货。但是,这是行不通的。上面的代码有效 - 所以当有人添加一本书时可以保证免费送货,但这似乎阻碍了另一个意图(购物车> 50)的工作。如果我删除代码
add_filter( 'woocommerce_shipping_free_shipping_is_available', 'custom_free_per_class', 10, 2 );
当达到 50 时添加免费送货的功能有效,但肯定书籍不再免费了。
有人知道这里出了什么问题吗?
如果有人能帮助我,我将不胜感激。
谢谢!
我自己解决了。该函数的输入参数不是 ($return, $package) - 它只是 ($is_available).
我 运行 是一家使用 WooCommerce 的在线商店,销售普通产品和书籍。我想达到以下目标:
- 如果某人的购物车价值 < 50,他应该支付运费
- 如果某人的购物车价值 > 50 应免运费
- 如果有人将书添加到购物车,运费始终免费
我尝试使用以下代码:
function custom_free_per_class( $return, $package ) {
// Setup an array of shipping classes that allow Flat Rate Shipping
$shippingclass_array = array( 'bookshipping');
// loop through the cart checking the shipping classes
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
$shipping_class = get_the_terms( $values['product_id'], 'product_shipping_class' );
if ( isset( $shipping_class[0]->slug ) && in_array( $shipping_class[0]->slug, $shippingclass_array ) ) {
return true;
break;
}//if
}//foreach
}//function
add_filter( 'woocommerce_shipping_free_shipping_is_available', 'custom_free_per_class', 10, 2 );
add_filter( 'woocommerce_package_rates', 'hide_shipping_when_free_is_available', 10, 2 );
/**
* Hide shipping rates when free shipping is available
*
* @param array $rates Array of rates found for the package
* @param array $package The package array/object being shipped
* @return array of modified rates
*/
function hide_shipping_when_free_is_available( $rates, $package ) {
// Only modify rates if free_shipping is present
if ( isset( $rates['free_shipping'] ) ) {
// To unset all methods except for free_shipping, do the following
$free_shipping = $rates['free_shipping'];
$rates = array();
$rates['free_shipping'] = $free_shipping;
}
return $rates;
}
我在 WooCommerce 后端设置了在购物车价值达到 50 后应该可以免费送货。但是,这是行不通的。上面的代码有效 - 所以当有人添加一本书时可以保证免费送货,但这似乎阻碍了另一个意图(购物车> 50)的工作。如果我删除代码
add_filter( 'woocommerce_shipping_free_shipping_is_available', 'custom_free_per_class', 10, 2 );
当达到 50 时添加免费送货的功能有效,但肯定书籍不再免费了。
有人知道这里出了什么问题吗? 如果有人能帮助我,我将不胜感激。
谢谢!
我自己解决了。该函数的输入参数不是 ($return, $package) - 它只是 ($is_available).