Advertisement
businessdad

Currency Switcher - WooCommerce Blocks Checkout Integration

May 29th, 2020
1,642
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.30 KB | None | 0 0
  1. /**
  2.  * Aelia Currency Switcher, Prices by Country, Tax Display by Country
  3.  * --
  4.  * Integration with the "cart" and "checkout" blocks provided by WooCommerce Blocks.
  5.  *
  6.  * THIS CODE IS PROVIDED AS A PROOF OF CONCEPT. Please make sure to read all the documentation,
  7.  * below, before using it.
  8.  *
  9.  * GPL DISCLAIMER
  10.  * THE USE OF THIS CODE IS AS YOUR OWN RISK.
  11.  * This code is provided free of charge and comes without any warranty, implied or explicit, to the extent permitted
  12.  * by applicable law. Except when otherwise stated in writing the copyright holders and/or other parties provide the
  13.  * program "as is" without warranty of any kind, either expressed or implied, including, but not limited to, the implied
  14.  * warranties of merchantability and fitness for a particular purpose. The entire risk as to the quality and performance
  15.  * of the program is with you. Should the program prove defective, you assume the cost of all necessary servicing, repair
  16.  * or correction.
  17.  *
  18.  * Need a consultation, or assistance to customise this code? Find us on Codeable: https://aelia.co/hire_us
  19.  *
  20.  * BACKGROUND
  21.  * On a "plain" WooCommerce setup, changing fields like billing and shipping country on the
  22.  * cart and checkout page triggers a call to the site to refresh the cart contents. The Aelia
  23.  * plugins intercept this call, check which country was selected and set customer's country and
  24.  * currency accordingly.
  25.  *
  26.  * For some reason, the WooCommerce Blocks plugin follows a different approach. Rather than the
  27.  * standard call used by WooCommerce, that plugin uses a custom REST API to update the shipping
  28.  * details. The Aelia plugins are not aware of that call, therefore they can't "see" that the
  29.  * address changed.
  30.  *
  31.  * Unfortunately, the WooCommerce Blocks plugin is still quite rigid and doesn't offer events
  32.  * that can be intercepted to detect the change. Due to that, a more "rustic" approach is needed,
  33.  * to listen for the specific REST calls it makes.
  34.  *
  35.  * CAVEATS
  36.  * - This code listens for the "shipping country" change. When this code was written (29 May 2020),
  37.  *   the WooCommerce Blocks plugin didn't seem to trigger any event when the BILLING country.
  38.  * - This code doesn't check if the Aelia plugins are supposed to use the shipping country or the
  39.  *   billing country. Since the WooCommerce Blocks plugin only triggers a call on "shipping address change",
  40.  *   this code takes such country and uses it to set customer's country.
  41.  *
  42.  * IMPLEMENTATION
  43.  * For this code snippet to work, you will probably have to wrap it into a mini-plugin. You can
  44.  * do that as follows:
  45.  *
  46.  * 1. Create a file called your-plugin-name file in the new subfolder.
  47.  * 2. Add a plugin header to the file: https://developer.wordpress.org/plugins/plugin-basics/header-requirements/.
  48.  *    You can just set it to "Plugin Name: YOUR PLUGIN NAME".
  49.  * 3. Add your custom code below the plugin header.
  50.  * 4. Save the file.
  51.  * 5. Create a folder called wp-content/plugins/your-plugin-name on your site (e.g. via FTP).
  52.  * 6. Enter the new folder.
  53.  * 7. Upload the file you just created.
  54.  * 8. Go to WordPress > Plugins to enable the custom plugin.
  55.  *
  56.  * By doing this, the custom code will be loaded together with the other plugin. That will be much earlier than
  57.  * file "functions.php", and it will ensure that the custom code will be loaded soon enough.
  58.  */
  59.  
  60. add_filter('woocommerce_rest_is_request_to_rest_api', function($is_request_to_rest_api) {
  61.   if($is_request_to_rest_api) {
  62.         // Extract the path to the REST API
  63.     $rest_api_path = parse_url(esc_url_raw(wp_unslash($_SERVER['REQUEST_URI'])), PHP_URL_PATH);
  64.  
  65.         // Check if the path matches the "update shipping" call made by the WooCommerce Blocks plugin
  66.     if(substr_compare($rest_api_path, '/wc/store/cart/update-shipping', -strlen('/wc/store/cart/update-shipping')) === 0) {
  67.             // Extract and decode the posted data
  68.             // @see WC_API_Server::get_raw_data()
  69.       $raw_body = file_get_contents('php://input');
  70.       $body = json_decode($raw_body, true);
  71.  
  72.             // If a country was posted, simulate a "country selection". This will allow
  73.             // the Aelia plugins to pick it up automatically
  74.       if(!empty($body['country'])) {
  75.         $_POST['aelia_customer_country'] = $body['country'];
  76.       }
  77.     }
  78.   }
  79.  
  80.   return $is_request_to_rest_api;
  81. }, 1, 3);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement