You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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!


Step 1: Hook Into the Right Magento Event

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.

Step 2: Create a Custom Module (Don’t Touch Core Files!)

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>
Step 3: Build the Observer Class (Where the Magic Happens)

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);
        }
    }
}
Step 4: Test the Solution
  1. Clear Magento Cache: Go to System > Cache Management and flush all cache types.
  2. Create a New Product: Navigate to Catalog > Manage Products > Add Product, fill in the basic product details, and save.
  3. Check Custom Options: After saving, go to the Custom Options tab—you’ll see your pre-defined options already added!
Key Tips & Notes
  • Customize the Options: Modify the $customOptions array 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 09:55:19