businessdad icon

WooCommerce - Replace the labels of variation options

businessdad | PRO | 09/05/16 03:09:28 PM UTC | 0 ⭐ | 966 👁️ | Never ⏰ | []
PHP |

1.33 KB

|

None

|

0 👍

/

0 👎

/**
 * Replaces the labels for a variation attribute, on the frontend, so that they
 * show amounts in the active currency.
 *
 * @param string $option_name The original option name.
 * @return string The new option name, with the amount in the active currency.
 * @author Aelia <[email protected]>
 * @link https://aelia.co
 */
add_filter('woocommerce_variation_option_name', function($option_name) {
  $amount_to_convert = null;
  // Check if the label contains one of the "supported" amounts
  switch($option_name) {
    case '£10':
      $amount_to_convert = 10;
      break;
    case '£20':
      $amount_to_convert = 20;
      break;
    case '£50':
      $amount_to_convert = 50;
      break;
    case '£100':
      $amount_to_convert = 100;
      break;
  }
  
  // If an amount was determined, convert it to the active currency, format it
  // and use the new amount as the label for the option
  if(!empty($amount_to_convert)) {
    $active_currency = get_woocommerce_currency();
    // Generate the new label by calling the conversion functions provided by
    // the WooCommerce Currency Switcher
    $option_name =   wc_price(apply_filters('wc_aelia_cs_convert', $price, 'GNP', $active_currency),
                             array('currency' => $active_currency));
  }
 
  return $option_name;
}, 10, 1);

Comments