Advertisement
businessdad

Currency Switcher - Integration template for bookings

Nov 20th, 2017
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.88 KB | None | 0 0
  1. /**
  2.  * This code snippet is an example of how to implemnt a callback for the
  3.  * conversion of custom products of type "booking". Please note that this is just
  4.  * a skeleton, and no conversion is actually performed. The purpose of this code
  5.  * is to illustrate how to interact with the Currency Switcher to add custom
  6.  * conversion logic for custom product types.
  7.  *
  8.  * HOW TO USE THIS CODE
  9.  * Simply add the code to the bottom of your theme's functions.php file, and it
  10.  * will run automatically. For more information: https://www.skyverge.com/blog/add-custom-code-to-wordpress/
  11.  *
  12.  * GPL DISCLAIMER
  13.  * Because this code program is free of charge, there is no warranty for it, to the extent permitted by applicable law.
  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.  * Need a consultation, or assistance to customise this code? Find us on Codeable: https://aelia.co/hire_us
  20.  */
  21.  
  22. /**
  23.  * Intercepts the product conversion callbacks used by the Currency Switcher, adding
  24.  * new ones to handle custom products.
  25.  *
  26.  * @param callable $callback The original callback passed to the hook.
  27.  * @param WC_Product The product to examine.
  28.  * @return callable
  29.  */
  30. function my_custom_conversion_callback($callback, $product) {
  31.   if($product->is_type('booking')) {
  32.     $callback = 'my_custom_booking_conversion';
  33.   }
  34.   return $callback;
  35. }
  36.  
  37. add_filter('wc_aelia_currencyswitcher_product_convert_callback', array($this, 'my_custom_conversion_callback'), 10, 2);
  38.  
  39. /**
  40.  * Converts the prices of a booking product to the specified currency.
  41.  *
  42.  * @param WC_Product_MyCustomProduct product A custom product.
  43.  * @param string currency A currency code.
  44.  * @return WC_Product_MyCustomProduct The product with converted prices.
  45.  */
  46. function my_custom_booking_conversion($product, $currency) {
  47.   /* Here you can perform any currency related operation you need. For example,
  48.    * if your product prices were loaded in base currency, this is the perfect
  49.    * place to replace them with the ones in the correct currency.
  50.    *
  51.    * If you designed your product class with built-in multi-currency support,
  52.    * and it automatically loads its prices in the appropriate currency, you
  53.    * won't have to do anything here. You will still need this callback, though,
  54.    * so that the Currency Switcher will know that someone "took care" of the
  55.    * custom product types, and no warnings will be raised.
  56.    */
  57.  
  58.   // Ticket 6057 - Return booking products as they are
  59.   return $product;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement