Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
882 views
in Technique[技术] by (71.8m points)

wordpress - Woocommerce how can I enable shipping method depending on total price

I want to change allowed shiping method according to these conditions:

If cart subtotal is less than 300 € then allowed shipping method is local delivery.

if cart subtotal is betwen 300 and 1000€ than allowed shipping method is flat rate.

Over 1000€ is shipping free.

Limit for free shipping can be set in admin settings. But rest couldn't be set. I tried to change it in my custom cart-shiping.php WC template. This Way:

if($woocommerce->cart->subtotal > 300 and $woocommerce->cart->subtotal < 1000)
    $chosen_method =  "flat_rate";    
foreach ( $available_methods as $method ) :
    global $woocommerce;
    $subtotal = $woocommerce->cart->subtotal;
    if((($subtotal > 300 and $subtotal  < 1000   or $method->method_id == "local_delivery")  and ($subtotal < 300  or $method->method_id == "flat_rate")) or ($subtotal > 1000 and $method->method_id == "free_shipping")){
    ?>
    <li>
        <input type="radio" name="shipping_method[<?php echo $index; ?>]" data-index="<?php echo $index; ?>" id="shipping_method_<?php echo $index; ?>_<?php echo sanitize_title( $method->id ); ?>" value="<?php echo esc_attr( $method->id ); ?>" <?php checked( $method->id, $chosen_method ); ?> class="shipping_method" />
        <label for="shipping_method_<?php echo $index; ?>_<?php echo sanitize_title( $method->id ); ?>"><?php echo wp_kses_post( wc_cart_totals_shipping_method_label( $method ) ); ?></label>
    </li>
 <?php 
 }
endforeach; ?>

This is almost what I need but Total price (price of all items in cart inluding shipping price) sometimes doesn't fit because this price is calcualed in WC_Cart class and shpping method has been selected yet. Does anybody know any way to solve this problem or any entire cleaner way (except this payd plugin)?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Here's the snippet you need to use. Using woocommerce_package_rates filter hook you can conditionally set or unset the shipping methods

add_filter( 'woocommerce_package_rates', 'custom_package_rates', 10, 2 );
function custom_package_rates( $rates, $package ) {

    $total = WC()->cart->cart_contents_total;

    if( 300 <= $total ) {

        unset( $rates['flat_rate'] );
        unset( $rates['free_shipping'] );

    } elseif ( 301 >= $total && 1000 <= $total ) {

        unset( $rates['local_delivery'] );
        unset( $rates['free_shipping'] );

    }

    // etc add the remaining condition

    return $rates;
} 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...