Press ESC to close

How to Add a New Option in Bundle Item? – Magento 2

For Magento 2 merchants, the ability to Add a New Option in a Bundle Item can greatly enhance the flexibility of their product bundles. While Magento 1 included options like ‘Is Hidden,’ Magento 2 requires a custom approach to achieve similar functionality. By mastering the process of adding new options, store owners can introduce personalized product configurations, meet unique customer requirements, and stand out in a competitive e-commerce landscape. This guide provides step-by-step instructions to empower your store with this advanced feature.

How to Add a New Option in a Bundle Item in Magento 2

First, need to create a plugin to inject your option. For that, we will add the below code in di.xml to add a plugin for Magento\Bundle\Ui\DataProvider\Product\Form\Modifier\Composite.

<type name="Magento\Bundle\Ui\DataProvider\Product\Form\Modifier\Composite">
   <plugin name="cd_add_new_bundle_option" type="Namespace\Extension\Plugin\Bundle\Ui\DataProvider\Product\Form\Modifier\BundleOptions"/>
</type>

Now we will need to add the code below in the file BundleOptions.php which is located at ‘Codedecorator\Learn\Plugin\Bundle\Ui\DataProvider\Product\Form\Modifier to add the option in all existing options for render.

<?php
namespace Codedecorator\Learn\Plugin\Bundle\Ui\DataProvider\Product\Form\Modifier;

use Magento\Ui\Component\Form;
use Magento\Bundle\Model\Product\Type;
use Magento\Bundle\Ui\DataProvider\Product\Form\Modifier\BundlePanel;
use Magento\Catalog\Model\Locator\LocatorInterface;
use Magento\Framework\ObjectManagerInterface;
use Magento\Bundle\Api\ProductOptionRepositoryInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;

class BundleOptions extends \Magento\Bundle\Ui\DataProvider\Product\Form\Modifier\Composite
{
    protected $optionFactory;

    public function __construct(
        LocatorInterface $locator,
        ObjectManagerInterface $objectManager,
        ProductOptionRepositoryInterface $optionsRepository,
        ProductRepositoryInterface $productRepository,
        \Magento\Bundle\Model\OptionFactory $OptionFactory,
        array $modifiers = []
    )
    {
        parent::__construct($locator, $objectManager, $optionsRepository, $productRepository, $modifiers);
        $this->optionFactory = $OptionFactory;
    }

    public function afterModifyMeta(
        \Magento\Bundle\Ui\DataProvider\Product\Form\Modifier\Composite $subject,
        $meta
    )
    {
        $meta["bundle-items"]["children"]["bundle_options"]["children"]["record"]["children"]["product_bundle_container"]["children"]["option_info"]["children"]["is_hidden"] = [
            'arguments' => [
                'data' => [
                    'config' => [
                        'dataType' => Form\Element\DataType\Number::NAME,
                        'formElement' => Form\Element\Checkbox::NAME,
                        'componentType' => Form\Field::NAME,
                        'description' => __('Is Hidden'),
                        'dataScope' => 'is_hidden',
                        'label' => ' ',
                        'valueMap' => [
                            'true' => '1',
                            'false' => '0',
                        ],
                        'sortOrder' => 30,
                    ],
                ],
            ],
        ];

        return $meta;
    }

    /**
     * @param \Magento\Bundle\Ui\DataProvider\Product\Form\Modifier\Composite $subject
     * @param $data
     * @return mixed
     * @throws \Magento\Framework\Exception\InputException
     * @throws \Magento\Framework\Exception\NoSuchEntityException
     */
    public function afterModifyData(
        \Magento\Bundle\Ui\DataProvider\Product\Form\Modifier\Composite $subject,
        $data
    )
    {
        $product = $this->locator->getProduct();
        $modelId = $product->getId();
        $isBundleProduct = $product->getTypeId() === Type::TYPE_CODE;
        $optionfactory = $this->optionFactory->create()->load($modelId, 'parent_id');
        $optionfactorycollection = $optionfactory->getCollection()->getData();

        if ($isBundleProduct && $modelId) {
            foreach ($data[$modelId][BundlePanel::CODE_BUNDLE_OPTIONS][BundlePanel::CODE_BUNDLE_OPTIONS] as $key => $val) {
                $data[$modelId][BundlePanel::CODE_BUNDLE_OPTIONS][BundlePanel::CODE_BUNDLE_OPTIONS][$key]['is_hidden'] = ($optionfactorycollection[$key]['is_hidden']) ? '1' : '0';
            }
        }

        return $data;

    }
}

Now in the last step, we need to create the column in the ‘catalog_product_bundle_option’ table. For this, we can create an InstallSchema file.

$setup->startSetup();
$connection = $setup->getConnection();
$tableName = $setup->getTable(self::TABLE_NAME);

if ($connection->isTableExists($tableName) == true) {
    $columns = [
        'is_hidden' => [
            'type' => \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
            'nullable' => false,
            'unsigned' => true,
            'comment' => __('Bundel Item Hidden Status')
        ]
    ];

    foreach ($columns as $name => $definition) {
        $connection->addColumn($tableName, $name, $definition);
    }
}
$setup->endSetup();

Adding a new option in a Bundle Item in Magento 2 opens up endless possibilities for creating dynamic and personalized product bundles. This customization empowers store owners to meet diverse customer needs while enhancing the shopping experience. Whether you’re offering tailored bundles or experimenting with new configurations, mastering this process ensures your store stays competitive in the e-commerce landscape. By implementing these changes effectively, you can boost customer satisfaction and drive higher sales.

That’s all, thanks for your attention. Happy coding 😀

jhjbhjbhjbhjjjhbj

Share Post

Leave a Reply

Your email address will not be published. Required fields are marked *