Skip to content

Instantly share code, notes, and snippets.

@armand1m
Created October 11, 2016 03:08
Show Gist options
  • Save armand1m/7ee11c2c8e51cf8b2164f8c9bc4648f9 to your computer and use it in GitHub Desktop.
Save armand1m/7ee11c2c8e51cf8b2164f8c9bc4648f9 to your computer and use it in GitHub Desktop.
woocommerce snippet
<?php
function wc_get_country() {
$_country = WC()->customer->get_country();
if ( $_country !== WC()->customer->get_shipping_country() && 'shipping' === get_option('wc_price_based_country_based_on', 'billing') ) {
$_country = WC()->customer->get_shipping_country();
}
return $_country;
}
var_dump(wc_get_country());
WC()->customer->set_country('US');
WC()->customer->set_shipping_country('US');
var_dump(wc_get_country());
/**
* Set WooCommerce customer country
*
* @param string $country
*/
function st_cxs( $country ) {
$ship_to_different_address = get_option( 'woocommerce_ship_to_destination' ) === 'shipping' ? 1 : 0;
if (
WC()->customer->get_country() !== WC()->customer->get_shipping_country() &&
'shipping' === get_option('wc_price_based_country_based_on', 'shipping') &&
'1' == apply_filters( 'woocommerce_ship_to_different_address_checked', $ship_to_different_address )
)
{
WC()->customer->set_shipping_country( $country );
} else {
WC()->customer->set_country( $country );
WC()->customer->set_shipping_country( $country );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment