I am using WooCommerce plugin and have the following query:

Within the single product page, I have a product where based on certain selections by the buyer, a shipping cost value is calculated, which I then need to pass/include as part of the cart items as a new column, "Shipping Cost" when viewing your cart.

The thing is though, the buyer might decide to buy say three different products where each of these three products might have varied shipping cost calculations, after pressing the "Add to Cart" button three times.

For example:

Product A selections calculates a Shipping Cost of $2.00
Product B selections calculates a Shipping Cost of $4.00
Product C selections calculates a Shipping Cost of $6.00

So based on the above, when the buyer has finished shopping and presses "View Cart", I want them to see:

Item                       Price      Shipping Cost      Quantity          Total     
Product A                      $10        $2                     1                 $10
Product B                      $5         $4                     1                 $5
Product C                      $15        $6                     1                 $15

It is the "Shipping Cost" custom field column I would like to add to the cart, which is what I am unsure how to do.

Further to the above, I also want to add to the "Cart Totals" below, another custom field of "Shipping Cost" which would total $12, which would then beed added to overall Product Total, i.e.

Cart Totals

Cart Subtotal       $30
Shipping Cost       $12

Order Total         $42

Some of the code that I have used for the Cart Total shipping cost is:

function woo_add_cart_fee() {
  global $woocommerce;
  $woocommerce->cart->add_fee( __('Shipping Cost', 'woocommerce'), 100 );
add_action( 'woocommerce_before_calculate_totals', 'woo_add_cart_fee');

So, I need to know how to add the "Shipping Cost" to both the Cart Product Details as well as to the Cart Totals.

Any coding help would be much appreciated.


  • What code have you tried so far? – Brad Dalton Jan 26 '14 at 4:50
  • Hi, have updated my post above but have harded-coded 100, which is not want I want but calculated. – tonyf Jan 26 '14 at 5:24
  • This is not a wordpress question but a woocommerce one, you better ask it in the support channels of the plugin as this kind of questions are mostly off-topic here – Mark Kaplun Jan 26 '14 at 5:25
  • Strange as I have seen alot of questions in this forum with the WooCommerce tag. – tonyf Jan 26 '14 at 5:27
  • yes, and the useful answer rate is not much above zero. – Mark Kaplun Jan 26 '14 at 9:09

First, store the custom field when you're editing your product. Lets say you're using the custom_shipping_cost custom field. Make sure its stored as a number, 20 for example, NOT $20.00

Then, you need to display this field on the cart page. Sadly, theres no filter for adding a new column in the cart table, so you need to edit the template file, or if its not required to be a column, you can do this instead, this code will add the extra value to the last column:

function additional_shipping_cost($subtotal, $values, $cart_item_key) {
    //Get the custom field value
    $custom_shipping_cost = get_post_meta($post->ID, 'custom_shipping_cost', true);

    //Just for testing, you can remove this line
    $custom_shipping_cost = 10;

    //Check if we have a custom shipping cost, if so, display it below the item price
    if ($custom_shipping_cost) {
        return $subtotal.'<br>+'.woocommerce_price($custom_shipping_cost).' Shipping Cost';
    } else {
        return $subtotal;   

So with this, the first part of the question is done. If you want to display it as your example above, you need to duplicate the plugins/woocommerce/templates/cart/cart.php file to themes/yourtheme/woocommerce/cart/cart.php. Then edit the file, add your own column, you can use the code above to display the price.

After this, we need to update the cart totals with the additional costs. Your code with the add_fee comes in handy:

function woo_add_cart_fee() {
    global $woocommerce;

    $extra_shipping_cost = 0;
    //Loop through the cart to find out the extra costs
    foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
        //Get the product info
        $_product = $values['data'];

        //Get the custom field value
        $custom_shipping_cost = get_post_meta($_product->id, 'custom_shipping_cost', true);

        //Just for testing, you can remove this line
        $custom_shipping_cost = 10;

        //Adding together the extra costs
        $extra_shipping_cost = $extra_shipping_cost + $custom_shipping_cost;

    //Lets check if we actually have a fee, then add it
    if ($extra_shipping_cost) {
        $woocommerce->cart->add_fee( __('Shipping Cost', 'woocommerce'), $extra_shipping_cost );
add_action( 'woocommerce_before_calculate_totals', 'woo_add_cart_fee');

Thats it, it should work after this. Make sure to remove the Just for testing... lines from both of the codes, i didn't created the custom field on my site for testing.

  • Hi passatgt - I really appreciate your help and excellent example with explanation. I will try what you have suggested and if ok, is it alright if I come back to you with any hiccups. – tonyf Jan 26 '14 at 12:26
  • If its related to the original question, sure:) – passatgt Jan 26 '14 at 12:34

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