Advertisement
businessdad

WooCommerce Currency Switcher - Conversion of custom product

Aug 29th, 2016
667
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.32 KB | None | 0 0
  1. /**
  2.  * Example class showing how to use the filters provided by the Aelia Currency
  3.  * Switcher to implement a callback for the conversion of prices for custom
  4.  * products.
  5.  *
  6.  * @author Aelia <support@aelia.co>
  7.  * @link https://aelia.co
  8.  */
  9. class Currency_Switcher_Integration_Example {
  10.   public function __construct() {
  11.     $this->set_hooks();
  12.   }
  13.  
  14.   /**
  15.    * Sets the hooks required by the class.
  16.    */
  17.   protected function set_hooks() {
  18.     add_filter('wc_aelia_currencyswitcher_product_convert_callback', array($this, 'wc_aelia_currencyswitcher_product_convert_callback'), 10, 2);
  19.   }
  20.  
  21.   /**
  22.    * Converts the prices of a subscription product to the specified currency.
  23.    *
  24.    * @param WC_Product_MyCustomProduct product A custom product.
  25.    * @param string currency A currency code.
  26.    * @return WC_Product_MyCustomProduct The product with converted prices.
  27.    */
  28.   public function convert_my_custom_product_prices($product, $currency) {
  29.     /* Here you can perform any currency related operation you need. For example,
  30.      * if your product prices were loaded in base currency, this is the perfect
  31.      * place to replace them with the ones in the correct currency.
  32.      *
  33.      * If you designed your product class with built-in multi-currency support,
  34.      * and it automatically loads its prices in the appropriate currency, you
  35.      * won't have to do anything here. You will still need this callback, though,
  36.      * so that the Currency Switcher will know that someone "took care" of the
  37.      * custom product types, and no warnings will be raised.
  38.      */
  39.    
  40.     return $product;
  41.   }  
  42.  
  43.   /**
  44.    * Intercepts the product conversion callbacks used by the Currency Switcher, adding
  45.    * new ones to handle custom products.
  46.    *
  47.    * @param callable $callback The original callback passed to the hook.
  48.    * @param WC_Product The product to examine.
  49.    * @return callable
  50.    */
  51.   public function wc_aelia_currencyswitcher_product_convert_callback($callback, $product) {
  52.     // This array maps a list of classes to a callback key. If your plugin uses
  53.     // namespaced classes, or if you are planning to introduce namespaced classes
  54.     // in the future, you can specify both the simple and the namespaced class
  55.     // name, as in the example below
  56.     $method_keys = array(
  57.       // If the product class is "WC_Product_MyCustomProduct", we will use our
  58.       // convert_my_custom_product_prices method to load its currency-specific
  59.       // prices, or to invoke the conversion
  60.       'WC_Product_MyCustomProduct' => 'convert_my_custom_product_prices',
  61.       // Namespaced class
  62.       '\My\Namespaced\Class\WC_Product_MyCustomProduct' => 'convert_my_custom_product_prices',
  63.     );
  64.  
  65.     // Determine the conversion method to use. We add a safeguard check, in case
  66.     // we specify the wrong callback, so that the site won't crash if the method
  67.     // doesn't exist
  68.     if(!empty($method_keys[get_class($product)])) {
  69.       // Get the callback method from the map, depending on the class
  70.       $callback_method = $method_keys[get_class($product)];
  71.       // If we have the method we specified in the map, replace the original
  72.       // callback with our own
  73.       if(method_exists($this, $callback_method)) {
  74.         $callback = array($this, $callback_method);
  75.       }      
  76.     }
  77.  
  78.     return $callback;
  79.   }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement