我编辑了一个在线脚本,创建一个进度条来显示:
“额外花费 XX 欧元即可免费送货”。
但我想编辑以添加额外的过滤器,以避免在只有一种产品类型且是特定产品类型的情况下避免出现此文本。
这是因为人们还可以购买非实体产品但属于自定义类型产品“预约”的预约。
add_filter( 'woocommerce_package_rates', 'ecommercehints_free_shipping_for_specific_products', 10, 2 );
function ecommercehints_free_shipping_for_specific_products($rates, $package, $order) {
// Loop through order items
foreach ( $order->get_items() as $item ) {
// Get an instance of the WC_Product Object from the WC_Order_Item_Product
$product = $item->get_product();
$cart_total = WC()->cart->get_subtotal();
if( $product->is_type('Appointment') ){
echo 'You\'ve unlocked free shipping!';
} else if ( $cart_total < 50 ) {
unset( $rates['free_shipping:21'] ); // The shipping method radio button value
}
break; // We stop the loop and keep the first item
}
return $rates;
}
// Show 'Spend another X amount' on checkout.
add_filter( 'woocommerce_checkout_before_order_review', 'ecommercehints_checkout_page_progress_bar', 10 );
function ecommercehints_checkout_page_progress_bar() {
$cart_total = WC()->cart->get_subtotal();
$cart_remaining = 50 - $cart_total;
$product = $item->get_product();
if ($cart_total < 50 ) {
echo 'Spend another ' . get_woocommerce_currency_symbol() . $cart_remaining . ' for free shipping!<br><progress id="freeshippingprogress" max="50" value="'.$cart_total.'"></progress>';
} else if ( $product->is_type('Appointment') {
echo 'You\'ve unlocked free shipping!';
}
}
您的代码中有一些错误,例如:
$order
过滤器挂钩不存在 woocommerce_package_rates
变量。此外,您需要确保“预约”是有效的产品类型。一般来说,产品类型总是小写的,所以我在下面使用了“appointment”(小写)。
尝试以下修改后的代码:
// Conditional function that checks if there are only "appointment" product types in cart
function has_only_appointments( $cart_items ) {
$others_found = false; // Initializing
// Loop through cat items
foreach ( $cart_items as $item ) {
if ( ! $item['data']->is_type('appointment') ) {
$others_found = true;
break;
}
}
return !$others_found;
}
// Filter shipping methods
add_filter( 'woocommerce_package_rates', 'ecommercehints_free_shipping_for_specific_products', 10, 2 );
function ecommercehints_free_shipping_for_specific_products( $rates, $package ) {
// check if there are only "appointment" product types in cart
if( has_only_appointments( $package['contents'] ) ){
return $rates;
}
// If there is shippable products remove free shipping if cart subtotal is under specific amount
if ( $package['contents_cost'] < 50 ) {
unset( $rates['free_shipping:21'] ); // The targeted Free shipping method rate ID
}
return $rates;
}
// Show 'Spend another X amount' on checkout.
add_filter( 'woocommerce_checkout_before_order_review', 'ecommercehints_checkout_page_progress_bar', 10 );
function ecommercehints_checkout_page_progress_bar() {
$subtotal = WC()->cart->get_subtotal();
// check if there are only "appointment" product types in cart or If cart subtotal is up to 50
if( has_only_appointments( WC()->cart->get_cart() ) || $subtotal >= 50 ){
echo __("You've unlocked free shipping!", "woocommerce");
} elseif ( $subtotal < 50 ) {
printf( __('Spend another %s for free shipping!', 'woocommerce'), wc_price( 50 - $subtotal ) );
printf('<br><progress id="freeshippingprogress" max="50" value="%d"></progress>', intval($subtotal) );
}
}
应该可以...
重要提示:清空购物车以刷新运输方式缓存数据。