Advertisement
businessdad

WooCommerce - Replace the labels of variation options

Sep 5th, 2016
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.33 KB | None | 0 0
  1. /**
  2.  * Replaces the labels for a variation attribute, on the frontend, so that they
  3.  * show amounts in the active currency.
  4.  *
  5.  * @param string $option_name The original option name.
  6.  * @return string The new option name, with the amount in the active currency.
  7.  * @author Aelia <support@aelia.co>
  8.  * @link https://aelia.co
  9.  */
  10. add_filter('woocommerce_variation_option_name', function($option_name) {
  11.   $amount_to_convert = null;
  12.   // Check if the label contains one of the "supported" amounts
  13.   switch($option_name) {
  14.     case '£10':
  15.       $amount_to_convert = 10;
  16.       break;
  17.     case '£20':
  18.       $amount_to_convert = 20;
  19.       break;
  20.     case '£50':
  21.       $amount_to_convert = 50;
  22.       break;
  23.     case '£100':
  24.       $amount_to_convert = 100;
  25.       break;
  26.   }
  27.  
  28.   // If an amount was determined, convert it to the active currency, format it
  29.   // and use the new amount as the label for the option
  30.   if(!empty($amount_to_convert)) {
  31.     $active_currency = get_woocommerce_currency();
  32.     // Generate the new label by calling the conversion functions provided by
  33.     // the WooCommerce Currency Switcher
  34.     $option_name =   wc_price(apply_filters('wc_aelia_cs_convert', $price, 'GNP', $active_currency),
  35.                              array('currency' => $active_currency));
  36.   }
  37.  
  38.   return $option_name;
  39. }, 10, 1);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement