Oh snap! You are using an old version of browser. Update your browser to get new awesome features. Click for more details.

How to call CLI command via Cron file and pass an object as param



Put this code in your cron file

/**
 * @var \Magento\Framework\Serialize\SerializerInterface
 */
private $serializer;

/**
 * @var \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory
 */
private $productCollectionFactory;

public function __construct(
    \Magento\Framework\Serialize\SerializerInterface $serializer,
    \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory
) {
    $this->serializer               = $serializer;
    $this->productCollectionFactory = $productCollectionFactory;
}

public function execute()
{
    $collection = $this->productCollectionFactory->create();
    $collection->addAttributeToSelect('*');
    $collection = $this->serializer->serialize($collection);
    //Set you CLI command here and pass product collection array in the --collection argument
    system('php bin/magento customer:product:update --collection='.escapeshellarg($collection));

}

How to get --collection data in CLI file

$collection = $input->getOption(self::COLLECTION_OBJECT);

$collection = $this->serializer->unserialize($collection);

1 comment