Magento – How to remove bundle options from product when loading the admin edit view

controllersmagento2

I want to strip all bundle options from a bundle product when it loads except the first one. To give this some context, I have products with upwards of 20 options, each containing the same set of products. This means the form contains literally thousands of fields. I was working around it by increasing php max_input_vars but when that topped 10,000 and still failed saving one of the largest products I figured it was getting stupid.

At the moment, when saving a bundle product I've attached a function to the save handler which copies the first bundle option and applies it to the remaining option. I plan to add a number input field which specifies the number of bundle options instead of adding the options manually, but that's for another time.

The problem is, I don't know where to begin to find out where I can intercept the product loading and strip out the options I don't want. Obviously when the product has loaded I only want the UI to show one bundle option with the quantity field, not loads of identical bundle options.

To top that off, if you try to save a bundle product which has more than 20 options the table paginates. If you don't make any changes and try to save it throws an error. There's an issue about it on GitHub here: https://github.com/magento/magento2/issues/6916

So, which Controller file can I override so that when a Bundle product is loading I can remove all but one of the bundle options?

Best Answer

The data for the bundle grid comes from the following file:

Magento_Bundle/Ui/DataProvider/Product/Form/Modifier/Composite.php

I created a module which adds a plugin.

di.xml:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <type name="Magento\Bundle\Ui\DataProvider\Product\Form\Modifier\Composite">
        <plugin name="my-composite-modifier" type="Namespace\Modulename\Plugin\CompositePlugin" sortOrder="1" />
    </type>
</config>

Then the php to start playing with the data.

Plugin/CompositePlugin.php:

<?php
namespace Namespace\Modulename\Plugin;

class CompositePlugin
{
    public function afterModifyData(\Magento\Bundle\Ui\DataProvider\Product\Form\Modifier\Composite $subject, $data)
    {
        // Do something with the data here!
        // Bundle items are under:
        // $data[product_id]["bundle_options"]["bundle_options"]

        return $data;
    }
}

Hopefully this helps someone else.

Related Topic