Sunteți pe pagina 1din 9

4/12/2019 php - Display shipping cost on product page - WooCommerce - Stack Overflow

The results are in! See what nearly 90,000 developers picked as their most loved, dreaded, and desired
coding languages and more in the 2019 Developer Survey.

Display shipping cost on product page ­ Ask Question

WooCommerce

I'm trying to make a
custom page on
2 Wordpress that display
2 subscription types in
a form with 3
variations each
(monthly, 6 month, 12
3 month). Each variation
gets a radio button and
I have a total price that
is live updated when
user clicks on the radio
buttons. This part is
working.

But now, I want to add
3 other radio buttons
to choose the
shipment method.
(When user select
one, it'll live update the
total price too).

I've been searching a
long time how to get
shipping cost for a
product but nothing
has worked.

Anyone knows :) ?

php   wordpress  

woocommerce  
By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our
Terms of Service.

https://stackoverflow.com/questions/47409734/display-shipping-cost-on-product-page-woocommerce 1/9
4/12/2019 php - Display shipping cost on product page - WooCommerce - Stack Overflow

shipping  

woocommerce­subscriptions

edited Nov 21 '17 at 19:54
LoicTheAztec
97.4k 14 71 114

asked Nov 21 '17 at 9:37
Alex Vand
1,244 1 5 22

In woocommerce the
shipment options are
enabled in cart and
checkout, only once
you have added a
product to cart, as
they are based on
the shipping zones
(customer location)…
So when a new
customer buy
something, he can
add the related
Location data
needed by Shipping
in checkout page
(optionally in cart)…
For each shipping
method in each
shipping zone, you
can set different
prices using shipping
classes, then you
can set the right
shipping class to
each product or
product variation…
To enable shipping
choice in the product
itself, is an advanced
and complicated
development. –
 LoicTheAztec Nov
21 '17 at 19:59

[...] I'm a developper,
so i can add this on
my custom page with
php code, hooks or
anything that can
helps me to do this.
Maybe i'll have to do
tricky things to make
it works. Actually, I
By using our site, you acknowledge that you have read and understand our 
made a custom page Cookie Policy, Privacy Policy, and our
Terms of Service. with a custom form
for choosing between
https://stackoverflow.com/questions/47409734/display-shipping-cost-on-product-page-woocommerce 2/9
4/12/2019 php - Display shipping cost on product page - WooCommerce - Stack Overflow
my variable
subscriptions. If you
want an exemple of
what i want, it's
almost exactly the
same as :
lepetitballon.com/sub
scription (sorry it's in
french). –
  Alex Vand  Nov 22
'17 at 21:25

1 Answer

This question is too
broad. So I can
2 answer partially as
you should need to
make some work
yourself, and ask later
more specific
questions…

Now the correct
way to set shipping
data on product
page, is to use
Ajax to update
the data, as the
action is maid by
the customer on
client side
(browser), avoiding
'post' and reload
the page. But this
should be your
work...

1) Customer location
(for shipping Zones):

You should need to
get the customer
location or shipping
zone first.

Then you will need to
update the customer
By using our site, you acknowledge that you have read and understand our 
country in  WC()- Cookie Policy, Privacy Policy, and our
Terms of Service. >session  and in 

https://stackoverflow.com/questions/47409734/display-shipping-cost-on-product-page-woocommerce 3/9
4/12/2019 php - Display shipping cost on product page - WooCommerce - Stack Overflow

WC()->customer
objects. This can be
done with the
following:

## Get the geolocate


$country_code = WC()
// or
// $country_code = W

## Set a new country


$new_country_code =

## 1. WC_session: se

// Get the data


$customer_session =
// Change some data
$customer_session['c
$customer_session['s
// Set the changed d
$customer_session =

## 2. WC_Customer: s

WC()->customer->set_
WC()->customer->set_

2) The shipping
methods (by
Shipping Zone, with
costs):

In Woocommerce the
Shipping methods for
a Shipping Zone are
only available when
customer add a
product to cart…

Based on this answer
code: Display
shipping methods to
frontend as in the
admin panel?  
we can make a
custom array of the
necessary data to be
used to get the
shipping methods by
shipping Zones, with
costs and everything
By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our
Terms of Service. needed.

https://stackoverflow.com/questions/47409734/display-shipping-cost-on-product-page-woocommerce 4/9
4/12/2019 php - Display shipping cost on product page - WooCommerce - Stack Overflow

The code below is
much more complete
and include the
shipping methods
costs:

// Initializing vari
$zones = $data = $cl

// Rest of the World


$zone
$zones[$zone->get_id
$zones[$zone->get_id
>get_formatted_locat
$zones[$zone->get_id
>get_shipping_method

// Merging shipping
$shipping_zones = ar

// Shipping Classes
$shipping
$shipping_classes

// The Shipping Clas


foreach($shipping_cl
//
$key_class_cost

// The shipping
$classes_keys[$s
'term_id' =>
'name' => $s
'slug' => $s
'count' => $
'key_cost' =
);
}

// For 'No class" co


$classes_keys[0] = a
'term_id' => '',
'name' => 'No s
'slug' => 'no_cl
'count' => '',
'key_cost' => 'n
);

foreach ( $shipping_
$zone_id = $ship
$zone_name = $zo
$shipping_zone['zone
$zone_locations
$zone_location_n

// Set the data


$data[$zone_id]=
'zone_id'
'zone_name'
'zone_locati
By using our site, you acknowledge that you have read and understand our 
'zone_locati Cookie Policy, Privacy Policy, and our
Terms of Service. 'shipping_me
);
https://stackoverflow.com/questions/47409734/display-shipping-cost-on-product-page-woocommerce 5/9
4/12/2019 php - Display shipping cost on product page - WooCommerce - Stack Overflow

foreach ( $shipp
$method_id
$instance_id
$enabled = $
// Settings
$instance_se
if( $enabled
$data[$z
'$me
'ins
'rat
'def
'cus
);

if( $met
$dat
$instance_settings['
$dat
= $instance_settings
}
if( $met
$dat
= $instance_settings
$dat
$sm_obj->cost;
}
if( $met
$dat
= $instance_settings
$dat
['calculation_type']
$cla
fore

$dat
['classes_&_costs']
}
}
}
}

// Row output (for t


echo '<pre>'; print_

custom shipping
methods 
Now if you are
using custom
shipping
methods (enabled
sometimes by 3rd
party shipping
plugins) you will
By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our
Terms of Service. need to make

https://stackoverflow.com/questions/47409734/display-shipping-cost-on-product-page-woocommerce 6/9
4/12/2019 php - Display shipping cost on product page - WooCommerce - Stack Overflow

some changes in
the code…

Costs and taxes
calculation 
You should need to
make the taxes
calculations, as
the costs are
displayed just as
they are set in
shipping settings…

3) Product page

Customer location:  
You will need first to
have a location
selector (to define the
Shipping Zone) or to
set the location based
on Woocommerce
geolocation.

Shipping Methods:  
Once the Shipping
Zone is defined, you
can get the
corresponding
Shipping Methods
and rates (costs),
displaying on this
product page your
radio buttons for
Shipping methods.

To get this, you should
need to alter the
single product pages:

Overriding the
Woocommerce
templates via
your theme (and
WC Subscriptions
templates too)
Using the
available filters
and action hooks
By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our
Terms of Service.

https://stackoverflow.com/questions/47409734/display-shipping-cost-on-product-page-woocommerce 7/9
4/12/2019 php - Display shipping cost on product page - WooCommerce - Stack Overflow

Use Javascript,
jQuery to alter
prices and Ajax
to update the
customer data
( WC_Session  and
WC_Customer ).

You should need to
get/set/update the
"chosen_shipping_
methods" with the
following code (Ajax).

Get the Chosen
Shipping method:

$chosen_shipping = W

Set/Update the
Chosen Shipping
method (through
Javascript/Ajax and 
admin-ajax.php ):

// HERE the new meth


$method_rate_id = ar

// Set/Update the Ch
WC()->session->set(

edited Nov 23 '17 at 5:31

answered Nov 23 '17 at 4:55
LoicTheAztec
97.4k 14 71 114

Your answer's
gonna helps me a
lot ! I think this is
exactly what i need.
I missed those
"basics"
informations on how
Woocommerce
works. It will be
easier now to find
new missing
By using our site, you acknowledge that you have read and understand our 
informations. Thank Cookie Policy, Privacy Policy, and our
Terms of Service. you so much for the
time you took for my
https://stackoverflow.com/questions/47409734/display-shipping-cost-on-product-page-woocommerce 8/9
4/12/2019 php - Display shipping cost on product page - WooCommerce - Stack Overflow
problem. I hope it
will helps some
other people too.
When i finish it, i will
post my solution
here. Have a nice
day –  Alex Vand
Nov 23 '17 at 9:35

Think it is, so i mark
it as accepted :) –
  Alex Vand  Nov 23
'17 at 15:06

By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our
Terms of Service.

https://stackoverflow.com/questions/47409734/display-shipping-cost-on-product-page-woocommerce 9/9

S-ar putea să vă placă și