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?
The Controller is classic and the template looks like this:
? Seem you're wrong$orders = $this->getProductCollection();
=>$orders = $this->getOrderCollection();