1

We used the following method, to create a custom field for the product custom option section: https://devhooks.in/blog/magento2-how-to-add-custom-field-in-custom-option

This works fine, but we can not define the value for a store view specific.

How can we modify the code, to save different values for store views?

See our; CustomOptions\Plugin\Catalog\Ui\DataProvider\Product\Form\Modifier

<?php 
namespace Module\CustomOptions\Plugin\Catalog\Ui\DataProvider\Product\Form\Modifier;

class CustomOptions
{

    public function afterModifyMeta(
        \Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\CustomOptions $subject,
        $meta
    ) {
        $meta['custom_options']['children']['options']['children']['record']['children']['container_option']['children']['container_common']['children']['custom_text'] =
        $this->getTitleFieldConfig(
            200,
            [
                'arguments' => [
                    'data' => [
                        'config' => [
                            'label' => __('Description'),
                            'componentType' => \Magento\Ui\Component\Form\Field::NAME,
                            'formElement' => \Magento\Ui\Component\Form\Element\Textarea::NAME,
                            'valueUpdate' => 'input',
                            'imports' => [
                                'optionId' => '${ $.provider }:${ $.parentScope }.option_id'
                            ]
                        ],
                    ],
                ],
            ]
        );
        return $meta;
    }

    /**
     * Get config for "Title" fields
     *
     * @param int $sortOrder
     * @param array $options
     * @return array
     */
    protected function getTitleFieldConfig($sortOrder, array $options = [])
    {
        return array_replace_recursive(
            [
                'arguments' => [
                    'data' => [
                        'config' => [
                            'label' => __('Description'),
                            'componentType' => \Magento\Ui\Component\Form\Field::NAME,
                            'formElement' => \Magento\Ui\Component\Form\Element\Textarea::NAME,
                            'dataScope' => 'description',
                            'dataType' => \Magento\Ui\Component\Form\Element\DataType\Text::NAME,
                            'sortOrder' => $sortOrder,
                            'validation' => [
                                'required-entry' => false
                            ],
                        ],
                    ],
                ],
            ],
            $options
        );
    }
}
1
  • 1
    I believe custom option doesn't allow store view scope.
    – HoangHieu
    Apr 4, 2023 at 4:42

1 Answer 1

0

You can use event catalog_product_save_after to to save different values for store views like you want.

It's should look like this:

In etc/adminhtml/events.xml:

<event name="catalog_product_save_after">
    <observer name="change_value_for_special_store" instance="Module\Custom\Observer\ChangeValueSpecialStore" />
</event>

In class ChangeValueSpecialStore:

<?php

namespace Module\Custom\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Store\Model\StoreManagerInterface;

class ChangeValueSpecialStore implements ObserverInterface {

    protected $storeManager;
    public function __construct
    (
        StoreManagerInterface $storeManager
    )
    {
        $this->storeManager = $storeManager;
    }

    public function execute(Observer $observer)
    {
        $product = $observer->getEvent()->getProduct();
        $stores = $this->storeManager->getStores(true); // get all stores id for loop
        foreach ($stores as $store) {
            if ($store->getId() == 1) {
                $yourValueHere = 'test 1';
                $product->setCustomText($yourValueHere); // example your custom field name is custom_text
                $product->setStoreId($store->getId());
                $product->save();
            }
            if ($store->getId() == 2) {
                $yourValueHere = 'test 2';
                $product->setCustomText($yourValueHere); // example your custom field name is custom_text
                $product->setStoreId($store->getId());
                $product->save();
            }
            ...
        }
    }
}
3
  • Thanks! But how should we implement this?
    – JGeer
    Apr 4, 2023 at 10:34
  • @JGeer, I have update answer, you can check again
    – Shou
    Apr 10, 2023 at 8:01
  • Thanks again! But can you tell me how to implement this well, in the module mentioned?
    – JGeer
    Apr 12, 2023 at 14:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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