I'm using Wordpress 3.9.14.
I use the flat_rate
method for my shipping costs.
It contains a default cost_per_order
and I use 2 shipping_classes
inside flat_rate
for additional costs for some products.
I want to show the shipping costs on the product page.
Right now I use the $product->shipping_class_id()
to get the ID but I can't figure out how to get the costs for this shipping_class_id.
Because if I know the costs I can add this to the cost_per_order
to calculate the total shipping costs for the product.
I also can't figure out how to get the cost_per_order
value.
If there is no solution at all, I can use as an emergency solution a switch/case
statement and put the costs in there but then I need to change the code when I change the flat_rate
costs, so I don't prefer this kind of solutions.
I have figured it out:
$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);