I have a couple products at checkout that I need to be able to get all of the custom options that are selected for them through code.
Any help is much appreciated!
Re: Magento2 Get product custom options on product list page. $customOptions = $objectManager->get('Magento\Catalog\Model\Product\Option') ->getProductOptionCollection($_product); if (empty($customOptions)) { //check if product has custom options.
I will just give you an example of one product. Let's say that you know the Sku (for example, let it be "ABCDE") of your required product. So you will be able to get the ID of that product.
The code will be somewhat like:-
$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>';
}
I think this will let you get started.
Depending upon the type of the option in the variable "$optionType
", you need to call another nested "foreach
" loop. I have worked on text boxes, text fields, drop downs, but not on other types. So I suppose you need to do some more RnD by yourself.
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