0

This is my scenario I have hidden website_ids field in product form (default magento show it) So how i can use plugin for set website ids for product when it create. Is there any entry-point to do this

My goal is if not website ids available when post data so init default website ids for product

Added modifier for modify Form:

public function modifyMeta(array $meta)
{
    $this->_meta = unset($meta['websites']);
    return $this->_meta;
}
0

I post my answer here in case someone in future may need

Create di.xml file for prepare plugin class

<type name="Magento\Catalog\Controller\Adminhtml\Product\Initialization\Helper">
    <plugin name="updateProductWebsiteIds" type="Namespace\Module\Plugin\Controller\Product\Initialization\Helper\WebsitePlugin" sortOrder="40" />
</type>

original class here

Add new file Class WebsitePlugin

public function afterInitialize(
    \Magento\Catalog\Controller\Adminhtml\Product\Initialization\Helper $subject,
    \Magento\Catalog\Model\Product $product
) {
    if (Add your logic check here  && isset($product)) {
        if (!$product->getWebsiteIds()) {
            $defaultWebsiteId = $this->storeManager->getDefaultStoreView()->getWebsiteId();
            $websitesIds = [$defaultWebsiteId];
            $product->setWebsiteIds($websitesIds);
        }
    }
    return $product;
}

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.