How to set dynamic postcode in WooCommerce?

I have a marketplace store where multiple sellers can advertise their products, but the shipping estimates are wrong because the system uses the shop’s postcode, not the seller’s, to make the math.

I am looking for a hook, a filter, or something like that, where I could intercept a request for a shipping estimate, preferably passing also the Product ID, so I can get the Seller’s postcode and use it as origin postcode.

While debugging to find out how to do this, I came to cart.js, which is responsible for calculating shipping costs on /cart. It uses this function:

/**
* Handles a shipping calculator form submit.
*
* @param {Object} evt The JQuery event.
*/
shipping_calculator_submit: function( evt ) {
evt.preventDefault();

var $form = $( evt.currentTarget );

block( $( ‘div.cart_totals’ ) );
block( $form );

// Provide the submit button value because wc-form-handler expects it.
$( ‘<input />’ ).attr( ‘type’, ‘hidden’ )
.attr( ‘name’, ‘calc_shipping’ )
.attr( ‘value’, ‘x’ )
.appendTo( $form );

// Make call to actual form post URL.
$.ajax( {
type: $form.attr( ‘method’ ),
url: $form.attr( ‘action’ ),
data: $form.serialize(),
dataType: ‘html’,
success: function( response ) {
update_wc_div( response );
},
complete: function() {
unblock( $form );
unblock( $( ‘div.cart_totals’ ) );
}
} );
}
};

$form.serialize() has the city, state and postcode that the customer entered in the form. Then I went to the action to see how it performs in the back-end, so I ended up in woocommerce/includes/shortcodes/class-wc-shortcode-cart.php:

/**
* Calculate shipping for the cart.
*/
public static function calculate_shipping() {
try {
WC()->shipping->reset_shipping();

$country = isset( $_POST[‘calc_shipping_country’] ) ? wc_clean( wp_unslash( $_POST[‘calc_shipping_country’] ) ) : ”; // WPCS: input var ok, CSRF ok, sanitization ok.
$state = isset( $_POST[‘calc_shipping_state’] ) ? wc_clean( wp_unslash( $_POST[‘calc_shipping_state’] ) ) : ”; // WPCS: input var ok, CSRF ok, sanitization ok.
$postcode = isset( $_POST[‘calc_shipping_postcode’] ) ? wc_clean( wp_unslash( $_POST[‘calc_shipping_postcode’] ) ) : ”; // WPCS: input var ok, CSRF ok, sanitization ok.
$city = isset( $_POST[‘calc_shipping_city’] ) ? wc_clean( wp_unslash( $_POST[‘calc_shipping_city’] ) ) : ”; // WPCS: input var ok, CSRF ok, sanitization ok.

if ( $postcode && ! WC_Validation::is_postcode( $postcode, $country ) ) {
throw new Exception( __( ‘Please enter a valid postcode / ZIP.’, ‘woocommerce’ ) );
} elseif ( $postcode ) {
$postcode = wc_format_postcode( $postcode, $country );
}

if ( $country ) {
WC()->customer->set_location( $country, $state, $postcode, $city );
WC()->customer->set_shipping_location( $country, $state, $postcode, $city );
} else {
WC()->customer->set_billing_address_to_base();
WC()->customer->set_shipping_address_to_base();
}

WC()->customer->set_calculated_shipping( true );
WC()->customer->save();

wc_add_notice( __( ‘Shipping costs updated.’, ‘woocommerce’ ), ‘notice’ );

do_action( ‘woocommerce_calculated_shipping’ );

} catch ( Exception $e ) {
if ( ! empty( $e ) ) {
wc_add_notice( $e->getMessage(), ‘error’ );
}
}
}

However, I don’t see where it gets the Shop’s postcode anywhere, I was expecting something like get_option( ‘woocommerce_store_postcode’ );.

That all being said, is it possible to change WooCommerce’s origin postcodes dynamically?

I’m using Dokan, by the way.

Read more here:: How to set dynamic postcode in WooCommerce?

Leave a Reply

Your email address will not be published. Required fields are marked *