3

I am developing an eCommerce site using WooCommerce for a client, and they want to be able to show the shipping cost of a product on the product page (because that's what they have on their old store site). Is there a way to do this? I've looked around and can't find anything definitive, and although there is a similar question here on StackExchange, no one has answered the question.

I'm aware that there's a $79 plugin to do this, but is there a way to set the shipping cost per item as well? Would rather avoid purchasing a plugin.

closed as off-topic by Pieter Goosen, mor7ifer, Mayeenul Islam, Mark Kaplun, cybmeta Feb 4 '15 at 11:35

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

8

You can present the shipping costs per item in WooCommerce 2.x. enter image description here

Since you have set the prices as fixed and per item you can show them in the product description by simply writing them into the product description, or you can access the shipping class for the item (it's a taxonomy) "product_shipping_class" that can be assigned to each product individually or via quick edit to all products.

How to show it in a product template? Each product page is divided up into a directory called woocommerce. You can copy that directory from the plug-in directory for Woocommerce directly into your theme folder. Once there you can make any changes without having future updates overwrite your theme changes. Here's the link to the instructions for woocommerce theme files.

So you start with the 'single-product.php' page. This is the main loop for the single product page, and then 'content-single-product.php' which details the hooks within the product template.

Depending on where you want to display the shipping information you can now access the terms assigned to the product (in case you have different rates for different products). Use get_the_terms( $post->ID, 'product_shipping_class'). For more information on working with call see this post.

Using some php to parse out the array you will know the shipping class and you can then display information about the shipping costs with a calculation based on the shipping class returned.

There are a few more ways to do this that involve the shipping class object, but this should get your started.

  • Thanks for that Steve, just one follow-up question - I've used that snippet/call you put above by adapting it to create a variable first and then echo the variable in the right place in the template. However, it just prints out the word 'Array'. My PHP isn't great, so not sure how to proceed from here? In WC, I've created a Shipping Class for each price (Delivery: £3.00, Delivery: £4.00 etc), and assigned a cost to them in the Shipping section of the settings. – tristanojbacon Oct 7 '13 at 2:12
  • Try printing out the variable with var_dump($variable). You should see what is in the array. You can then call the specific items by their array structure. – Steve Eldridge Oct 7 '13 at 14:54
  • I did a var_dump, however it printed bool(false)..... – tristanojbacon Oct 7 '13 at 14:59
  • Ok, did you assign a shipping class to the items? – Steve Eldridge Oct 7 '13 at 17:41
  • Yes, the item I tested this on has a shipping class called Delivery: £3.00, which I have linked to the Flat Rate shipping table, costing £3.00. I have confirmed that this works, because when I add the item to the cart, the delivery cost says £3.00 – tristanojbacon Oct 7 '13 at 18:17
3

Old post but might help to display shipping cost price

<?php
$slug = $product->get_shipping_class_id();
$slugobject = get_term_by('term_taxonomy_id', $slug, 'product_shipping_class');
echo $slugobject->description;?>

This script will work for flat rate shipping type:

global $product; /* Remove if set already.. */
$shipping_class = $product->get_shipping_class();
$flat_rate = new WC_Shipping_Flat_Rate;
$symbol = get_woocommerce_currency_symbol();
echo $symbol . $flat_rate->flat_rates[$shipping_class]['cost'];
  • 1
    This doesn't work anymore on WooCommerce 2.6+ – Avishai Feb 1 '17 at 18:30

Not the answer you're looking for? Browse other questions tagged or ask your own question.