businessdad icon

WooCommerce Currency Switcher - Conversion of custom product

businessdad | PRO | 08/29/16 09:13:33 AM UTC | 0 ⭐ | 1535 👁️ | Never ⏰ | []
PHP |

3.32 KB

|

None

|

0 👍

/

0 👎

/**
 * Example class showing how to use the filters provided by the Aelia Currency
 * Switcher to implement a callback for the conversion of prices for custom
 * products.
 *
 * @author Aelia <[email protected]>
 * @link https://aelia.co
 */
class Currency_Switcher_Integration_Example {
  public function __construct() {
    $this->set_hooks();
  }
  
  /**
   * Sets the hooks required by the class.
   */
  protected function set_hooks() {
    add_filter('wc_aelia_currencyswitcher_product_convert_callback', array($this, 'wc_aelia_currencyswitcher_product_convert_callback'), 10, 2);
  }
  
  /**
   * Converts the prices of a subscription product to the specified currency.
   *
   * @param WC_Product_MyCustomProduct product A custom product.
   * @param string currency A currency code.
   * @return WC_Product_MyCustomProduct The product with converted prices.
   */
  public function convert_my_custom_product_prices($product, $currency) {
    /* Here you can perform any currency related operation you need. For example,
     * if your product prices were loaded in base currency, this is the perfect
     * place to replace them with the ones in the correct currency.
     *
     * If you designed your product class with built-in multi-currency support,
     * and it automatically loads its prices in the appropriate currency, you
     * won't have to do anything here. You will still need this callback, though,
     * so that the Currency Switcher will know that someone "took care" of the
     * custom product types, and no warnings will be raised.
     */
    
    return $product;
  }  
 
  /**
   * Intercepts the product conversion callbacks used by the Currency Switcher, adding
   * new ones to handle custom products.
   *
   * @param callable $callback The original callback passed to the hook.
   * @param WC_Product The product to examine.
   * @return callable
   */
  public function wc_aelia_currencyswitcher_product_convert_callback($callback, $product) {
    // This array maps a list of classes to a callback key. If your plugin uses
    // namespaced classes, or if you are planning to introduce namespaced classes
    // in the future, you can specify both the simple and the namespaced class
    // name, as in the example below
    $method_keys = array(
      // If the product class is "WC_Product_MyCustomProduct", we will use our
      // convert_my_custom_product_prices method to load its currency-specific
      // prices, or to invoke the conversion
      'WC_Product_MyCustomProduct' => 'convert_my_custom_product_prices',
      // Namespaced class
      '\My\Namespaced\Class\WC_Product_MyCustomProduct' => 'convert_my_custom_product_prices',
    );
 
    // Determine the conversion method to use. We add a safeguard check, in case
    // we specify the wrong callback, so that the site won't crash if the method
    // doesn't exist
    if(!empty($method_keys[get_class($product)])) {
      // Get the callback method from the map, depending on the class
      $callback_method = $method_keys[get_class($product)];
      // If we have the method we specified in the map, replace the original
      // callback with our own
      if(method_exists($this, $callback_method)) {
        $callback = array($this, $callback_method);
      }      
    }
 
    return $callback;
  }
}

Comments