【问题标题】:Woocommerce - Get shipping costs on the product pageWoocommerce - 在产品页面上获取运费
【发布时间】:2017-03-09 20:11:57
【问题描述】:

我正在使用 Wordpress 3.9.14。

我使用flat_rate 方法计算运费。
它包含一个默认的cost_per_order,我在flat_rate 中使用了2 个shipping_classes,用于某些产品的额外费用。

我想在产品页面上显示运费。
现在我使用$product->shipping_class_id() 获取 ID,但我不知道如何获取此 shipping_class_id 的费用。
因为如果我知道费用,我可以将其添加到cost_per_order 以计算产品的总运费。

我也不知道如何获得cost_per_order 值。

如果根本没有解决方案,我可以使用switch/case 语句作为紧急解决方案并将成本放在那里,但是当我更改flat_rate 成本时我需要更改代码,所以我不这样做'不喜欢这种解决方案。

【问题讨论】:

  • 你真的应该更新你的 WordPress 版本。 3.9.14 已经过时了。
  • 更新 WordPress 已在我的待办事项列表中,但现在我没有时间去做。而且我知道他们更改了运输模块,但对于像我这样仍在使用 Wordpress 3 的人,我认为该解决方案可能非常有帮助。

标签: php wordpress woocommerce shipping


【解决方案1】:

我想通了:

$shipping_class_id = $product->get_shipping_class_id();
$shipping_class= $product->get_shipping_class();
$fee = 0;

if ($shipping_class_id) {
   $flat_rates = get_option("woocommerce_flat_rates");
   $fee = $flat_rates[$shipping_class]['cost'];
}

$flat_rate_settings = get_option("woocommerce_flat_rate_settings");
echo 'Shipping cost: ' . ($flat_rate_settings['cost_per_order'] + $fee);

【讨论】:

  • 你想在哪里展示它。只要你有你的产品变量,你就应该很好。
  • 好的,谢谢@FamousWolluf
【解决方案2】:

我在 woocommerce/checkout/review-order.php 中完成了我的需要。 所以我认为你可以在任何地方使用它......

$product = wc_get_product($cart_item['product_id']);

// just to know what we have
$shipping_class_id = $product->get_shipping_class_id();
$shipping_class = $product->get_shipping_class();

// name of shipping class if you need
$shipping_class_term = get_term( $shipping_class_id, 'product_shipping_class' );
echo $shipping_class_term->name;

// and here IF we know $instance_id (is shipping method id in shipping zone admin page) 
$instance_id = 12; // who knows how to get it with code - welcome
$flat_rate = new WC_Shipping_Flat_Rate($instance_id);
$symbol = get_woocommerce_currency_symbol();

echo $symbol.$flat_rate->instance_settings['class_cost_'.$shipping_class_id]);

希望这对某人有所帮助。还是有更好的办法?

【讨论】:

  • 你的解决方案使用的是固定的 instance_id,而我的解决方案是使用当前产品而不使用固定的 instance_id。
猜你喜欢
  • 2018-05-04
  • 2017-02-21
  • 1970-01-01
  • 2013-02-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-06
相关资源
最近更新 更多