4

Magento 2.2.5. I have set up a multi website and it is working fine.

My problem is when admin create any new product Product in website field default website selection checkbox is checked by default. so reason for this product is automatically assign to default website.

But i want to remove the default website checkbox checked and give required validation to this fields so admin have a attention to select website scope while create new product.

For more information check below screen-shoot.

Default add new product form

enter image description here

What i want after modification

enter image description here


Edit :

Answer of @Idham Choudry is right for single website because there is only single checkbox for select product in website, but when i use this for multi-website there is more than two checkbox so all the checkbox are required. so ideally & in my case if we select any one checkbox then there is not necessary to select all checkbox. In short all checkbox is not required, if we select any one then validation will be remove.


Try with JavaScript way

  1. Include custom JS using XML in below file

app/code/Namespace/Modulename/view/adminhtml/layout/catalog_product_new.xml

<head>
    <link src="Namespace_modulename::js/website_validation.js"/>
</head>
  1. My custom JS in below path

app/code/Namespace/Modulename/view/adminhtml/web/js/website_validation.js

require(['jquery'],function($){
  $('#save-button').click(function(e) {
            e.preventDefault();
            var test = $(".admin__fieldset-product-websites :input").is(":checked");
            console.log(test);
});

Using this way my custom JS is load successfully but i can't give a validation to Product in website checkbox field.

Can anyone help me to solved out this problem.

Any help would be appreciated!

1

This checkbox fields is generated by virtualtype inside app/code/Magento/Catalog/etc/adminhtml/di.xml

<item name="websites" xsi:type="array">
     <item name="class" xsi:type="string">Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\Websites</item>
     <item name="sortOrder" xsi:type="number">30</item>
</item>

You need to override this inside your module Namespace/Module/etc/adminhtml/di.xml like this:

<virtualType name="Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\Pool">
        <arguments>
            <argument name="modifiers" xsi:type="array">
                <item name="websites" xsi:type="array">
                    <item name="class" xsi:type="string">Namespace\Module\Ui\DataProvider\Product\Form\Modifier\Websites</item>
                  <item name="sortOrder" xsi:type="number">30</item>
                </item>
            </argument>
        </arguments>
    </virtualType>

Now you need to create new ui dataprovider file that will be extending the core file, and modify the getFieldsForFieldset() fuction which generate Product in Websites checkboxes, the new file will be located in Namespace/Module/Ui/DataProvider/Product/Form/Modifier/Websites.php :

namespace Magento\Catalog\Ui\DataProvider\Product\Form\Modifier;

use Magento\Catalog\Model\Locator\LocatorInterface;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Store\Api\WebsiteRepositoryInterface;
use Magento\Store\Api\GroupRepositoryInterface;
use Magento\Store\Api\StoreRepositoryInterface;
use Magento\Ui\Component\Form;
use Magento\Ui\Component\DynamicRows;

class Websites extends \Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\Websites{
   protected function getFieldsForFieldset()
    {
        $children = [];
        $websiteIds = $this->getWebsitesValues();
        $websitesList = $this->getWebsitesList();
        $isNewProduct = !$this->locator->getProduct()->getId();
        $tooltip = [
            'link' => 'http://docs.magento.com/m2/ce/user_guide/configuration/scope.html',
            'description' => __(
                'If your Magento installation has multiple websites, ' .
                'you can edit the scope to use the product on specific sites.'
            ),
        ];
        $sortOrder = 0;
        $label = __('Websites');

        $defaultWebsiteId = $this->websiteRepository->getDefault()->getId();
        foreach ($websitesList as $website) {
            $isChecked = in_array($website['id'], $websiteIds)
                || ($defaultWebsiteId == $website['id'] && $isNewProduct);
            $children[$website['id']] = [
                'arguments' => [
                    'data' => [
                        'config' => [
                            'dataType' => Form\Element\DataType\Number::NAME,
                            'componentType' => Form\Field::NAME,
                            'formElement' => Form\Element\Checkbox::NAME,
                            'description' => __($website['name']),
                            'tooltip' => $tooltip,
                            'sortOrder' => $sortOrder,
                            'dataScope' => 'website_ids.' . $website['id'],
                            'label' => $label,
                            'valueMap' => [
                                'true' => (string)$website['id'],
                                'false' => '0',
                            ],
                            'validation' => [
                                'required-entry' => true,
                                'validate-number' => true,
                            ],
                            'value' => $isChecked ? (string)$website['id'] : ''
                        ],
                    ],
                ],
            ];

            $sortOrder++;
            $tooltip = null;
            $label = ' ';

            if (!$isNewProduct && !in_array($website['id'], $websiteIds) && $website['storesCount']) {
                $children['copy_to_stores.' . $website['id']] = $this->getDynamicRow($website['id'], $sortOrder);
                $sortOrder++;
            }
        }

        return $children;
    }
}

in the above code i just add the required-entry as validation and remove the default value for checkbox ''value' => $isChecked ? (string)$website['id'] : '', and the result will look like this:

enter image description here

  • thanks for giving your valuable time. i was try your above code it is give required but product form is save without checkbox is checked when it is required field. – Chirag Patel Nov 27 '18 at 14:06
  • +1 for your analysis :) – Chirag Patel Nov 27 '18 at 14:07
  • @ChiragPatel check for my updated answer, i just add more validation – Idham Choudry Nov 27 '18 at 14:11
  • Okay let me check. – Chirag Patel Nov 27 '18 at 14:13
  • Validation is working but both checkbox are required field in multiwebsite in my case, it is not necessary to select both checkbox. – Chirag Patel Nov 27 '18 at 14:18

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.