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

Magento 2中如何为所有简单产品创建自定义选项?

Absolutely, there are several solid ways to add custom options to all simple products in Magento 2—let’s walk through the most practical methods depending on your technical comfort level:

1. Admin Bulk Update (No-Code Approach)

If you’re not comfortable with code, Magento’s built-in bulk tools get the job done quickly:

  • Head to Catalog > Products in your admin panel.
  • Use the filter to narrow down to only simple products (set Product Type to Simple Product).
  • Check the box at the top of the product list to select all matching items, or pick specific ones.
  • Click the Actions dropdown and choose Update Attributes.
  • In the popup, scroll to the Custom Options section (you may need to expand it) and click Add New Option.
  • Configure your option: set the type (Dropdown, Text Field, etc.), title, price, price type, and whether it’s required.
  • Hit Save to apply the option to all selected simple products.

Note: This works best for smaller product catalogs—if you have thousands of products, it might time out.

2. Data Patch (Developer-Friendly, Scalable)

For larger catalogs or deployable, maintainable changes, a data patch is the way to go. Here’s a sample implementation:

Create a file Vendor/Module/Setup/Patch/Data/AddCustomOptionsToSimpleProducts.php:

<?php
namespace Vendor\Module\Setup\Patch\Data;

use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\Product\Option;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;

class AddCustomOptionsToSimpleProducts implements DataPatchInterface
{
    private $productCollectionFactory;
    private $optionFactory;
    private $moduleDataSetup;

    public function __construct(
        CollectionFactory $productCollectionFactory,
        OptionFactory $optionFactory,
        ModuleDataSetupInterface $moduleDataSetup
    ) {
        $this->productCollectionFactory = $productCollectionFactory;
        $this->optionFactory = $optionFactory;
        $this->moduleDataSetup = $moduleDataSetup;
    }

    public function apply()
    {
        $this->moduleDataSetup->startSetup();

        // Fetch all simple products
        $products = $this->productCollectionFactory->create()
            ->addAttributeToFilter('type_id', 'simple');

        foreach ($products as $product) {
            // Skip if option already exists to avoid duplicates
            $optionExists = false;
            foreach ($product->getOptions() as $existingOption) {
                if ($existingOption->getTitle() === 'Custom Engraving') {
                    $optionExists = true;
                    break;
                }
            }

            if (!$optionExists) {
                $option = $this->optionFactory->create();
                $option->setProductId($product->getId())
                    ->setTitle('Custom Engraving')
                    ->setType('text') // Use 'drop_down' for multiple choice, 'checkbox' for multi-select, etc.
                    ->setIsRequire(0) // 1 = required, 0 = optional
                    ->setSortOrder(1)
                    ->setPrice(3.99)
                    ->setPriceType('fixed') // 'fixed' or 'percent'
                    ->setSku('engraving-option');

                $option->save();
            }
        }

        $this->moduleDataSetup->endSetup();
    }

    public static function getDependencies()
    {
        return [];
    }

    public function getAliases()
    {
        return [];
    }
}

After creating this file, run the setup upgrade command to apply the patch:

bin/magento setup:upgrade
3. Third-Party Extensions

If you want a no-code solution with more flexibility (like scheduling updates or advanced filtering), check the Magento Marketplace for bulk custom options extensions. Look for tools with high ratings and recent updates to ensure compatibility with your Magento version.

Key Notes Before You Start
  • Always test any method in a staging environment first to avoid breaking your live store.
  • Custom options affect pricing and cart behavior—double-check your price type (fixed/percent) and required status match your business needs.
  • For very large catalogs, data patches or CLI commands (a variation of the data patch approach) will perform better than admin bulk updates.

内容的提问来源于stack exchange,提问作者Haseem habeeb

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 08:21:29