/** * YITH Bundles - Aelia Currency Switcher Integration (PROTOTYPE) * The following code is a prototype for the development of an integration between the YITH Bundles * plugin and the Aelia Currency Switcher. * * IMPORTANT * This code should only be used when the Aelia Currency Switcher is active. Please remember to disable * or remove it if you deactivate or uninstall our plugin. * * DISCLAIMER * Aelia and any member of its staff are not responsible for any data loss or damage incurred * when using the code, which you can use at your own risk. * * GPL DISCLAIMER * Because this code program is free of charge, there is no warranty for it, to the extent permitted by applicable law. * Except when otherwise stated in writing the copyright holders and/or other parties provide the program "as is" * without warranty of any kind, either expressed or implied, including, but not limited to, the implied warranties of * merchantability and fitness for a particular purpose. The entire risk as to the quality and performance of the program * is with you. should the program prove defective, you assume the cost of all necessary servicing, repair or correction. * * Need a consultation, or assistance to customise this code? Find us on Codeable: https://aelia.co/hire_us */ namespace Aelia\WC\CurrencySwitcher\YITH_Bundles; if(!defined('ABSPATH')) exit; // Exit if accessed directly use \WC_Aelia_CurrencySwitcher; use \WC_Aelia_CurrencyPrices_Manager; use \WC_Product; use \WC_Product_Yith_Bundle; /** * Implements support for YITH Bundles plugin. */ class YITH_Bundles_Integration { protected static $_instance; // @var WC_Aelia_CurrencyPrices_Manager The object that handles Currency Prices for the Products. protected static $_currencyprices_manager; /** * Returns the singleton instance of this class * * @return YITH_Bundles_Integration */ public static function instance() { if(empty(self::$_instance)) { self::$_instance = new static(); } return self::$_instance; } /** * Returns the instance of the currency prices manager class. * * @return WC_Aelia_CurrencyPrices_Manager */ protected static function currencyprices_manager() { if(empty(self::$_currencyprices_manager)) { self::$_currencyprices_manager = \WC_Aelia_CurrencyPrices_Manager::instance(); } return self::$_currencyprices_manager; } /** * Indicates if a bundle is priced on a per product basis. * * @param WC_Product product The bundle product to check. * @return bool */ protected function is_bundle_priced_individually($product) { return !empty($product->per_items_pricing); } /** * Class constructor */ public function __construct() { $this->set_hooks(); } /** * Set 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); add_action('woocommerce_process_product_meta_yith_bundle', array($this, 'woocommerce_process_product_meta_yith_bundle')); } /** * Callback to perform the conversion of bundle prices into selected currency. * * @param callable $convert_callback A callable, or null. * @param WC_Product The product to examine. * @return callable */ public function wc_aelia_currencyswitcher_product_convert_callback($convert_callback, $product) { $method_keys = array( 'WC_Product_Yith_Bundle' => 'yith_bundle', ); // Determine the conversion method to use $method_key = isset($method_keys[get_class($product)]) ? $method_keys[get_class($product)] : ''; $convert_method = 'convert_' . $method_key . '_product_prices'; if(!method_exists($this, $convert_method)) { return $convert_callback; } return array($this, $convert_method); } /** * Indicates if a product requires conversion. * * @param WC_Product product The product to process. * @param string currency The target currency for which product prices will * be requested. * @return bool */ protected function product_requires_conversion($product, $currency) { // If the product is already in the target currency, it doesn't require // conversion return empty($product->currency) || ($product->currency != $currency); } /** * Converts the prices of a bundle product to the specified currency. * * @param WC_Product_Yith_Bundle product A variable product. * @param string currency A currency code. * @return WC_Product_Yith_Bundle The product with converted prices. */ public function convert_yith_bundle_product_prices(WC_Product_Yith_Bundle $product, $currency) { if(!$this->product_requires_conversion($product, $currency)) { return $product; } if(!$this->is_bundle_priced_individually($product)) { $product = self::currencyprices_manager()->convert_simple_product_prices($product, $currency); } return $product; } /*** Manual pricing of bundles ***/ /** * Event handler fired when a bundle is being saved. It processes and * saves the Currency Prices associated with the bundle. * * @param int post_id The ID of the Post (bundle) being saved. */ public function woocommerce_process_product_meta_yith_bundle($post_id) { self::currencyprices_manager()->process_product_meta($post_id); } } YITH_Bundles_Integration::instance();