Skip to content

Commit 7107525

Browse files
author
Stanislav Idolov
authored
ENGCOM-1420: [Forwardport] [FIX]: Recent orders are not filtered per store at the customer account page #14910
2 parents 4f9b705 + 1e9ed5c commit 7107525

File tree

3 files changed

+92
-30
lines changed

3 files changed

+92
-30
lines changed

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

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@
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+
use Magento\Framework\App\ObjectManager;
14+
815
/**
916
* Sales order history block
1017
*
@@ -13,6 +20,11 @@
1320
*/
1421
class Recent extends \Magento\Framework\View\Element\Template
1522
{
23+
/**
24+
* Limit of orders
25+
*/
26+
const ORDER_LIMIT = 5;
27+
1628
/**
1729
* @var \Magento\Sales\Model\ResourceModel\Order\CollectionFactory
1830
*/
@@ -28,25 +40,34 @@ class Recent extends \Magento\Framework\View\Element\Template
2840
*/
2941
protected $_orderConfig;
3042

43+
/**
44+
* @var \Magento\Store\Model\StoreManagerInterface
45+
*/
46+
private $storeManager;
47+
3148
/**
3249
* @param \Magento\Framework\View\Element\Template\Context $context
3350
* @param \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory
3451
* @param \Magento\Customer\Model\Session $customerSession
3552
* @param \Magento\Sales\Model\Order\Config $orderConfig
3653
* @param array $data
54+
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
3755
*/
3856
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,
43-
array $data = []
57+
Context $context,
58+
CollectionFactory $orderCollectionFactory,
59+
Session $customerSession,
60+
Config $orderConfig,
61+
array $data = [],
62+
StoreManagerInterface $storeManager = null
4463
) {
4564
$this->_orderCollectionFactory = $orderCollectionFactory;
4665
$this->_customerSession = $customerSession;
4766
$this->_orderConfig = $orderConfig;
48-
parent::__construct($context, $data);
4967
$this->_isScopePrivate = true;
68+
$this->storeManager = $storeManager ?: ObjectManager::getInstance()
69+
->get(StoreManagerInterface::class);
70+
parent::__construct($context, $data);
5071
}
5172

5273
/**
@@ -55,19 +76,30 @@ public function __construct(
5576
protected function _construct()
5677
{
5778
parent::_construct();
79+
$this->getRecentOrders();
80+
}
81+
82+
/**
83+
* Get recently placed orders. By default they will be limited by 5.
84+
*/
85+
private function getRecentOrders()
86+
{
5887
$orders = $this->_orderCollectionFactory->create()->addAttributeToSelect(
5988
'*'
6089
)->addAttributeToFilter(
6190
'customer_id',
6291
$this->_customerSession->getCustomerId()
92+
)->addAttributeToFilter(
93+
'store_id',
94+
$this->storeManager->getStore()->getId()
6395
)->addAttributeToFilter(
6496
'status',
6597
['in' => $this->_orderConfig->getVisibleOnFrontStatuses()]
6698
)->addAttributeToSort(
6799
'created_at',
68100
'desc'
69101
)->setPageSize(
70-
'5'
102+
self::ORDER_LIMIT
71103
)->load();
72104
$this->setOrders($orders);
73105
}

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

Lines changed: 47 additions & 20 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,33 @@ 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
{
51-
$data = [];
52-
$attribute = ['customer_id', 'status'];
67+
$attribute = ['customer_id', 'store_id', 'status'];
5368
$customerId = 25;
54-
$layout = $this->createPartialMock(\Magento\Framework\View\Layout::class, ['getBlock']);
69+
$storeId = 4;
70+
$layout = $this->createPartialMock(Layout::class, ['getBlock']);
5571
$this->context->expects($this->once())
5672
->method('getLayout')
5773
->will($this->returnValue($layout));
@@ -64,14 +80,20 @@ public function testConstructMethod()
6480
->method('getVisibleOnFrontStatuses')
6581
->will($this->returnValue($statuses));
6682

67-
$orderCollection = $this->createPartialMock(\Magento\Sales\Model\ResourceModel\Order\Collection::class, [
68-
'addAttributeToSelect',
69-
'addFieldToFilter',
70-
'addAttributeToFilter',
71-
'addAttributeToSort',
72-
'setPageSize',
73-
'load'
74-
]);
83+
$this->storeManagerMock = $this->getMockBuilder(StoreManagerInterface::class)
84+
->getMockForAbstractClass();
85+
$storeMock = $this->getMockBuilder(StoreInterface::class)->getMockForAbstractClass();
86+
$this->storeManagerMock->expects($this->once())->method('getStore')->willReturn($storeMock);
87+
$storeMock->expects($this->any())->method('getId')->willReturn($storeId);
88+
89+
$orderCollection = $this->createPartialMock(Collection::class, [
90+
'addAttributeToSelect',
91+
'addFieldToFilter',
92+
'addAttributeToFilter',
93+
'addAttributeToSort',
94+
'setPageSize',
95+
'load'
96+
]);
7597
$this->orderCollectionFactory->expects($this->once())
7698
->method('create')
7799
->will($this->returnValue($orderCollection));
@@ -85,25 +107,30 @@ public function testConstructMethod()
85107
->willReturnSelf();
86108
$orderCollection->expects($this->at(2))
87109
->method('addAttributeToFilter')
88-
->with($attribute[1], $this->equalTo(['in' => $statuses]))
89-
->will($this->returnSelf());
110+
->with($attribute[1], $this->equalTo($storeId))
111+
->willReturnSelf();
90112
$orderCollection->expects($this->at(3))
113+
->method('addAttributeToFilter')
114+
->with($attribute[2], $this->equalTo(['in' => $statuses]))
115+
->will($this->returnSelf());
116+
$orderCollection->expects($this->at(4))
91117
->method('addAttributeToSort')
92118
->with('created_at', 'desc')
93119
->will($this->returnSelf());
94-
$orderCollection->expects($this->at(4))
120+
$orderCollection->expects($this->at(5))
95121
->method('setPageSize')
96122
->with('5')
97123
->will($this->returnSelf());
98-
$orderCollection->expects($this->at(5))
124+
$orderCollection->expects($this->at(6))
99125
->method('load')
100126
->will($this->returnSelf());
101127
$this->block = new \Magento\Sales\Block\Order\Recent(
102128
$this->context,
103129
$this->orderCollectionFactory,
104130
$this->customerSession,
105131
$this->orderConfig,
106-
$data
132+
[],
133+
$this->storeManagerMock
107134
);
108135
$this->assertEquals($orderCollection, $this->block->getOrders());
109136
}

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)