Btw, you could checkout this shipping calculator plugin that display shipping info on product page https://codecanyon.net/item/woocommerce-shipping-calculator-on-product-page/11496815 , how they implement is quite simple but smart, basically what they did is add an item into the Woocommmerce (WC) cart programmatically, then use the existing WC code that is used in the checkout page to get the shipping information. So there’s no need to reinvent the wheel/functions on getting the shipping information. I have improvise their code to generate the string of shipping for multiple countries on a product. Wonder if you can follow some inspiration/solution from them. Here is roughly how
public function googleShippingFeedStr(){
$originalSetting = $_POST['calc_shipping_country'];
$countries = ['US','CA', 'AU', 'GB', 'CH', 'SE', 'DK', 'FR', 'DE', 'AT', 'BE', 'IE', 'IL', 'IT', 'JP',
'NL', 'NZ', 'NO', 'PL', 'PT', 'ZA', 'ES', 'TR', 'SG', 'MY', 'ID', 'AE', 'SA', 'KR', 'TH', 'VN', 'MX'];
$shipCountryFeed = [];
foreach($countries as $country){
$_POST['calc_shipping_country'] = $country;
WC_Shortcode_Cart::calculate_shipping();
if (isset($_POST["product_id"]) && $this->check_product_incart($_POST["product_id"]) === false) {
$qty = (isset($_POST['current_qty']) && $_POST['current_qty'] > 0) ? $_POST['current_qty'] : 1;
if (isset($_POST['variation_id']) && $_POST['variation_id'] != "" && $_POST['variation_id'] > 0) {
$cart_item_key = WC()->cart->add_to_cart($_POST["product_id"], $qty, $_POST['variation_id']);
} else {
$cart_item_key = WC()->cart->add_to_cart($_POST["product_id"], $qty);
}
$packages = WC()->cart->get_shipping_packages();
$packages = WC()->shipping->calculate_shipping($packages);
$available_methods = WC()->shipping->get_packages();
WC()->cart->remove_cart_item($cart_item_key);
} else {
$packages = WC()->cart->get_shipping_packages();
$packages = WC()->shipping->calculate_shipping($packages);
$available_methods = WC()->shipping->get_packages();
}
wc_clear_notices();
if ($this->get_setting('shipping_type') == 2) {
if (isset($available_methods[0]["rates"]) && count($available_methods[0]["rates"]) > 0) {
$count = 0;
//echo '<ul class="shipping_with_price">';
foreach ($available_methods[0]["rates"] as $key => $method) {
$finalCost=$method->cost;
if(isset($method->taxes) && !empty($method->taxes)){
foreach($method->taxes as $tax){
$finalCost=$finalCost+$tax;
}
}
//echo "<li>";
//echo wp_kses_post($method->label) . " <strong>(" . wc_price($finalCost) . ")</strong>";
//echo "</li>";
$pos = false;
$shipName = $method->label;
if( ($pos = stripos($shipName, '[')) !== FALSE )
$shipName = trim(substr($method->label, 0, $pos ));
$shipCountryFeed[] = $country.'::'.$shipName.':USD'.$finalCost;
break; //only the first default most important shipping
}
//echo '</ul>';
}
}
}
//reset
$_POST['calc_shipping_country'] = $originalSetting;
WC_Shortcode_Cart::calculate_shipping();
return join(',', $shipCountryFeed);
}
-
This reply was modified 5 years, 10 months ago by
esia168.
-
This reply was modified 5 years, 10 months ago by
esia168.
-
This reply was modified 5 years, 10 months ago by
esia168.