CCCPaste

LearnDash - Aelia Currency Switcher integrati...

set_hooks();
	}

	/**
	 * Returns the instance of the Aelia Currency Switcher.
	 *
	 * @return object|false The plugin instance, or false if the plugin is not
	 * active.
	 */
	protected function cs() {
		if(!is_object($GLOBALS['woocommerce-aelia-currencyswitcher'])) {
			return false;
		}
		return $GLOBALS['woocommerce-aelia-currencyswitcher'];
	}


  /**
   * Sets all actions and filters used by the plugin.
   */
	protected function set_hooks() {
		add_action('init', array($this, 'init'), 10);
	}

	/**
	 * Performs actions when all plugins have been loaded.
	 */
	public function init() {
		// Load the plugin's text domain, to allow localisation
		load_plugin_textdomain(self::$text_domain, false, dirname(plugin_basename(__FILE__)) . '/languages/');

		// We can only run our custom code if the Currency Switcher is active
		if($this->cs() !== false) {
			$this->pricing_manager = $this->cs()->currencyprices_manager();

			add_filter('wc_aelia_currencyswitcher_product_convert_callback', array($this, 'wc_aelia_currencyswitcher_product_convert_callback'), 10, 2);
			add_action('woocommerce_process_product_meta_' . self::PRODUCT_TYPE, array($this, 'woocommerce_process_product_meta'));
		}
	}

	/**
	 * Event handler fired when a Product is being saved. It processes and saves
	 * the Currency Prices associated with the Product.
	 *
	 * @param int post_id The ID of the Post (product) being saved.
	 */
	public function woocommerce_process_product_meta($post_id) {
		$this->pricing_manager->process_product_meta($post_id);
	}

	/**
	 * Alter the callbacks used by the Currency Switcher, replacing the callback
	 * for a "course" type with a custom one.
	 *
	 * @param callable convert_callback The original callback.
	 * @param WC_Product product The callback being converted.
	 * @return callable The callback that will convert the product's prices.
	 */
	public function wc_aelia_currencyswitcher_product_convert_callback($convert_callback, $product) {
		if($product->is_type(self::PRODUCT_TYPE)) {
			$convert_callback = array($this, 'convert_product_prices');
		}

		return $convert_callback;
	}

	/**
	 * Converts the prices of a "course" product.
	 *
	 * IMPORTANT
	 * This method assumes that a "course" is just another simple product. If that's not the case, the
	 * conversion may not work as expected. In that case, the best course of action would be for the
	 * authors of LearnDash to add native multi-currency support to their plugin.
	 *
	 * @param WC_Product product The product whose prices will be converted.
	 * @param string currency The target currency.
	 * @return WC_Product The product, with the prices converted to the target
	 * currency.
	 */
	public function convert_product_prices(WC_Product $product, $currency) {
		$product = $this->pricing_manager->convert_simple_product_prices($product, $currency);

		return $product;
	}
}

WC_LearnDash_Aelia_Currency_Switcher_Integration::instance();