1

I'm trying to extract some datas from magento. Here, the orders. I search on the net and I don't understand why I always got null orders.

I have a Block like this:

namespace TATA\TOTO\Block\Extraction;

use \Magento\Framework\View\Element\Template\Context;
use \Magento\Sales\Model\ResourceModel\Order\CollectionFactory as OrderCollectionFactory;
use \Magento\Sales\Model\ResourceModel\Order\Collection as OrderCollection;
use \TATA\TOTO\Block\AbstractExtractionBlock;

/**
 * Order Block
 */
class Order extends AbstractExtractionBlock
{
    /* @var OrderCollectionFactory $_orderCollectionFactory */
    protected $_orderCollectionFactory;

    /* @var OrderCollection $_orderCollection */
    protected $_orderCollection;

    /**
     * Order constructor.
     *
     * @param Context $context
     * @param OrderCollectionFactory $orderCollectionFactory
     * @param array $data
     */
    public function __construct(
        Context $context,
        OrderCollectionFactory $orderCollectionFactory,
        array $data = [])
    {
        $this->_orderCollectionFactory = $orderCollectionFactory;
        $this->_orderCollection = null;

        parent::__construct($context, $data);null);
    }

    /**
     * Get the Order Collection
     *
     * @return OrderCollection
     */
    public function getOrderCollection()
    {
        if (is_null($this->_orderCollection)) {
            $this->_orderCollection = $this->_orderCollectionFactory->create()
                ->addFieldToSelect('*');

            $this->setPaginationParameters($this->_orderCollection);
        }
        return $this->_orderCollection;
    }
}

The Block extends from an abstract Block which implement an Interface. I'm not sure you need all of this. See the Interface below:

namespace TATA\TOTO\Block;

use \Magento\Framework\Data\Collection;

/**
 * Extraction Block Interface
 */
interface ExtractionBlockInterface
{
    /**
     * Get the pagination parameters into the respective vars.
     */
    public function getPaginationParameters();

    /**
     * Set the pagination parameters.
     *
     * @param Collection $collection
     * @return Collection
     */
    public function setPaginationParameters(Collection $collection);

    /**
     * Escape the param form HTML and delete all newline char.
     *
     * @param string $param
     * @return string
     */
    public function frontendEscape($param);
}

The Controller is classic and the template looks like this:

use \Magento\Sales\Model\ResourceModel\Order\Collection as OrderCollection;
use \Magento\Sales\Model\Order;

/* @var OrderCollection $orders */
$orders = $this->getProductCollection();
echo 'ORDER_ID|CUST_ID|PRODUCT_ID|REFERENCE|UNIT_PRICE|CURRENCY|QUANTITY|DATE<br>';

if (empty($orders)) return; // Prevent the null value for foreach.

// Never goes there because $orders is null

/* @var Order $order */
foreach ($orders as $order) {
    echo $order->getId(); //Trying to display the Id.
}

Do you see an mistake I've made?

4
  • 1
    I'm confused about The Controller is classic and the template looks like this:? Seem you're wrong $orders = $this->getProductCollection(); => $orders = $this->getOrderCollection(); Commented Dec 14, 2016 at 16:54
  • Oh my god... I'm so embarrassed. Shame on me.. I've copy paste the content of an other extractor (product in fact) and my IDE didn't make a warning there... When I said that the Controller is classic is because there's only the following inside: public function execute() { $this->_view->loadLayout(); $this->_view->renderLayout(); }
    – Scorsi
    Commented Dec 15, 2016 at 9:32
  • Your code is working now? Commented Dec 15, 2016 at 9:35
  • 1
    Yeah! It works properly.
    – Scorsi
    Commented Dec 15, 2016 at 9:36

1 Answer 1

0

Seem that your collection is wrong:

Replace:

$orders = $this->getProductCollection(); 

by

$orders = $this->getOrderCollection();

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.