Last active
May 7, 2022 21:43
-
-
Save Preciousomonze/58a1e85557a74a4094f245adb0f8786f to your computer and use it in GitHub Desktop.
This MU-Plugin helps preset or preselect a country/state based on checkout. It can be shipping or billing. All you need to do is call the function 'CLASS::process_selected_country_or_state_etc('NG')' to set the value. Inspired by https://wordpress.org/support/topic/change-session-shipping-country-on-product-page-collection-page-2/ . Use at your β¦
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* WooCommerce Change Session Country/State for checkout. | |
* | |
* @package pekky-wc-change-session-country-state | |
* @author Precious Omonzejele (CodeXplorer π€Ύπ½ββοΈπ₯π¦π€‘) | |
* | |
* @wordpress-plugin | |
* Plugin Name: WooCommerce Change Session Country/State for checkout | |
* Plugin URI: https://gist.github.com/Preciousomonze/58a1e85557a74a4094f245adb0f8786f | |
* Description: This MU-Plugin helps preset or preselect a country/state based on checkout. It can be shipping or billing. All you need to do is call the function 'CLASS::process_selected_country_or_state_etc('NG')' to set the value. | |
* Author: Precious Omonzejele (CodeXplorer π€Ύπ½ββοΈπ₯π¦π€‘) | |
* Author URI: https://codexplorer.ninja | |
* Version: 1.0.0 | |
* Requires at least: 5.0 | |
* Tested up to: 5.9 | |
* WC requires at least: 5.0 | |
* WC tested up to: 6.0 | |
*/ | |
class Pekky_WC_Change_Session_Country_State { | |
/** | |
* Default country. (e.g: NG for Nigeria). | |
* | |
* @var string | |
*/ | |
protected static $wc_default_country = ''; | |
/** | |
* If the override/change should affect logged in Customers. | |
* | |
* @var bool | |
*/ | |
public static $affect_existing_customers = false; | |
/** | |
* Type. | |
* | |
* state or country. | |
* | |
* @var string | |
*/ | |
protected static $type = ''; | |
/** | |
* Boostrap and shoot. | |
*/ | |
public static function init() { | |
// Hook on checkout. | |
add_filter( 'woocommerce_before_checkout_form', array( __CLASS__, 'set_selected_country_or_state_etc' ), 10 ); | |
} | |
/** | |
* Check if woocommerce is active. | |
*/ | |
public function is_woocommerce_active() { | |
if ( class_exists( 'woocommerce' ) ) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
/** | |
* Processes Selected country or state. | |
* | |
* ETC is incase we later support more values like city. | |
* | |
* @param string $value Value should be the abbr of the country or state, e.g NG for Nigeria. | |
* @param string $type (optional) any valid type like country,state,town. only country and state supported for now. | |
*/ | |
public static function process_selected_country_or_state_etc( $value, $type = 'country' ) { | |
// If woocommerce isn't active or empty data, do nothing. | |
if ( !self::is_woocommerce_active() || empty( $value ) ) { | |
return; | |
} | |
if ( is_null( WC()->session ) ) { | |
WC()->session = new WC_Session_Handler(); | |
WC()->session->init(); | |
} | |
// Start storing. | |
$process_type = strtolower( $type ); | |
self::$type = $process_type; | |
/** | |
* Filters the session name for the selected country or state. | |
*/ | |
$session_name = apply_filters( 'pekky_wc_csc_selected_' . $process_type . '_session_name', 'pekky_selected_' . $process_type ); | |
WC()->session->set( $session_name, $value ); | |
} | |
/** | |
* Set selected country or state. | |
* | |
* ETC is incase we later support more values like city. | |
* | |
* @hooked | |
*/ | |
public static function set_selected_country_or_state_etc() { | |
// If we set affect_existing_customers to false and user already exists, don't override country. | |
if ( ! self::$affect_existing_customers && WC()->customer->get_is_paying_customer() ) { | |
return; | |
} | |
/** | |
* Filters the field type being changed. | |
*/ | |
$process_type = strtolower( apply_filters( 'pekky_wc_csc_field_type', self::$type ) ); | |
/** | |
* Filters the session name for the selected country or state. | |
*/ | |
$session_name = apply_filters( 'pekky_wc_csc_selected_' . $process_type . '_session_name', 'pekky_selected_' . $process_type ); | |
$selected = WC()->session->get( $session_name ); | |
if ( ! empty( $selected ) ) { | |
/** | |
* Filters the country type country. | |
* | |
* Values can only be billing or shipping. Default is shipping. | |
*/ | |
$country_state_etc_type = strtolower( apply_filters( 'pekky_wc_csc_country_state_etc_type', 'shipping' ) ); | |
// ..customer->set_billing_country. | |
WC()->customer->{"set_" . $country_state_etc_type . "_" . $process_type}( $selected ); | |
} | |
} | |
} | |
// Launch! | |
Pekky_WC_Change_Session_Country_State::init(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment