Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Custom Options Programmatically in Magento

Tags:

php

magento

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!

like image 635
DaveC Avatar asked May 13 '10 18:05

DaveC


People also ask

How can I get custom option value in Magento 2?

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.


1 Answers

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.

like image 57
Knowledge Craving Avatar answered Oct 12 '22 10:10

Knowledge Craving