businessdad

LearnDash - Aelia Currency Switcher integration (PROOF OF CONCEPT)

Jul 23rd, 2026
32,152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.25 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: LearnDash - Integration with Aelia Currency Switcher (PROOF OF CONCEPT)
  4. Plugin URI: https://aelia.co/
  5. Description: This plugin tries to pass the LearnDash "course" product to the Aelia Currency Switcher for conversion.
  6. Version: 1.0.0
  7. Author: Aelia
  8. Author URI: https://aelia.co
  9. License: GPLv3
  10. */
  11.  
  12. /**
  13.  * GPL DISCLAIMER
  14.  * Except when otherwise stated in writing the copyright holders and/or other parties provide the program "as is"
  15.  * without warranty of any kind, either expressed or implied, including, but not limited to, the implied warranties of
  16.  * merchantability and fitness for a particular purpose. The entire risk as to the quality and performance of the program
  17.  * is with you. should the program prove defective, you assume the cost of all necessary servicing, repair or correction.
  18.  */
  19.  
  20. /**
  21.  * == Changelog ==
  22.  *
  23.  * = 1.0.0 =
  24.  * First release, 04 April 2022.
  25.  */
  26.  
  27. /**
  28.  * Experimental integration with LearnDash. This plugin tries to pass the LearnDash "course" product to
  29.  * the Aelia Currency Switcher for conversion.
  30.  *
  31.  * IMPORTANT
  32.  * This plugin is to be intended as the proof of concept to show the LeadnDash developer how they could easily
  33.  * implement a basic integration  with the Aelia Currency Switcher. It's not meant to be a guarnatee of compatibility,
  34.  * and we cannot provide any support for it.
  35.  */
  36. class WC_LearnDash_Aelia_Currency_Switcher_Integration {
  37.     /**
  38.      * The singleton instance of this class.
  39.      *
  40.      * @var WC_LearnDash_Aelia_Currency_Switcher_Integration
  41.      */
  42.     protected static $_instance;
  43.  
  44.     /**
  45.      * The product type used by LearnDash.
  46.      *
  47.      * @var string
  48.      */
  49.     const PRODUCT_TYPE = 'course';
  50.  
  51.     /**
  52.      * The text domain used by the plugin.
  53.      *
  54.      * @var string
  55.      */
  56.     protected static $text_domain = 'wc-aelia-learndash-integration';
  57.  
  58.     /**
  59.      * The instance of the Pricing Manager used by the Aelia Currency Switcher.
  60.      *
  61.      * @var object
  62.      */
  63.     protected $pricing_manager;
  64.  
  65.     /**
  66.      * Returns the singleton instance of this class.
  67.      *
  68.      * @return WC_LearnDash_Aelia_Currency_Switcher_Integration
  69.      */
  70.     public static function instance(): WC_LearnDash_Aelia_Currency_Switcher_Integration {
  71.         return static::$_instance ?? static::$_instance = new static();
  72.     }
  73.  
  74.   /**
  75.      * Constructor.
  76.      */
  77.     public function __construct() {
  78.         $this->set_hooks();
  79.     }
  80.  
  81.     /**
  82.      * Returns the instance of the Aelia Currency Switcher.
  83.      *
  84.      * @return object|false The plugin instance, or false if the plugin is not
  85.      * active.
  86.      */
  87.     protected function cs() {
  88.         if(!is_object($GLOBALS['woocommerce-aelia-currencyswitcher'])) {
  89.             return false;
  90.         }
  91.         return $GLOBALS['woocommerce-aelia-currencyswitcher'];
  92.     }
  93.  
  94.  
  95.   /**
  96.    * Sets all actions and filters used by the plugin.
  97.    */
  98.     protected function set_hooks() {
  99.         add_action('init', array($this, 'init'), 10);
  100.     }
  101.  
  102.     /**
  103.      * Performs actions when all plugins have been loaded.
  104.      */
  105.     public function init() {
  106.         // Load the plugin's text domain, to allow localisation
  107.         load_plugin_textdomain(self::$text_domain, false, dirname(plugin_basename(__FILE__)) . '/languages/');
  108.  
  109.         // We can only run our custom code if the Currency Switcher is active
  110.         if($this->cs() !== false) {
  111.             $this->pricing_manager = $this->cs()->currencyprices_manager();
  112.  
  113.             add_filter('wc_aelia_currencyswitcher_product_convert_callback', array($this, 'wc_aelia_currencyswitcher_product_convert_callback'), 10, 2);
  114.             add_action('woocommerce_process_product_meta_' . self::PRODUCT_TYPE, array($this, 'woocommerce_process_product_meta'));
  115.         }
  116.     }
  117.  
  118.     /**
  119.      * Event handler fired when a Product is being saved. It processes and saves
  120.      * the Currency Prices associated with the Product.
  121.      *
  122.      * @param int post_id The ID of the Post (product) being saved.
  123.      */
  124.     public function woocommerce_process_product_meta($post_id) {
  125.         $this->pricing_manager->process_product_meta($post_id);
  126.     }
  127.  
  128.     /**
  129.      * Alter the callbacks used by the Currency Switcher, replacing the callback
  130.      * for a "course" type with a custom one.
  131.      *
  132.      * @param callable convert_callback The original callback.
  133.      * @param WC_Product product The callback being converted.
  134.      * @return callable The callback that will convert the product's prices.
  135.      */
  136.     public function wc_aelia_currencyswitcher_product_convert_callback($convert_callback, $product) {
  137.         if($product->is_type(self::PRODUCT_TYPE)) {
  138.             $convert_callback = array($this, 'convert_product_prices');
  139.         }
  140.  
  141.         return $convert_callback;
  142.     }
  143.  
  144.     /**
  145.      * Converts the prices of a "course" product.
  146.      *
  147.      * IMPORTANT
  148.      * This method assumes that a "course" is just another simple product. If that's not the case, the
  149.      * conversion may not work as expected. In that case, the best course of action would be for the
  150.      * authors of LearnDash to add native multi-currency support to their plugin.
  151.      *
  152.      * @param WC_Product product The product whose prices will be converted.
  153.      * @param string currency The target currency.
  154.      * @return WC_Product The product, with the prices converted to the target
  155.      * currency.
  156.      */
  157.     public function convert_product_prices(WC_Product $product, $currency) {
  158.         $product = $this->pricing_manager->convert_simple_product_prices($product, $currency);
  159.  
  160.         return $product;
  161.     }
  162. }
  163.  
  164. WC_LearnDash_Aelia_Currency_Switcher_Integration::instance();
Advertisement
Add Comment
Please, Sign In to add comment