我使用的是 Wordpress 3.9.14。
我使用 flat_rate
我的运费方法。
它包含默认 cost_per_order
我用 2 shipping_classes
里面flat_rate
某些产品的额外费用。
我想在产品页面上显示运费。
现在我使用 $product->shipping_class_id()
获取 ID,但我不知道如何获取此 shipping_class_id 的费用。
因为如果我知道成本,我可以将其添加到 cost_per_order
计算产品的总运费。
我也不知道如何获得 cost_per_order
值(value)。
如果根本没有解决方案,我可以使用 switch/case
作为紧急解决方案声明并将成本放在那里但是当我更改 flat_rate
时我需要更改代码成本,所以我不喜欢这种解决方案。
最佳答案
我想通了:
$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);
关于php - Woocommerce - 在产品页面上获取运费,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40279590/