0

I'm pretty new to Magento, and I are trying to create a module for it, but I'm a little stock now because I'm trying to get all products out there are created in the store there are enabled.

I'm working on a custom central class for my module so I can call my functions on cross for CLI, Crontabs where else I need it.

And what I need its called just a new product class and then get products data out like this.

use SmartPack\Framework\Product;

$products = new Product();
print_r($products->getProducts());

And the class I start working on its like this

<?php
namespace CustomClass\Framework;

class Product
{
    function getProducts()
    {
        return [];
    }
}

Mabey I miss understand how its working on Magento and how to speak with the framework, so hope on a little help here.

2 Answers 2

0

use following code to get enable product collection.

class Yourclass extends \Magento\Framework\View\Element\Template
{   
    /*Product collection variable*/ 
    protected $_productCollection;

    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,        
        \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollection,       
        array $data = []
    )
    {    
        $this->_productCollection= $productCollection;    
        parent::__construct($context, $data);
    }
    
    public function getProductCollection()
    {

        $collection = $this->_productCollection->create();
        $collection->addAttributeToSelect('*');
        $collection->addAttributeToFilter('status',\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED);

        return $collection;
    }
}
?>
3
  • If I use your sample, its give me this error return ( PHP Fatal error: Uncaught ArgumentCountError: Too few arguments to function SmartPack\Framework\Product::__construct(), 0 passed in /var/www/project/app/code/SmartPack/WMS/Console/Command/ProductSync.php on line 54 and at least 2 expected in /var/www/project/app/code/SmartPack/Framework/Product.php:10 ) So the problem is when I try call this function inside a CLI code its get broken. Commented Aug 11, 2021 at 15:30
  • It clear tell that in your constructor you not pass the argument where in above answer I pass 2 argument there. Commented Aug 12, 2021 at 4:21
  • Yes, I know the problem is its not working, the way I found its not the same as yours, but almost, did not know the different for sure, look my replay. Mabey you can explain why my code working and what you show me here did not working as expect. Commented Aug 12, 2021 at 20:06
0

I'm search a lot and found this solution, and its working as I need. The response from Dhiren I did not found use full based on I need my data into a CLI command.

<?php

namespace SmartPack\Framework;

class Product
{
    function getProducts()
    {
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $this->productCollection = $objectManager->get('Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');

        $products = $this->productCollection->create()
            ->addAttributeToSelect('*')
            ->addAttributeToFilter(
                'status',
                \Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED
            )
            ->getData();

        return $products;
    }
}

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.