Magento 1.9后台添加产品时程序化添加自定义选项实现问询
Hey there, I’ve dealt with this exact scenario in Magento 1.9 countless times—let’s break down a foolproof, step-by-step solution that automatically adds custom options when you create a new product in the admin. No more hunting for where to place code or guessing how Magento’s hooks work!
We’ll use the catalog_product_save_before event—it triggers right before a product is saved to the database, which is perfect for injecting our custom options. This ensures the options are added as part of the product creation process.
Always use a custom module instead of editing core Magento files—this keeps your changes safe during updates. Here’s how to set it up:
2.1 Enable the Module
Create the file app/etc/modules/YourCompany_CustomOption.xml with this code:
<?xml version="1.0"?> <config> <modules> <YourCompany_CustomOption> <active>true</active> <codePool>local</codePool> </YourCompany_CustomOption> </modules> </config>
(Replace YourCompany with your actual company/namespace name.)
2.2 Configure the Module & Event Observer
Create the module’s config file at app/code/local/YourCompany/CustomOption/etc/config.xml:
<?xml version="1.0"?> <config> <modules> <YourCompany_CustomOption> <version>1.0.0</version> </YourCompany_CustomOption> </modules> <global> <models> <customoption> <class>YourCompany_CustomOption_Model</class> </customoption> </models> <events> <catalog_product_save_before> <observers> <customoption_add_custom_options> <type>singleton</type> <class>customoption/observer</class> <method>addCustomOptions</method> </customoption_add_custom_options> </observers> </catalog_product_save_before> </events> </global> </config>
Create the observer file at app/code/local/YourCompany/CustomOption/Model/Observer.php—this is where we’ll define and add the custom options:
<?php class YourCompany_CustomOption_Model_Observer { public function addCustomOptions(Varien_Event_Observer $observer) { $product = $observer->getProduct(); // Only add options for NEW products (remove this check to add to existing products too) if (!$product->isObjectNew()) { return; } // Define your custom options here—tweak this to match your needs $customOptions = [ [ 'title' => 'Custom Engraving', 'type' => 'field', 'is_require' => 0, 'sort_order' => 1, 'price' => 5.00, 'price_type' => 'fixed', 'sku' => 'ENGRAVE-001', ], [ 'title' => 'Gift Wrapping', 'type' => 'drop_down', 'is_require' => 0, 'sort_order' => 2, 'values' => [ [ 'title' => 'Basic Wrapping ($2)', 'price' => 2.00, 'price_type' => 'fixed', 'sku' => 'GIFT-BASIC', 'sort_order' => 1, ], [ 'title' => 'Premium Wrapping ($5)', 'price' => 5.00, 'price_type' => 'fixed', 'sku' => 'GIFT-PREMIUM', 'sort_order' => 2, ], ], ], ]; // Loop through options and attach them to the product foreach ($customOptions as $optionData) { $option = Mage::getModel('catalog/product_option') ->setProductId($product->getId()) ->setStoreId($product->getStoreId()) ->addData($optionData); // Add option values if the option type requires them (e.g., drop-down, radio) if (isset($optionData['values']) && is_array($optionData['values'])) { foreach ($optionData['values'] as $valueData) { $value = Mage::getModel('catalog/product_option_value') ->setProductOption($option) ->addData($valueData); $option->addOptionValue($value); } } $product->addOption($option); } } }
- Clear Magento Cache: Go to
System > Cache Managementand flush all cache types. - Create a New Product: Navigate to
Catalog > Manage Products > Add Product, fill in the basic product details, and save. - Check Custom Options: After saving, go to the
Custom Optionstab—you’ll see your pre-defined options already added!
- Customize the Options: Modify the
$customOptionsarray to match your needs. Supported option types include:field,area,file,drop_down,radio,checkbox,multiple. - Avoid Duplicates: If you want to add options to existing products, remove the
isObjectNew()check—but add a validation step first to avoid adding duplicate options (e.g., check if an option with the same title already exists for the product). - Debugging: If something breaks, check
var/log/system.log—Magento logs observer errors here.
内容的提问来源于stack exchange,提问作者Prashant Patil

