On the cart page there's the following foreach loop:
foreach($this->getItems() as $_item) {
}
I need to get the product options for these items, I've tried a few methods but I'm unable to retrieve the results I need.
I've tried:
foreach($this->getItems() as $_item) {
print_r($_item->getProductOptions());
}
And:
foreach($this->getItems() as $_item) {
print_r($_item->getOptionList());
}
Are there any other functions I could use?
The custom option is a native feature of Magento and you can add Custom option from Admin side in product Add/edit section.
In order to get attribute option id by the label, it is required to use \Magento\Catalog\Model\Product Factory class in the construct. A huge variety of attribute types are available such as dropdown, visual swatch, multi-select, etc wherein you can add multiple options in a single attribute.
When you add custom options to a product in Magento 2, you give your customers the opportunity to choose product options as to their needs without relying on the product attributes. Customizable options allow you to categorize products, create cart price rules and dynamic categories rules.
Try using:
$_item->getProduct()->getTypeInstance(true)->getOrderOptions($_item->getProduct());
This might get you started in the right direction...
$productSku = "ABCDE";
$product = Mage::getModel('catalog/product');
$productId = $product->getIdBySku( $productSku );
$product->load($productId);
/**
* In Magento Models or database schema level, the product's Custom Options are
* executed & maintained as only "options". So, when checking whether any product has
* Custom Options or not, we should check by using this method "hasOptions()" only.
*/
if($product->hasOptions()) {
echo '<pre>';
foreach ($product->getOptions() as $o) {
$optionType = $o->getType();
echo 'Type = '.$optionType;
if ($optionType == 'drop_down') {
$values = $o->getValues();
foreach ($values as $k => $v) {
print_r($v);
}
}
else {
print_r($o);
}
}
echo '</pre>';
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With