博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Magento2 时间转化
阅读量:7046 次
发布时间:2019-06-28

本文共 1639 字,大约阅读时间需要 5 分钟。

hot3.png

功能:获取某一天(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
     *
     * \Magento\Framework\DataObject $row
     * 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

你可能感兴趣的文章
nyoj429 骨牌铺方格
查看>>
ttt
查看>>
日语能力考试N2级核心词汇必备—接续词
查看>>
【第42题】【062题库】2019年OCP认证062考试新题
查看>>
Slider Page Control
查看>>
Java WebService一个构建
查看>>
linux一台服务器配置多个Tomcat
查看>>
模板的继承
查看>>
vuex 使用
查看>>
iOS 6 SDK: 在应用内展示App Store 【StoreKit,SKStoreProductViewController】
查看>>
GIt常用命令
查看>>
JavaScript的学习
查看>>
dedecms _ 当前位置问题的代码
查看>>
Win2D 官方文章系列翻译 - 调整控件分辨率
查看>>
SharePoint 离线安装
查看>>
consul_nginx_uprsync动态负载均衡
查看>>
Python基础之逻辑运算符
查看>>
hdu Rescue
查看>>
vagrant特性——基于docker开发环境(docker和vagrant的结合)-0-简介
查看>>
解决在配置Oracle数据库连接错误问题
查看>>