0

On my magento I have different store views and some simple products with customizable options. I also have some custom code that checks the label of these options:

$myvalue = 0;
$options = $item->getProductOptions();
foreach ($options['options'] as $option) {
  if($option['label'] == "mylabel") $myvalue = $option['value'];
}

On default store view everything works fine. My problem is the transations: I have translated the customizable option title via backend by switching scope on the product page. Now, my if condition no longer works when I'm not in default store view because the $option holds only the translated label.

Is there a way to get the default label instead of the translated one?

1 Answer 1

1

Try this method.

  1. Retrieve the default store view label.

    To get the default label of the customizable option, you need to load the product in the default store view context and retrieve the label from there.

     foreach ($defaultOptions['options'] as $option) {
       if ($option['label'] == 'mylabel') {
         $defaultLabel = $option['value'];
         break;
       }
     }
    

2.Retrieve the translated label for the current store view Next, you need to retrieve the translated label for the current store view.

 $myvalue = 0;
$options = $item->getProductOptions();
$translatedLabel = '';

foreach ($options['options'] as $option) {
  if ($option['label'] == 'mylabel') {
    $translatedLabel = $option['value'];
    break;
  }
}

3.Check and use the correct label.

Now that you have both the default label ($defaultLabel) and the translated label ($translatedLabel), you can modify your if condition to check if the translated label is empty.

if (!empty($translatedLabel)) {
  // Translated label is available
  $myvalue = $translatedLabel;
} else {
  // Use default label
  $myvalue = $defaultLabel;
}

In the updated code, if the $translatedLabel variable is not empty, it means a translation is available for the current store view, so it assigns the translated label to $myvalue. Otherwise, if the $translatedLabel is empty, it uses the default label stored in $defaultLabel.

Hope you this is useful to you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.