Magento

How To Fix Bundle Product Options In Magento 2 ?

How To Fix Bundle Product Options In Magento 2

A bundle of options presented in front of the consumer is known to be a bundle product. The customers can select the items from the bundle product as per their requirement. This feature of Magento development improves the functionality of the website.

Although Magento platform is very easy to manage, you might face some errors, but do not worry we shall help you solve them. Are you facing issues in managing bundle title and other options? If yes, then let’s talk with an example,

Say there are two store Store 1 (en) & Store 2 (it) in to site.

You have set “bunoption 1” name for “store 1(en)” and now you have to switch out store and set “bunoption 21” title for “store2(it)” then save the product.

Expected result

‘Store 1 (en)’ bundle option title = bunoption 1.
‘Store 2 (it)’ bundle option title = bunoption 1.

Title should be changed store vise yet the actual result will be different.

Actual result

It is reflecting to all store view.

Store en’ bundle option title = bunoption 1.
Store it’ bundle option title = bunoption 1.

Your bundle title has not changed as per store view.

Solution

You need to overwrite the Resource Model.

I.e :-

1. app/code/NameSpace/ExtensionName/etc/adminhtml/di.xml

<config xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:noNamespaceSchemaLocation=”urn:magento:framework:ObjectManager/etc config.xsd”>

<preference for=”Magento\Bundle\Model\ResourceModel\Option” type=”NameSpace\ExtensionName\Model\ResourceModel\Option” />

</config>

2. app/code/NameSpace/ExtensionName/Model/ResourceModel/Option.php

namespace NameSpace\ExtensionName\Model\ResourceModel;

class Option extends \Magento\Bundle\Model\ResourceModel\Option
{

          protected function _afterSave(\Magento\Framework\Model\AbstractModel $object)
           {
              $conditions = [
                       ‘option_id = ?’ => $object->getId(),
                       ‘store_id = ?’ => $object->getStoreId(),
                       ‘parent_product_id = ?’ => $object->getParentId()
              ];

              $connection = $this->getConnection();

              if ($this->isOptionPresent($conditions)) {
                   $connection->update(
                         $this->getTable(‘catalog_product_bundle_option_value’),
                           [
                                ‘title’ => $object->getTitle()
                           ],
                           $conditions
                   );
              } else {

                   $data = new \Magento\Framework\DataObject();
                   $data->setOptionId($object->getId())
                   ->setStoreId($object->getStoreId())
                   ->setParentProductId($object->getParentId())
                   ->setTitle($object->getTitle());

                  $connection->insert($this->getTable(‘catalog_product_bundle_option_value’), $data->getData());

                   /**
                   * also saving default value if this store view scope
                   */
                  if ($object->getStoreId()) {
                      $mainConditions = [
                         ‘option_id = ?’ => $object->getId(),
                         ‘store_id = 0’ => $object->getStoreId(),
                         ‘parent_product_id = ?’ => $object->getParentId()
                      ];
                      if (!$this->isOptionPresent($conditions)) {
                            $data->setStoreId(0);
                            $data->setTitle($object->getDefaultTitle());
                            $connection->insert($this->getTable(‘catalog_product_bundle_option_value’), $data->getData());
                      }
                 }
           }

            return $this;
     }

            private function isOptionPresent($conditions)
             {
                   $connection = $this->getConnection();

                   $select = $connection->select()->from($this->getTable(‘catalog_product_bundle_option_value’));
                           foreach ($conditions as $condition => $conditionValue) {
                                  $select->where($condition, $conditionValue);
                           }
                           $select->limit(1);

                           $rowSelect = $connection->fetchRow($select);

                           return (is_array($rowSelect) && !empty($rowSelect));
             }

}

3. Run the below command

php/bin magento setup:upgrade
php bin/magento setup:di:compile

It’s done. Now you can easily manage the bundle title or option name according to store wise.

For any queries !! Just fill up the contact form and our experts will get back to you soon.

Interested & Talk More?

Let's brew something together!

GET IN TOUCH
WhatsApp Image