Advertisement
businessdad

WooCommerce - Replace product prices by ID and currency

Nov 16th, 2017
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.29 KB | None | 0 0
  1. /**
  2.  * Replaces regular and/or sale prices for specific product IDs, depending on the currency.
  3.  *
  4.  * HOW TO USE THIS CODE
  5.  * Simply add the code to the bottom of your theme's functions.php file, and it
  6.  * will run automatically. For more information: https://www.skyverge.com/blog/add-custom-code-to-wordpress/
  7.  *
  8.  * GPL DISCLAIMER
  9.  * Because this code program is free of charge, there is no warranty for it, to the extent permitted by applicable law.
  10.  * Except when otherwise stated in writing the copyright holders and/or other parties provide the program "as is"
  11.  * without warranty of any kind, either expressed or implied, including, but not limited to, the implied warranties of
  12.  * merchantability and fitness for a particular purpose. The entire risk as to the quality and performance of the program
  13.  * is with you. should the program prove defective, you assume the cost of all necessary servicing, repair or correction.
  14.  *
  15.  * Need a consultation, or assistance to customise this code? Find us on Codeable: https://aelia.co/hire_us
  16.  *
  17.  * @param float converted_price
  18.  * @param float original_price
  19.  * @param string from_currency
  20.  * @param string to_currency
  21.  * @param int price_decimals
  22.  * @param WC_Product product
  23.  * @param string price_type
  24.  * @return string
  25.  */
  26. add_filter('wc_aelia_cs_convert_product_price', function($converted_price, $original_price, $from_currency, $to_currency, $price_decimals, $product, $price_type) {
  27.   // Store custom product prices
  28.   $product_prices = array(
  29.     // 3578 is a product ID
  30.     3578 => array(
  31.       'USD' => array(
  32.         'regular_price' => 349,
  33.         //'sale_price' => 99,
  34.       ),
  35.       'EUR' => array(
  36.         'regular_price' => 340,
  37.         //'sale_price' => 79,
  38.       ),
  39.       'GBP' => array(
  40.         'regular_price' => 299,
  41.         //'sale_price' => 79,
  42.       ),
  43.       'AUD' => array(
  44.         'regular_price' => 490,
  45.         //'sale_price' => 79,
  46.       ),
  47.     ),
  48.   );
  49.   // If a price is set for the combination of product ID, currency and price type,
  50.   // take it and replace the result of the conversion
  51.   if(isset($product_prices[$product->get_id()][$to_currency][$price_type])) {
  52.     $converted_price = $product_prices[$product->get_id()][get_woocommerce_currency()][$price_type];
  53.   }
  54.  
  55.   return $converted_price;
  56. }, 10, 7);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement