【问题标题】:How to get minimum order amount for free shipping in woocommerce如何在woocommerce中获得免费送货的最低订单金额
【发布时间】:2014-12-22 07:20:07
【问题描述】:

如何在 woocommerce 中获取获得免费送货所需的最低订单金额(woocommerce_free_shipping_min_amount 在 woocommerce 管理面板 -> 设置 -> 送货 -> 免费送货 -> 最低订单金额中设置)?

我想在前端页面显示这个价格

【问题讨论】:

    标签: php wordpress woocommerce shipping


    【解决方案1】:

    此值存储在 option 中,位于键 woocommerce_free_shipping_settings 下。它是一个由WC_Settings_API->init_settings() 加载的数组。

    如果你想直接访问它,你可以使用get_option():

    $free_shipping_settings = get_option( 'woocommerce_free_shipping_settings' );
    $min_amount = $free_shipping_settings['min_amount'];
    

    【讨论】:

    【解决方案2】:

    从 WooCommerce 2.6 版开始,接受的答案不再有效。它仍然提供输出,但该输出是错误的,因为它没有使用新引入的 Shipping Zones。

    为了获得特定区域包邮的最低消费金额,试试我整理的这个功能:

    /**
     * Accepts a zone name and returns its threshold for free shipping.
     *
     * @param $zone_name The name of the zone to get the threshold of. Case-sensitive.
     * @return int The threshold corresponding to the zone, if there is any. If there is no such zone, or no free shipping method, null will be returned.
     */
    function get_free_shipping_minimum($zone_name = 'England') {
      if ( ! isset( $zone_name ) ) return null;
    
      $result = null;
      $zone = null;
    
      $zones = WC_Shipping_Zones::get_zones();
      foreach ( $zones as $z ) {
        if ( $z['zone_name'] == $zone_name ) {
          $zone = $z;
        }
      }
    
      if ( $zone ) {
        $shipping_methods_nl = $zone['shipping_methods'];
        $free_shipping_method = null;
        foreach ( $shipping_methods_nl as $method ) {
          if ( $method->id == 'free_shipping' ) {
            $free_shipping_method = $method;
            break;
          }
        }
    
        if ( $free_shipping_method ) {
          $result = $free_shipping_method->min_amount;
        }
      }
    
      return $result;
    }
    

    将上述函数放在functions.php中,并在模板中使用,如下所示:

    $free_shipping_min = '45';
    
    $free_shipping_en = get_free_shipping_minimum( 'England' );
    if ( $free_shipping_en ) {
        $free_shipping_min = $free_shipping_en;
    }
    
    echo $free_shipping_min;
    

    希望这对某人有所帮助。

    【讨论】:

      猜你喜欢
      • 2016-11-05
      • 1970-01-01
      • 2020-11-26
      • 2017-07-01
      • 2015-08-16
      • 2021-07-05
      • 1970-01-01
      • 2019-07-29
      相关资源
      最近更新 更多