Skip to content

Commit 359a647

Browse files
coderimusrostyslav-hymon
authored andcommitted
Recent orders were not filtered per store
1 parent 18a7e29 commit 359a647

File tree

3 files changed

+87
-26
lines changed

3 files changed

+87
-26
lines changed

app/code/Magento/Sales/Block/Order/Recent.php

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
*/
66
namespace Magento\Sales\Block\Order;
77

8+
use Magento\Framework\View\Element\Template\Context;
9+
use Magento\Sales\Model\ResourceModel\Order\CollectionFactory;
10+
use Magento\Customer\Model\Session;
11+
use Magento\Sales\Model\Order\Config;
12+
use Magento\Store\Model\StoreManagerInterface;
13+
814
/**
915
* Sales order history block
1016
*
@@ -13,6 +19,11 @@
1319
*/
1420
class Recent extends \Magento\Framework\View\Element\Template
1521
{
22+
/**
23+
* Limit of orders
24+
*/
25+
const ORDER_LIMIT = 5;
26+
1627
/**
1728
* @var \Magento\Sales\Model\ResourceModel\Order\CollectionFactory
1829
*/
@@ -28,23 +39,31 @@ class Recent extends \Magento\Framework\View\Element\Template
2839
*/
2940
protected $_orderConfig;
3041

42+
/**
43+
* @var \Magento\Store\Model\StoreManagerInterface
44+
*/
45+
private $storeManager;
46+
3147
/**
3248
* @param \Magento\Framework\View\Element\Template\Context $context
3349
* @param \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory
3450
* @param \Magento\Customer\Model\Session $customerSession
3551
* @param \Magento\Sales\Model\Order\Config $orderConfig
52+
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
3653
* @param array $data
3754
*/
3855
public function __construct(
39-
\Magento\Framework\View\Element\Template\Context $context,
40-
\Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory,
41-
\Magento\Customer\Model\Session $customerSession,
42-
\Magento\Sales\Model\Order\Config $orderConfig,
56+
Context $context,
57+
CollectionFactory $orderCollectionFactory,
58+
Session $customerSession,
59+
Config $orderConfig,
60+
StoreManagerInterface $storeManager,
4361
array $data = []
4462
) {
4563
$this->_orderCollectionFactory = $orderCollectionFactory;
4664
$this->_customerSession = $customerSession;
4765
$this->_orderConfig = $orderConfig;
66+
$this->storeManager = $storeManager;
4867
parent::__construct($context, $data);
4968
$this->_isScopePrivate = true;
5069
}
@@ -55,19 +74,30 @@ public function __construct(
5574
protected function _construct()
5675
{
5776
parent::_construct();
77+
$this->getRecentOrders();
78+
}
79+
80+
/**
81+
* Get recently placed orders. By default they will be limited by 5.
82+
*/
83+
protected function getRecentOrders()
84+
{
5885
$orders = $this->_orderCollectionFactory->create()->addAttributeToSelect(
5986
'*'
6087
)->addAttributeToFilter(
6188
'customer_id',
6289
$this->_customerSession->getCustomerId()
90+
)->addAttributeToFilter(
91+
'store_id',
92+
$this->storeManager->getStore()->getId()
6393
)->addAttributeToFilter(
6494
'status',
6595
['in' => $this->_orderConfig->getVisibleOnFrontStatuses()]
6696
)->addAttributeToSort(
6797
'created_at',
6898
'desc'
6999
)->setPageSize(
70-
'5'
100+
self::ORDER_LIMIT
71101
)->load();
72102
$this->setOrders($orders);
73103
}

app/code/Magento/Sales/Test/Unit/Block/Order/RecentTest.php

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@
55
*/
66
namespace Magento\Sales\Test\Unit\Block\Order;
77

8+
use Magento\Framework\View\Element\Template\Context;
9+
use Magento\Sales\Model\ResourceModel\Order\CollectionFactory;
10+
use Magento\Customer\Model\Session;
11+
use Magento\Sales\Model\Order\Config;
12+
use Magento\Store\Model\StoreManagerInterface;
13+
use Magento\Framework\View\Layout;
14+
use Magento\Store\Api\Data\StoreInterface;
15+
use Magento\Sales\Model\ResourceModel\Order\Collection;
16+
817
class RecentTest extends \PHPUnit\Framework\TestCase
918
{
1019
/**
@@ -32,26 +41,34 @@ class RecentTest extends \PHPUnit\Framework\TestCase
3241
*/
3342
protected $orderConfig;
3443

44+
/**
45+
* @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject
46+
*/
47+
protected $storeManagerMock;
48+
3549
protected function setUp()
3650
{
37-
$this->context = $this->createMock(\Magento\Framework\View\Element\Template\Context::class);
51+
$this->context = $this->createMock(Context::class);
3852
$this->orderCollectionFactory = $this->createPartialMock(
39-
\Magento\Sales\Model\ResourceModel\Order\CollectionFactory::class,
53+
CollectionFactory::class,
4054
['create']
4155
);
42-
$this->customerSession = $this->createPartialMock(\Magento\Customer\Model\Session::class, ['getCustomerId']);
56+
$this->customerSession = $this->createPartialMock(Session::class, ['getCustomerId']);
4357
$this->orderConfig = $this->createPartialMock(
44-
\Magento\Sales\Model\Order\Config::class,
58+
Config::class,
4559
['getVisibleOnFrontStatuses']
4660
);
61+
$this->storeManagerMock = $this->getMockBuilder(StoreManagerInterface::class)
62+
->getMockForAbstractClass();
4763
}
4864

4965
public function testConstructMethod()
5066
{
5167
$data = [];
52-
$attribute = ['customer_id', 'status'];
68+
$attribute = ['customer_id', 'store_id', 'status'];
5369
$customerId = 25;
54-
$layout = $this->createPartialMock(\Magento\Framework\View\Layout::class, ['getBlock']);
70+
$storeId = 4;
71+
$layout = $this->createPartialMock(Layout::class, ['getBlock']);
5572
$this->context->expects($this->once())
5673
->method('getLayout')
5774
->will($this->returnValue($layout));
@@ -64,14 +81,20 @@ public function testConstructMethod()
6481
->method('getVisibleOnFrontStatuses')
6582
->will($this->returnValue($statuses));
6683

67-
$orderCollection = $this->createPartialMock(\Magento\Sales\Model\ResourceModel\Order\Collection::class, [
68-
'addAttributeToSelect',
69-
'addFieldToFilter',
70-
'addAttributeToFilter',
71-
'addAttributeToSort',
72-
'setPageSize',
73-
'load'
74-
]);
84+
$this->storeManagerMock = $this->getMockBuilder(StoreManagerInterface::class)
85+
->getMockForAbstractClass();
86+
$storeMock = $this->getMockBuilder(StoreInterface::class)->getMockForAbstractClass();
87+
$this->storeManagerMock->expects($this->once())->method('getStore')->willReturn($storeMock);
88+
$storeMock->expects($this->any())->method('getId')->willReturn($storeId);
89+
90+
$orderCollection = $this->createPartialMock(Collection::class, [
91+
'addAttributeToSelect',
92+
'addFieldToFilter',
93+
'addAttributeToFilter',
94+
'addAttributeToSort',
95+
'setPageSize',
96+
'load'
97+
]);
7598
$this->orderCollectionFactory->expects($this->once())
7699
->method('create')
77100
->will($this->returnValue($orderCollection));
@@ -85,24 +108,29 @@ public function testConstructMethod()
85108
->willReturnSelf();
86109
$orderCollection->expects($this->at(2))
87110
->method('addAttributeToFilter')
88-
->with($attribute[1], $this->equalTo(['in' => $statuses]))
89-
->will($this->returnSelf());
111+
->with($attribute[1], $this->equalTo($storeId))
112+
->willReturnSelf();
90113
$orderCollection->expects($this->at(3))
114+
->method('addAttributeToFilter')
115+
->with($attribute[2], $this->equalTo(['in' => $statuses]))
116+
->will($this->returnSelf());
117+
$orderCollection->expects($this->at(4))
91118
->method('addAttributeToSort')
92119
->with('created_at', 'desc')
93120
->will($this->returnSelf());
94-
$orderCollection->expects($this->at(4))
121+
$orderCollection->expects($this->at(5))
95122
->method('setPageSize')
96123
->with('5')
97124
->will($this->returnSelf());
98-
$orderCollection->expects($this->at(5))
125+
$orderCollection->expects($this->at(6))
99126
->method('load')
100127
->will($this->returnSelf());
101128
$this->block = new \Magento\Sales\Block\Order\Recent(
102129
$this->context,
103130
$this->orderCollectionFactory,
104131
$this->customerSession,
105132
$this->orderConfig,
133+
$this->storeManagerMock,
106134
$data
107135
);
108136
$this->assertEquals($orderCollection, $this->block->getOrders());

app/code/Magento/Sales/view/frontend/templates/order/recent.phtml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,21 @@
88

99
?>
1010
<div class="block block-dashboard-orders">
11-
<?php $_orders = $block->getOrders(); ?>
11+
<?php
12+
$_orders = $block->getOrders();
13+
$count = count($_orders);
14+
?>
1215
<div class="block-title order">
1316
<strong><?= /* @escapeNotVerified */ __('Recent Orders') ?></strong>
14-
<?php if (sizeof($_orders->getItems()) > 0): ?>
17+
<?php if ($count > 0): ?>
1518
<a class="action view" href="<?= /* @escapeNotVerified */ $block->getUrl('sales/order/history') ?>">
1619
<span><?= /* @escapeNotVerified */ __('View All') ?></span>
1720
</a>
1821
<?php endif; ?>
1922
</div>
2023
<div class="block-content">
2124
<?= $block->getChildHtml() ?>
22-
<?php if (sizeof($_orders->getItems()) > 0): ?>
25+
<?php if ($count > 0): ?>
2326
<div class="table-wrapper orders-recent">
2427
<table class="data table table-order-items recent" id="my-orders-table">
2528
<caption class="table-caption"><?= /* @escapeNotVerified */ __('Recent Orders') ?></caption>

0 commit comments

Comments
 (0)