Magento2 doesn’t provide the option ‘Is Hidden’ which was there in Magneto1. So here we will explain to you how to add the ‘Is Hidden’ option with help of that anyone can create any other option based on their requirement.
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();
That’s all, thanks for your attention. Happy coding 😀
Leave a Reply