Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add custom options to dropdown via code in Magento

Tags:

magento

i had to add custom options automatically when a product is added , the code works fine but i need to create a drop down menu with options and i dont know how to add options to the drop down created , my code is

public function Add_CustomOptions_Automatically($observer) {
    $product = $observer->getEvent()->getProduct();
    $save = false; if (!$product->getOptions()) $save = true;

    $optionData = array(
        'previous_group'    => 'text',
        'title'             => 'Size',
        'type'              => 'drop_down',
        'is_require'        => 0,
        'sort_order'        => 0,
        'price'             => 0,
        'price_type'        => 'fixed');    


    if($save):
        $product->setHasOptions(1)->save();
        $option = Mage::getModel('catalog/product_option')
                    ->setProductId($product->getId())
                    ->setStoreId($product->getStoreId())           
                    ->addData($optionData);

        $option->save();
        $product->addOption($option);
    endif;
}

}

I've created 'type' => 'drop_down' but how can I add options? I have no idea how to add the options, and any help would be very much appreciated.

thanks,

like image 579
jarus Avatar asked Aug 02 '10 09:08

jarus


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.

How do I create a dropdown in Magento 2?

There are two ways to create the drop down attribute in Magento. 1 -> Manually : Go To admin panel, Store -> Attributes -> Product . 2 -> Programmatically . Now In this file, create an Array of the attributes that you want to add as below.


2 Answers

You can supply an array of values to the option array, this will then be added to the option. as below :-)

$product = Mage::getModel('catalog/product');
$product->load(200); // product id here

$opt = array(
    'is_delete'         => 0,
    'is_require'        => false,
    'previous_group'    => '',
    'title'             => 'New Example Option',
    'type'              => 'drop_down',
    'price_type'        => 'fixed',
    'price'             => '20.0000',
    'sort_order'        => 0,
    /** array of values for this option **/
    'values'            => array(
        array(
            'is_delete'     => 0,
            'title'         => 'Option One Here',
            'price_type'    => 'fixed',
            'price'         => 999,
            'sku'           => 'test-sku-here',
            'option_type_id'=> -1,
        ),
        array(
            'is_delete'     => 0,
            'title'         => 'Another Option',
            'price_type'    => 'fixed',
            'price'         => 999,
            'sku'           => 'another-sku-here',
            'option_type_id'=> -1,
    )),
);

$option = Mage::getModel('catalog/product_option')
    ->setProduct($product)
    ->addOption($opt)
    ->saveOptions();
like image 140
Webdevnetwork Avatar answered Sep 27 '22 18:09

Webdevnetwork


public function Add_CustomOptions_Automatically($observer) {
    $product = $observer->getEvent()->getProduct();

    $optionData = array(
        'previous_group'    => 'text',
        'title'             => 'Size',
        'type'              => 'drop_down',
        'is_require'        => 0,
        'sort_order'        => 0,
        'price'             => 0,
        'price_type'        => 'fixed');

    if(!(bool)$product->getOptions()):
        $product->setHasOptions(true)->save();
        $option = Mage::getModel('catalog/product_option')
                    ->setProductId($product->getId())
                    ->setStoreId($product->getStoreId())           
                    ->addData($optionData);

        // Answer starts here
        $value = Mage::getModel('catalog/product_option_value');
        $value->setOption($option)
              ->setTitle('Hello world!');
        $option->addValue($value);
        // Answer ends here

        $option->save();
        $product->addOption($option);
    endif;
}

As you can see you are adding a Mage_Catalog_Model_Product_Option_Value with a title and associating it with the $option you created. It can also have an SKU, price and sort order. Create as many values as you want in this way.

like image 23
clockworkgeek Avatar answered Sep 27 '22 17:09

clockworkgeek