Magento2 时间转化

功能:获取某一天(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 ;
        }
    }
}
 

转载于:https://my.oschina.net/ganfanghua/blog/3048938

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值