In today’s “Watch me customize my WooCommerce marketplace website” episode (see previous customization re: external products) we’ll go printing shipping zones and rates on the single product page, so that users know how much they’ll end up spending once they reach the Cart/Checkout and there are no “hidden” fees. Not bad for some transparency = better sales conversion rate!

You can see the result on my own website, at this link for example: https://www.apieceofsicily.com/en/shop/sicilian-crafts/ciabattino/ – as you can see right below the “Add to Cart” button there is a shipping table with the data taken from the shipping zones.

I must say I use YITH WooCommerce Multi Vendor / Marketplace so that data is actually taken from each vendor’s shipping setup – so in this tutorial we will first see how to get the default “WooCommerce Shipping Zones Data” and in a second snippet we will instead see how to get the info for each vendor instead.

Either way, enjoy!

Here’s the new “Shipping” section on the WooCommerce Single Product Page that gathers data from each shipping zones.

PHP Snippet: Display Shipping Zones & Methods @ WooCommerce Single Product Page (WooCommerce Plugin)

/**
 * @snippet       Show Shipping Rates - WooCommerce Single Product
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 5.1
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */

function bbloomer_yith_vendor_shipping() {
	$zones = WC_Shipping_Zones::get_zones();
	echo '<div><i class="fas fa-truck"></i> ' . __( 'Shipping', 'woocommerce' );
	echo '<table>';
	foreach ( $zones as $zone_id => $zone ) {
		echo '<tr><td>';
		echo $zone['zone_name'] . '</td><td>';
		$zone_shipping_methods = $zone['shipping_methods'];
		foreach ( $zone_shipping_methods as $index => $method ) {
			$instance = $method->instance_settings;
			$cost = $instance['cost'] ? $instance['cost'] : $instance['min_amount'];
			echo $instance['title'] . ' ' . wc_price( $cost ) . '<br>';
		}
		echo '</td></tr>';
	}
	echo '</table></div>';
}

PHP Snippet: Display Vendor’s Shipping Zones & Methods @ WooCommerce Single Product Page (YITH WooCommerce Multi Vendor / Marketplace Plugin)

/**
 * @snippet       Show YITH Vendor Shipping Rates - WooCommerce Single Product
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 5.1
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */

add_action( 'woocommerce_after_add_to_cart_form', 'bbloomer_yith_vendor_shipping' );

function bbloomer_yith_vendor_shipping() {
	global $product;
	$product_id = $product->get_id();
	$vendor = yith_get_vendor( $product, 'product' );
		if ( $vendor->zone_data ) {
			echo '<div><i class="fas fa-truck"></i> ' . __( 'Shipping', 'woocommerce' );
			echo '<table>';
			foreach ( $vendor->zone_data as $key => $zone ) {
				echo '<tr><td><i>';
				echo $zone['zone_name'] . '</i></td><td>';
				$zone_shipping_methods = $zone['zone_shipping_methods'];
				foreach ( $zone_shipping_methods as $index => $method ) {
					$cost = $method['method_cost'] ? $method['method_cost'] : $method['min_amount'];
					echo $method['method_title'] . ' ' . wc_price( $cost ) . '<br>';
				}
				echo '</td></tr>';
			}
			echo '</table></div>';
	}
}
Share:

Leave a Reply