1

I'm creating a cron to pass through all the products and set them visible and active.

This is my execute of the cron who runs well:

public function execute(){
    /**
     * @var $item \Magento\Catalog\Model\Product
     * @var \Magento\Catalog\Model\ResourceModel\Product\Collection $collection
     */

    $items = $this->productCollection->getItems();

    foreach($items as $item){
        echo  $item->getSku() .'<br>';
        $item->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH);
        $item->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED);
        $item->save();
    }
    echo 'finish';
}

I've got a product with none visibility and status disabled, but when I run the function the product is still with none visibility and status disabled.

What I'm doing wrong?

4 Answers 4

0

You should replace the $item->save(); with below code:

$item->getResource()->saveAttribute($item, 'visibility');

$item->getResource()->saveAttribute($item, 'status');
0

For getting product collection with status enable and visibility:

you have to keep below code in block file.

protected $_coreRegistry = null;

public function __construct(
    \Magento\Framework\View\Element\Template\Context $context,
    \Magento\Framework\Registry $registry,
    \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
    \Magento\Catalog\Model\Product\Attribute\Source\Status $productStatus,
    \Magento\Catalog\Model\Product\Visibility $productVisibility,
    array $data = []
)
{

    $this->_coreRegistry = $registry;
    $this->_productCollectionFactory = $productCollectionFactory;
    $this->productStatus = $productStatus;
    $this->productVisibility = $productVisibility;
    parent::__construct($context, $data);
}

public function getProductCollection()
{
    $collection = $this->_productCollectionFactory->create();
    $collection->addAttributeToFilter('manufacturer', ['eq' => '20']);
    $collection->addAttributeToFilter('status', ['in' => $this->productStatus->getVisibleStatusIds()]);
    $collection->setVisibility($this->productVisibility->getVisibleInSiteIds());

    return $collection;
}

Above code working for visibility type for:

  • VISIBILITY_IN_SEARCH
  • VISIBILITY_IN_CATALOG
  • VISIBILITY_BOTH
0

Try to change the Collection and set the AreaCode, I've created something similar and works.

namespace NameSpace\ModuleName\Console\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class EAVMassUpdateCommand extends Command
{
public function __construct(
       \Magento\Framework\App\State $state,
       \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $collectionFactory
    ) {
        $state->setAreaCode('adminhtml');
        $this->_collectionFactory = $collectionFactory;
        parent::__construct();
}

...

public function execute(){

    $items = $this->_collectionFactory->create()
        ->addAttributeToSelect('*')
        ->load();

    foreach($items as $item){
        echo  $item->getSku() .'<br>';
        $item->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH);
        $item->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED);
        $item->save();
    }
    echo 'finish';
}

Visibility Type:

VISIBILITY_NOT_VISIBLE =>  1

VISIBILITY_IN_CATALOG =>  2

VISIBILITY_IN_SEARCH =>  3

VISIBILITY_BOTH =>  4
0

Try this way:

public function __construct(
    .....
    \Magento\Framework\App\Action\Context $context
    \Magento\Catalog\Api\ProductRepositoryInterface $productRepositoryInterface,
    \Magento\Catalog\Model\ResourceModel\Product\Collection  $productCollection
) {
    ......
    $this->productRepository = $productRepositoryInterface;
    $this->productCollection = $productCollection;
    parent::__construct($context);
}
public function execute(){
$items = $this->productCollection->getItems();
foreach($items as $item){
    $productId =  $item->getEntityId();
    $product = $this->productRepository->getById($productId);
    $product->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_DISABLED);
    $product->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH);
    $this->productRepository->save($product);     
}
    echo 'finish';
}

Visibility Type:

VISIBILITY_NOT_VISIBLE =>  1

VISIBILITY_IN_CATALOG =>  2

VISIBILITY_IN_SEARCH =>  3

VISIBILITY_BOTH =>  4

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.