功能:获取某一天(2019-05-03)某产品(957)的订单数量
<?php
/**
* Copyright Olight. All rights reserved.
* Author: gfh
*/
namespace Silk\Reports\Block\Adminhtml\Report\Grid\Column\Renderer;
class Ordersnum extends \Magento\Backend\Block\Widget\Grid\Column\Renderer\AbstractRenderer
{
protected $_itemFactory;
protected $_timezoneInterface;
protected $_timezone;
public function __construct(
\Magento\Sales\Model\Order\ItemFactory $itemFactory,
\Magento\Framework\Stdlib\DateTime\TimezoneInterface $timezoneInterface,
\Magento\Framework\Stdlib\DateTime\Timezone $timezone
) {
$this->_itemFactory = $itemFactory;
$this->_timezoneInterface = $timezoneInterface;
$this->_timezone = $timezone;
}
/**
* Renders:get order items
*
* @param \Magento\Framework\DataObject $row
* @return int
*/
public function render(\Magento\Framework\DataObject $row)
{
//var_dump($row->getData());
try {
//获取参数
$productId = $row->getProductId();//957
$period = $row->getPeriod(); //2019-05-03
if (!empty($productId) && !empty($period)) {
//调整时间
$periodFormat = $this->_timezone->date($period);
$from = $this->_timezoneInterface->convertConfigTimeToUtc($periodFormat->format('Y-m-d 00:00:00'));//2019-05-02 23:00:00
$to = $this->_timezoneInterface->convertConfigTimeToUtc($periodFormat->format('Y-m-d 23:59:59')); //2019-05-03 22:59:59
//获取该productId,在这一天内的订单数量(sales_order_item)
$connection = $this->_itemFactory->create()->getCollection();
$connection->addFieldToFilter(
'product_id',
$productId
)->addFieldToFilter(
'created_at',
['from' => $from, 'to' => $to]
);
return $connection->getSize();
}
return ;
} catch(\Exception $e) {
return ;
}
}
}