【问题标题】:Woocommerce how can I enable shipping method depending on total priceWoocommerce如何根据总价启用运输方式
【发布时间】:2015-10-08 11:36:09
【问题描述】:

我想根据这些条件更改允许的运输方式:

如果购物车小计少于 300 欧元,则允许的送货方式是本地送货。

如果购物车小计介于 300 和 1000 欧元之间,则允许的运输方式是统一费率。

超过 1000 欧元免运费。

可以在管理员设置中设置免费送货限制。但是不能设置休息。 我试图在我的自定义 cart-shiping.php WC 模板中更改它。这样:

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; ?>

这几乎是我需要的,但总价格(购物车中所有商品的价格,包括运费)有时不合适,因为这个价格是在 WC_Cart 类中计算的,并且尚未选择配送方式。 有谁知道解决这个问题的任何方法或任何更清洁的方法 (this payd plugin 除外)?

【问题讨论】:

  • 目前我面前没有 woocommerce,但没有可以用来更改总价的过滤器吗?
  • 也许吧。但我宁愿需要钩子来更改 WC 会话对象中选择的方法。

标签: wordpress woocommerce


【解决方案1】:

这是您需要使用的 sn-p。使用woocommerce_package_rates 过滤器挂钩,您可以有条件地设置或取消设置运输方式

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;
} 

【讨论】:

  • 实际上我同时做了几乎完全相同的解决方案。当我在谷歌上搜索更多内容时,我发现了this。严格地说 woocommerce_package_rates 钩子是我需要的。不过谢谢你的回复。
  • 你也可以用$package-&gt;contents_cost代替WC()-&gt;cart-&gt;cart_contents_total
猜你喜欢
  • 1970-01-01
  • 2014-07-05
  • 2019-08-10
  • 1970-01-01
  • 2021-12-08
  • 2018-10-28
  • 2017-09-20
  • 2018-04-27
  • 2018-08-13
相关资源
最近更新 更多