我想通过使用过滤价格总金额和产品类型的代码片段来分离不同的输出条件。
我编辑了之前回答我的问题的代码,在末尾添加了一些不同的条件:
我正在尝试添加一些额外的条件,以不显示进度条(免费送货剩余的金额),而只有物理产品的金额<=50 euro, if it less but with some normal phisical products in the cart (included eventually some appointment or virtual products) the bar will showed, in the case that there are only appointment type products or virtual i would like to sho only a thank you message.
这是我的代码尝试:
// Conditional function that checks if there are only "appointment" product types in cart
function has_only_appointments() {
$others_found = false; // Initializing
// Loop through cat items
foreach ( WC()->cart->get_cart() as $item ) {
if ( ! $item['data']->is_type('custom') ) {
$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() ){
return $rates;
}
// If there is shippable products remove free shipping if cart subtotal is under specific amount
if ( WC()->cart->get_subtotal() < 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
if( has_only_appointments( WC()->cart->get_cart() ) ){
echo __("Thank you for your order", "woocommerce");
}
// HERE I WOULD CHECK IF THERE ARE NORMAL PRODUCT AND THE TOTAL AMOUNT OF THE NORMAL PRODUCTS (anything could be shipped) IS MORE THEN 50, FOR THE FREE SHIPPING
elseif ( $subtotal >= 50 ) {
echo __("Free shipping available", "woocommerce");
}
// HERE HERE THE LAST TO SHOW THE PROGRESS BAR TO REACH THE FREE SHIPPING AMOUNT (again by checking if there are only phisical products)
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) );
}
}
在下文中,我们另外计算实体产品(可发货产品)的购物车商品小计。
// Conditional function that checks if there are only "appointment" product types in cart
function has_only_appointments() {
$others_found = false; // Initializing
// Loop through cat items
foreach ( WC()->cart->get_cart() as $item ) {
if ( ! $item['data']->is_type('custom') ) {
$others_found = true;
break;
}
}
return !$others_found;
}
// Utility function: Get the cart subtotal excl taxes from shippable cart items (physical products)
function get_shippable_subtotal() {
$subtotal = 0; // Initializing
// Loop through cat items
foreach ( WC()->cart->get_cart() as $item ) {
if ( ! $item['data']->is_type('custom') ) {
$subtotal += $item['line_total'];
}
}
return $subtotal;
}
// 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() ){
return $rates;
}
$min_amount = 50; // <== Define the minimal amount to be spent
// If there is shippable products remove free shipping if cart subtotal is under specific amount
if ( get_shippable_subtotal() < $min_amount ) {
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() {
// If there are physical products in cart or mixed items
if ( ! has_only_appointments() ) {
$min_amount = 50; // <== Define the minimal amount to be spent
$subtotal = get_shippable_subtotal(); // Get shippable items subtotal
// HERE HERE THE LAST TO SHOW THE PROGRESS BAR TO REACH THE FREE SHIPPING AMOUNT
if ( $subtotal < $min_amount ) {
printf( __( 'Spend another %s for free shipping!', 'woocommerce' ), wc_price($min_amount- $subtotal ) );
printf( '<br><progress id="freeshippingprogress" max="50" value="%d"></progress>', intval($subtotal) );
}
// HERE I WOULD CHECK IF THERE ARE NORMAL PRODUCT AND THEIR TOTAL AMOUNT IS MORE THEN 50 FOR THE FREE SHIPPING
else {
echo __("Free shipping available", "woocommerce");
}
}
}
应该可以。
// Conditional function that checks if there are only "appointment" product types in cart
function has_only_appointments() {
$others_found = false; // Initializing
// Loop through cat items
foreach ( WC()->cart->get_cart() as $item ) {
if ( ! $item['data']->is_type('custom') ) {
$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() ){
return $rates;
}
$min_amount = 50; // <== Define the minimal amount to be spent
// If there is shippable products remove free shipping if cart subtotal is under specific amount
if ( WC()->cart->get_subtotal() < $min_amount ) {
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() {
// If there are physical products in cart or mixed items
if ( ! has_only_appointments() ) {
$min_amount = 50; // <== Define the minimal amount to be spent
$subtotal = WC()->cart->get_subtotal(); // Get cart items subtotal
// HERE HERE THE LAST TO SHOW THE PROGRESS BAR TO REACH THE FREE SHIPPING AMOUNT
if ( $subtotal < $min_amount ) {
printf( __( 'Spend another %s for free shipping!', 'woocommerce' ), wc_price($min_amount- $subtotal ) );
printf( '<br><progress id="freeshippingprogress" max="50" value="%d"></progress>', intval($subtotal) );
}
// HERE I WOULD CHECK IF THERE ARE NORMAL PRODUCT AND THEIR TOTAL AMOUNT IS MORE THEN 50 FOR THE FREE SHIPPING
else {
echo __("Free shipping available", "woocommerce");
}
}
}