Magento – custom option for all product

custom-optionsproduct

i want to add custom option for all product say ring size:7,6,5,4,3

i want to show if for all product types with attribute set as ring , i tried adding as custom option but in this way we have to add for all products , any easy way too show it as drop down fro all product

Bascially it drop down and user have to select it

Best Answer

Hi you can do this by magento event observer.try trigger an event on catalog_product_prepare_save observer check product attribute set is ring

Study about Creating Custom Option on Product Save

On This function you need check your attribute set for product is ring and your custom option is exit on not

See some logic:

$exit=false;

    $options = $product->getOptions();
    if ($options){
        foreach ($options as $option) {
            $optionType = $option->getType();
        if ($optionType == 'drop_down' && $option->getTitle()=='Ring' ) :
            $exit=true;
            break;
        endif;

        }
    }

    if($exit==false):
    $newOption = array(
        'title' => "Ring",
        'type' => 'drop_down',
        'is_require' => 1,
        'sort_order' => 20,
        'values' => array(
            array(
                'title' => "7",
                'price' => 42.00,
                'price_type' => 'fixed',
                'sku' => "",
                'sort_order' => '1'
            ),
            array(
                'title' => "6",
                'price' => 50,
                'price_type' => 'percent',
                'sku' => "",
                'sort_order' => '2'
            )
        )
    );

    //don't use it like this because it has no effect
    //$product->setProductOptions($options);
    $product->setCanSaveCustomOptions(true);
    //use it this way. Populate `$product->getOptionInstance()` directly
    $product->getOptionInstance()->addOption($newOption);
    //don't forget to state that the product has custom options
    $product->setHasOptions(true);

    endif;
Related Topic