Reputation: 13
I would like to create a plugin to add 2 shortcodes to my woocommerce shop so I can add the shipping cost as shortcode in text like this example:
Our shipping costs to Amsterdam is [shipping_cost_1] Euro
So on front end the text will be like this: Our shipping costs to Amsterdam is 6,95 Euro
These are the rates and shortcodes I want to use:
[shipping_cost_1] = flat rate woocommerce shipping cost including VAT for shipping zone 1 (shipping inside Amsterdam) [shipping_cost_2] = flat rate woocommerce shipping cost including VAT for shipping zone 2 (shipping outside Amsterdam)
For reference, this is where the shipping costs for Amsterdam are displayed on the checkout page:
I would like to add the code in the following structure:
// The shortcode function
function shipping_cost_display_1() {
// Get shipping cost from woocommerce
???
// Ad code returned
???
// Register shortcode
add_shortcode('shipping_cost_1', 'shipping_cost_display_1');
Upvotes: 0
Views: 993
Reputation: 1132
just wondering if you checked this article already: WooCommerce: Show Shipping Rates @ Single Product Page?
Basically you can get the shipping zones with a simple command:
WC_Shipping_Zones::get_zones();
Once you have them, you can loop over the array and find rates inside ($instance['cost']).
The following should do the trick for you - of course change "amsterdam" to your desired zone name:
function shipping_cost_display_1() {
$zones = WC_Shipping_Zones::get_zones();
foreach ( $zones as $zone_id => $zone ) {
if ( "amsterdam" !== $zone['zone_name'] ) continue;
$zone_shipping_methods = $zone['shipping_methods'];
foreach ( $zone_shipping_methods as $index => $method ) {
$instance = $method->instance_settings;
return $instance['cost'];
}
}
}
add_shortcode( 'shipping_cost_1', 'shipping_cost_display_1' );
There is also to say that the same shortcode could be used to return the two rates, by using a shortcode parameter.
Upvotes: 0