如何获得woocommerce免费送货的最低订单金额

问题描述 投票:7回答:2

我怎样才能获得免费送货所需的最低订单金额(woocommerce_free_shipping_min_amount在管理面板中设置woocommerce - >设置 - >送货 - >免费送货 - >最低订单金额)在woocommerce?

我想在前端页面显示此价格

php wordpress woocommerce shipping
2个回答
8
投票

该值存储在密钥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'];

3
投票

接受的答案不再适用于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;

希望这有助于某人。

© www.soinside.com 2019 - 2024. All rights reserved.