我在WooCommerce开办网上商店,销售普通产品和书籍。我想达到以下目的:
我尝试使用以下代码:
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时添加免费送货的功能有效,但书籍肯定不再免费。
任何人都知道这里出了什么问题? 如果有人能帮助我,我将非常感激。
谢谢!
答案 0 :(得分:0)
我自己解决了。该函数的输入参数不是($ return,$ package) - 它只是($ is_available)。