修订片段以根据新产品类型分离文本输出

问题描述 投票:0回答:1

我想通过使用过滤价格总金额和产品类型的代码片段来分离不同的输出条件。

我编辑了之前回答我的问题的代码,在末尾添加了一些不同的条件:

  • 检查结账时是否只有预约,什么都没有显示或只是一条感谢信息
  • 如果购物车中至少有一种正常产品(可变或简单),请检查是否<50 (free shipping text) or >=50以显示进度条

我正在尝试添加一些额外的条件,以不显示进度条(免费送货剩余的金额),而只有物理产品的金额<=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) );
    }    
}
woocommerce product checkout code-snippets subtotal
1个回答
0
投票

在下文中,我们另外计算实体产品(可发货产品)的购物车商品小计。

  • 如果购物车中只有预约项目,则结账时不会显示任何内容。
  • 如果有可运送的物品或混合物品:
  • 如果未达到最低金额(可运送物品小计),我们会显示一条带有进度条的消息,显示获得免费送货的剩余金额
  • 如果已达到最低金额
  • (可运送物品小计),我们允许免费运送,并显示一条消息。
这是代码:

// 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"); } } }
    
© www.soinside.com 2019 - 2024. All rights reserved.