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,
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.
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.
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();
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.
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