Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Specific zones id in Woocommerce WordPress

  • REFUNDED

Hi, can someone help me to put the right code to make my special item and regular item have specific zone id? Right now my special item and regular item have specific shipping method and class, but not zone yet.

I have one country in two Zones, but Woocommerce only read default the first zone only.That is why I try to use this kind method. If you have function code so my cart can read one country in all zone, please tell me.

My shipping zone id
page=wc-settings&tab=shipping&zone_id=246 (FOR SPECIAL ITEM)
page=wc-settings&tab=shipping&zone_id=247 (FOR REGULAR ITEM)



<?php
add_filter( 'woocommerce_cart_shipping_packages', 'split_shipping_packages_shipping_class' );
function split_shipping_packages_shipping_class( $packages ) {
// Reset the packages.
$packages = array();

// Special items.
$special_items = array();
$regular_items = array();

// Sort free from regular
foreach ( WC()->cart->get_cart() as $item ) {
if ( $item['data']->needs_shipping() ) {
if ( $item['data']->get_shipping_class() === 'china') {
$special_items[] = $item;
} elseif($item['data']->get_shipping_class() === 'japan') {
$regular_items[] = $item;
}
}
}
if ( $regular_items ) {
$packages[] = array(
'ship_via' => array( 'shipping1' ),
'contents' => $regular_items,
'contents_cost' => array_sum( wp_list_pluck( $regular_items, 'line_total' ) ),
'applied_coupons' => WC()->cart->applied_coupons,
'destination' => array(
'country' => WC()->customer->get_shipping_country(),
'state' => WC()->customer->get_shipping_state(),
'postcode' => WC()->customer->get_shipping_postcode(),
'city' => WC()->customer->get_shipping_city(),
'address' => WC()->customer->get_shipping_address(),
'address_2' => WC()->customer->get_shipping_address_2(),
),
);
}

// Put inside packages
if ( $special_items ) {
$packages[] = array(
'ship_via' => array( 'shipping2' ),
'contents' => $special_items,
'contents_cost' => array_sum( wp_list_pluck( $special_items, 'line_total' ) ),
'applied_coupons' => WC()->cart->applied_coupons,
'destination' => array(
'country' => WC()->customer->get_shipping_country(),
'state' => WC()->customer->get_shipping_state(),
'postcode' => WC()->customer->get_shipping_postcode(),
'city' => WC()->customer->get_shipping_city(),
'address' => WC()->customer->get_shipping_address(),
'address_2' => WC()->customer->get_shipping_address_2(),
),
);
}
return $packages;
}

?>

Answers (2)

2018-08-21

Arnav Joy answers:

Do you have site url I can see?


User180181 comments:

I believe no need to see site url for this solution. I just looking how to apply specific zones id to this code

2018-08-22

Reigel Gallarde answers:

Sorry I don't get what's the point. If you can include a video or screenshot, I might be able to help.


User180181 comments:

Hi Reigel Gallarde, it's good if you able to help me.
click this image https://imgur.com/ZLy2JMN

I use this code to separate ship item from different country. Zone 1 from Malaysia (means item from Malaysia = $regular_items) and Zone 1 from China (means item from China = $special_items).Every zone have Malaysia country where buyer from that country can buy from this website.

Problem come, when Woocommerce only allow read first country, see the image no 1, Malaysia for APG Shipping no 1,it's show at the cart. But cannot read Malaysia for Weight Based Shipping for item Ship from China.

When I try change Malaysia for APG Shipping become a second place, so the cart can't read and only read from Malaysia for Weight Based Shipping.

So I my idea, can we put zone id into code, so cart can read it.

url:page=wc-settings&tab=shipping&zone_id=246 (FOR SPECIAL ITEM)
url:page=wc-settings&tab=shipping&zone_id=247 (FOR REGULAR ITEM)

I see the code from here http://woocommerce.wp-a2z.org/oik_api/wc_shipping_zonesget_zone_by/

So the idea, can we put zone id code into the code for regular item and special item
https://imgur.com/HTxtvxx (example code only, I'm not a coder, so I just try and paste,and I know this is wrong)

Hope you can give solution to this
Thank you