1

how I can check in list.phtml if a product have custom options and if Required field is selected?

enter image description here

2
$hasRequired = false;
$options = $_product->getOptions();
if (is_array($options)) {
    foreach ($_product->getOptions() as $option) {
        if ($option->getIsRequire()) {
            $hasRequired = true;
            break;
        }
    }
}

now check the value of $hasRequired;

($_product - is the product instance for which you are checking)

4
  • thank you very much for your answer, I really appreciate your help.
    – Robert
    Mar 26 '20 at 9:35
  • can you please check that code I put in the list.phtml and I have this error on frontend: Warning: Invalid argument supplied for foreach()
    – Robert
    Mar 26 '20 at 9:57
  • maybe check what ($_product->getOptions() returns. maybe it does not return an array. I've updated the answer
    – Marius
    Mar 26 '20 at 10:16
  • I just put the code in the list.phtml is magento 2.3.4 version
    – Robert
    Mar 26 '20 at 10:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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