Skip to content

Trying to save the bundle’s child custom option field in db

I have created new shortdescription textarea field inside the magento admin. But after saving the product in admin field data is not store in catalog_product_bundle_selection
where i can see other bundle child value is store in this table.

Here is my code

VendorModuleetcdbSchema.xml

<?xml version="1.0"?>
<!--
/**
 * Created By: vendor . All rights reserved   
 */
-->
<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
    <table name="catalog_product_bundle_selection">
        <column xsi:type="varchar" name="short_description" nullable="true" length="255" comment="Short Description"/>
    </table>
</schema>

BundleProductetcadminhtmldi.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="MagentoBundleUiDataProviderProductFormModifierBundleCustomOptions">
        <plugin
            sortOrder="100"
            name="MyVendorMyModuleBundleModifierCustomOptions"
            type="BundleProductPluginUiDataProviderProductFormModifierBundleCustomOptions"/>
    </type>
    <type name="MagentoBundleUiDataProviderProductFormModifierComposite">
        <plugin
            sortOrder="100"
            name="MyVendorMyModuleModifierComposite"
            type="BundleProductPluginUiDataProviderProductFormModifierComposite"/>
    </type>
    <preference for="MagentoBundleModelLinkManagement" type="BundleProductModelLinkManagement" />
</config>

BundleProductPluginUiDataProviderProductFormModifierBundleCustomOptions.php

<?php 
namespace BundleProductPluginUiDataProviderProductFormModifier;

use MagentoUiComponentFormField;
use MagentoUiComponentFormElementTextarea;
use MagentoUiComponentFormElementDataTypeText;
use MagentoBundleUiDataProviderProductFormModifierBundleCustomOptions as MagentoBundleCustomOptions;

class BundleCustomOptions
{

    const FIELD_CUSTOM_FIELD_OPTION_NAME = 'short_description';

    public function afterModifyMeta(MagentoBundleCustomOptions $subject, array $meta)
    {
        if (isset($meta['bundle-items']['children']['bundle_options']['children']['record']['children']['product_bundle_container']['children']['bundle_selections']['children']['record']['children'])) {
        $data = $meta['bundle-items']['children']['bundle_options']['children']['record']['children']['product_bundle_container']['children']['bundle_selections']['children']['record']['children'][static::FIELD_CUSTOM_FIELD_OPTION_NAME] = $this->getCuststomFieldOptionFieldConfig(125);
        }
        return $meta;
    }

    protected function getCuststomFieldOptionFieldConfig($sortOrder)
    {
        return [
            'arguments' => [
                'data' => [
                    'config' => [
                        'label' => __('Short Description'),
                        'componentType' => Field::NAME,
                        'formElement' => Textarea::NAME,
                        'dataScope' => 'short_description',
                        'dataType' => Text::NAME,
                        'sortOrder' => $sortOrder,
                        'visible' => true,
                    ],
                ],
            ],
        ];
    }
}

BundleProductPluginUiDataProviderProductFormModifier

<?php 
namespace BundleProductPluginUiDataProviderProductFormModifier;

use MagentoBundleUiDataProviderProductFormModifierComposite as BundleComposite;
use MagentoCatalogModelLocatorLocatorInterface;
use MagentoBundleModelProductType;
use MagentoBundleUiDataProviderProductFormModifierBundlePanel;
use MagentoBundleModelSelection;

class Composite
{
    protected $locator;
    protected $selection;

    /**
     * @param LocatorInterface $locator
     * @param Selection $selection
     */
    public function __construct(
        LocatorInterface $locator,
        Selection $selection
    ) {
        $this->locator = $locator;
        $this->selection = $selection;
    }

    public function afterModifyData(BundleComposite $subject, array $data)
    {
        $product = $this->locator->getProduct();
        $modelId = $product->getId();
        $isBundleProduct = $product->getTypeId() === Type::TYPE_CODE;
        if ($isBundleProduct && $modelId) {
            foreach ($data[$modelId][BundlePanel::CODE_BUNDLE_OPTIONS][BundlePanel::CODE_BUNDLE_OPTIONS] as &$option) {
                foreach ($option['bundle_selections'] as &$selection) {
                    $this->selection->load($selection['selection_id']);
                    $content = $selection['short_description'] = $this->selection->getCustomField();
                }
            }
        }
        return $data;
    }
}

BundleProductModelLinkManagement.php

<?php 
namespace BundleProductModel;

class LinkManagement extends MagentoBundleModelLinkManagement
{

    // ....

    /**
     * @param MagentoBundleModelSelection $selectionModel
     * @param MagentoBundleApiDataLinkInterface $productLink
     * @param string $linkedProductId
     * @param string $parentProductId
     * @return MagentoBundleModelSelection
     * @SuppressWarnings(PHPMD.CyclomaticComplexity)
     * @SuppressWarnings(PHPMD.NPathComplexity)
     */
    protected function mapProductLinkToSelectionModel(
        MagentoBundleModelSelection $selectionModel,
        MagentoBundleApiDataLinkInterface $productLink,
        $linkedProductId,
        $parentProductId
    ) {

        $selectionModel = parent::mapProductLinkToSelectionModel($selectionModel, $productLink, $linkedProductId, $parentProductId);
        if ($productLink->getCustomField() !== null) {
            $selectionModel->setCustomField($productLink->getCustomField());
        }

        return $selectionModel;
    }

    // ....
}