Skip to content

Instantly share code, notes, and snippets.

@deltafactory
Last active December 28, 2015 22:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deltafactory/7572147 to your computer and use it in GitHub Desktop.
Save deltafactory/7572147 to your computer and use it in GitHub Desktop.
Lazy load WooCommerce WC_Countries patch - proof of concept.
Index: classes/class-wc-countries.php
===================================================================
--- classes/class-wc-countries.php (revision 799848)
+++ classes/class-wc-countries.php (working copy)
@@ -1,4 +1,30 @@
<?php
+
+class WC_Countries_Loader {
+
+ private $instance;
+
+ private function get_instance() {
+ if ( empty( $this->instance ) ) {
+ $this->instance = new WC_Countries();
+ }
+
+ return $this->instance;
+ }
+
+ public function __get( $name ) {
+ return $this->get_instance()->{$name};
+ }
+
+ public function __set( $name, $value ) {
+ $this->get_instance()->{$name} = $value;
+ }
+
+ public function __call( $func, $args ) {
+ return call_user_func_array( array( $this->get_instance(), $func ), $args );
+ }
+}
+
/**
* WooCommerce countries
*
Index: woocommerce.php
===================================================================
--- woocommerce.php (revision 799848)
+++ woocommerce.php (working copy)
@@ -477,7 +477,7 @@
// Load class instances
$this->product_factory = new WC_Product_Factory(); // Product Factory to create new product instances
- $this->countries = new WC_Countries(); // Countries class
+ $this->countries = new WC_Countries_Loader(); // Countries class
$this->integrations = new WC_Integrations(); // Integrations class
// Classes/actions loaded for the frontend and for ajax requests
@@ -1201,7 +1201,6 @@
// Variables for JS scripts
$woocommerce_params = array(
- 'countries' => json_encode( $this->countries->get_allowed_country_states() ),
'plugin_url' => $this->plugin_url(),
'ajax_url' => $this->ajax_url(),
'ajax_loader_url' => apply_filters( 'woocommerce_ajax_loader_url', $this->plugin_url() . '/assets/images/ajax-loader@2x.gif' ),
@@ -1221,8 +1220,10 @@
'cart_redirect_after_add' => get_option( 'woocommerce_cart_redirect_after_add' )
);
- if ( is_checkout() || is_cart() )
+ if ( is_checkout() || is_cart() ) {
+ $woocommerce_params['countries'] = json_encode( $this->countries->get_allowed_country_states() );
$woocommerce_params['locale'] = json_encode( $this->countries->get_country_locale() );
+ }
wp_localize_script( 'woocommerce', 'woocommerce_params', apply_filters( 'woocommerce_params', $woocommerce_params ) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment