2

I've had a custom select option in the product edit in the backend. When I try to save the product, my option is not saved.

Here the code :

di.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <virtualType name="Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\Pool">
        <arguments>
            <argument name="modifiers" xsi:type="array">
                <item name="ntdd_product_conf" xsi:type="array">
                    <item name="class" xsi:type="string">           Vendor\Module\Ui\DataProvider\Product\Form\Modifier\NewField
                    </item>
                    <item name="sortOrder" xsi:type="number">70</item>
                </item>
            </argument>
        </arguments>
    </virtualType>
</config>

NewField.php

    <?php

    namespace 
    Vendor\Module\Ui\DataProvider\Product\Form\Modifier;

    use Magento\Catalog\Model\Locator\LocatorInterface;
    use 
  Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\AbstractModifier;
    use Magento\Framework\Stdlib\ArrayManager;
    use Magento\Framework\UrlInterface;
    use Magento\Ui\Component\Container;
    use Magento\Ui\Component\Form\Fieldset;
    use Magento\Ui\Component\Form\Element\DataType\Text;
    use Magento\Ui\Component\Form\Element\Select;
    use Magento\Ui\Component\Form\Field;

class DateDelivery extends AbstractModifier
{

    // Components indexes
    const CUSTOM_FIELDSET_INDEX   = 'ntdd_product_conf';
    const CUSTOM_FIELDSET_CONTENT = 'ntdd_product_conf_content';
    const CONTAINER_HEADER_NAME   = 'ntdd_product_conf_content_header';

    // Fields names
    const FIELD_NAME_SELECT = 'ntdd_ready_days';

    /**
     * @var \Magento\Catalog\Model\Locator\LocatorInterface
     */
    protected $locator;

    /**
     * @var ArrayManager
     */
    protected $arrayManager;

    /**
     * @var UrlInterface
     */
    protected $urlBuilder;

    /**
     * @var array
     */
    protected $meta = [];

    /**
     * @param LocatorInterface $locator
     * @param ArrayManager $arrayManager
     * @param UrlInterface $urlBuilder
     */
    public function __construct(
        LocatorInterface $locator,
        ArrayManager $arrayManager,
        UrlInterface $urlBuilder
    ) {
        $this->locator = $locator;
        $this->arrayManager = $arrayManager;
        $this->urlBuilder = $urlBuilder;
    }

    /**
     * Data modifier, does nothing in our example.
     *
     * @param array $data
     * @return array
     */
    public function modifyData(array $data)
    {
        return $data;
    }

    /**
     * Meta-data modifier: adds ours fieldset
     *
     * @param array $meta
     * @return array
     */
    public function modifyMeta(array $meta)
    {
        $this->meta = $meta;
        $this->addCustomFieldset();

        return $this->meta;
    }

    /**
     * Merge existing meta-data with our meta-data (do not overwrite it!)
     *
     * @return void
     */
    protected function addCustomFieldset()
    {
        $this->meta = array_merge_recursive(
            $this->meta,
            [
                static::CUSTOM_FIELDSET_INDEX => $this->getFieldsetConfig(),
            ]
        );
    }

    /**
     * Declare ours fieldset config
     *
     * @return array
     */
    protected function getFieldsetConfig()
    {
        return [
            'arguments' => [
                'data' => [
                    'config' => [
                        'label' => __('DateDelivery Configurations'),
                        'componentType' => Fieldset::NAME,
                        'dataScope' => static::DATA_SCOPE_PRODUCT, // save data in the product data
                        'provider' => static::DATA_SCOPE_PRODUCT . '_data_source',
                        'ns' => static::FORM_NAME,
                        'collapsible' => true,
                        'sortOrder' => 10,
                        'opened' => true,
                    ],
                ],
            ],
            'children' => [
                static::CONTAINER_HEADER_NAME => $this->getHeaderContainerConfig(10),
                static::FIELD_NAME_SELECT => $this->getSelectFieldConfig(30),
            ],
        ];
    }

    /**
     * Get config for header container
     *
     * @param int $sortOrder
     * @return array
     */
    protected function getHeaderContainerConfig($sortOrder)
    {
        return [
            'arguments' => [
                'data' => [
                    'config' => [
                        'label' => null,
                        'formElement' => Container::NAME,
                        'componentType' => Container::NAME,
                        'template' => 'ui/form/components/complex',
                        'sortOrder' => $sortOrder,
                        'content' => __('please setup DateDelivery for this product.'),
                    ],
                ],
            ],
            'children' => [],
        ];
    }

    /**
     * Example select field config
     *
     * @param $sortOrder
     * @return array
     */
    protected function getSelectFieldConfig($sortOrder)
    {
        return [
            'arguments' => [
                'data' => [
                    'config' => [
                        'label' => __('Day(s) before being ready'),
                        'componentType' => Field::NAME,
                        'formElement' => Select::NAME,
                        'dataScope' => static::FIELD_NAME_SELECT,
                        'dataType' => Text::NAME,
                        'sortOrder' => $sortOrder,
                        'options' => $this->_getOptions(),
                        'visible' => true,
                        'disabled' => false,
                    ],
                ],
            ],
        ];
    }

    /**
     * Get days options as an option array:
     *      [
     *          label => string,
     *          value => option_id
     *      ]
     *
     * @return array
     */
    protected function _getOptions()
    {
        $options = [
            0 => [
                'label' => __('0 day'),
                'value' => 1
            ],
            1 => [
                'label' => __('1 day'),
                'value' => 1
            ],
            2 => [
                'label' => __('2 days'),
                'value' => 2
            ],
            3 => [
                'label' => __('3 days'),
                'value' => 3
            ],
            4 => [
                'label' => __('4 days'),
                'value' => 3
            ],
            5 => [
                'label' => __('5 days'),
                'value' => 3
            ],
            6 => [
                'label' => __('6 days'),
                'value' => 3
            ],
            7 => [
                'label' => __('7 days'),
                'value' => 3
            ],
        ];

        return $options;
    }
    }

When I try to get the attribut I get 'null' result:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->create('Magento\Catalog\Model\Product')->load(1);
$ntdd_ready_days = $product->getData('ntdd_ready_days');

var_dump($ntdd_ready_days);

I can't found where is the problem... Is everything alright ?

0

You are trying to access your custom configuration value like this:

$product->getData('ntdd_ready_days')

This means, ntdd_ready_days should be a product attribute. In order to make this work, you need to firstly create a product attribute with code ntdd_ready_days and then add a hook to the product save action and collect your select value from the post data, set it your attribute and save it.

But I think you are making thing more complex here. What you need to do here is, just create a select type product attribute via an upgrade script. This will automatically show your product attribute option in the backend and Magento will take care of the saving part.

Here is a starter link for you to get a glimpse on how to achieve the same. Rest, you need to figure it out yourself with the help of Google. :)

| improve this answer | |
  • That was the solution! Thank you very much!!! :) – user3535832 Sep 21 '18 at 0:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.