1

I have created a plugin and I want to change product visibility value on product save but it's not working.

ProductAfterSave.php

namespace Vendor\Module\Plugin;

class ProductAfterSave {

    public function afterSave(\Magento\Catalog\Model\Product $subject, $product)
    {
        $productModel = $this->objectManager->create('Magento\Catalog\Model\Product\Action');
        $productModel->setVisiblity(2);
        $productModel->save();

        return $product;
    }
} 

Can anyone please help me, Thanks.

1 Answer 1

3

You should use a before plugin like the below: I used the constante Visibility::VISIBILITY_IN_CATALOG for visibility but of course you can use the value 2.

If you are in trouble, I'll put my code online for you to check out a ful working version. good luck

class ProductBeforeSave {

    public function beforeBeforeSave(\Magento\Catalog\Model\Product $product)
    {
        $product->setVisibility(Visibility::VISIBILITY_IN_CATALOG);
    }
} 
3

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.