Skip to content

[Forwardport] [FIX]: Recent orders are not filtered per store at the customer account page #14910

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 39 additions & 7 deletions app/code/Magento/Sales/Block/Order/Recent.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@
*/
namespace Magento\Sales\Block\Order;

use Magento\Framework\View\Element\Template\Context;
use Magento\Sales\Model\ResourceModel\Order\CollectionFactory;
use Magento\Customer\Model\Session;
use Magento\Sales\Model\Order\Config;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Framework\App\ObjectManager;

/**
* Sales order history block
*
Expand All @@ -13,6 +20,11 @@
*/
class Recent extends \Magento\Framework\View\Element\Template
{
/**
* Limit of orders
*/
const ORDER_LIMIT = 5;

/**
* @var \Magento\Sales\Model\ResourceModel\Order\CollectionFactory
*/
Expand All @@ -28,25 +40,34 @@ class Recent extends \Magento\Framework\View\Element\Template
*/
protected $_orderConfig;

/**
* @var \Magento\Store\Model\StoreManagerInterface
*/
private $storeManager;

/**
* @param \Magento\Framework\View\Element\Template\Context $context
* @param \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory
* @param \Magento\Customer\Model\Session $customerSession
* @param \Magento\Sales\Model\Order\Config $orderConfig
* @param array $data
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
*/
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory,
\Magento\Customer\Model\Session $customerSession,
\Magento\Sales\Model\Order\Config $orderConfig,
array $data = []
Context $context,
CollectionFactory $orderCollectionFactory,
Session $customerSession,
Config $orderConfig,
array $data = [],
StoreManagerInterface $storeManager = null
) {
$this->_orderCollectionFactory = $orderCollectionFactory;
$this->_customerSession = $customerSession;
$this->_orderConfig = $orderConfig;
parent::__construct($context, $data);
$this->_isScopePrivate = true;
$this->storeManager = $storeManager ?: ObjectManager::getInstance()
->get(StoreManagerInterface::class);
parent::__construct($context, $data);
}

/**
Expand All @@ -55,19 +76,30 @@ public function __construct(
protected function _construct()
{
parent::_construct();
$this->getRecentOrders();
}

/**
* Get recently placed orders. By default they will be limited by 5.
*/
private function getRecentOrders()
{
$orders = $this->_orderCollectionFactory->create()->addAttributeToSelect(
'*'
)->addAttributeToFilter(
'customer_id',
$this->_customerSession->getCustomerId()
)->addAttributeToFilter(
'store_id',
$this->storeManager->getStore()->getId()
)->addAttributeToFilter(
'status',
['in' => $this->_orderConfig->getVisibleOnFrontStatuses()]
)->addAttributeToSort(
'created_at',
'desc'
)->setPageSize(
'5'
self::ORDER_LIMIT
)->load();
$this->setOrders($orders);
}
Expand Down
67 changes: 47 additions & 20 deletions app/code/Magento/Sales/Test/Unit/Block/Order/RecentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@
*/
namespace Magento\Sales\Test\Unit\Block\Order;

use Magento\Framework\View\Element\Template\Context;
use Magento\Sales\Model\ResourceModel\Order\CollectionFactory;
use Magento\Customer\Model\Session;
use Magento\Sales\Model\Order\Config;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Framework\View\Layout;
use Magento\Store\Api\Data\StoreInterface;
use Magento\Sales\Model\ResourceModel\Order\Collection;

class RecentTest extends \PHPUnit\Framework\TestCase
{
/**
Expand Down Expand Up @@ -32,26 +41,33 @@ class RecentTest extends \PHPUnit\Framework\TestCase
*/
protected $orderConfig;

/**
* @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $storeManagerMock;

protected function setUp()
{
$this->context = $this->createMock(\Magento\Framework\View\Element\Template\Context::class);
$this->context = $this->createMock(Context::class);
$this->orderCollectionFactory = $this->createPartialMock(
\Magento\Sales\Model\ResourceModel\Order\CollectionFactory::class,
CollectionFactory::class,
['create']
);
$this->customerSession = $this->createPartialMock(\Magento\Customer\Model\Session::class, ['getCustomerId']);
$this->customerSession = $this->createPartialMock(Session::class, ['getCustomerId']);
$this->orderConfig = $this->createPartialMock(
\Magento\Sales\Model\Order\Config::class,
Config::class,
['getVisibleOnFrontStatuses']
);
$this->storeManagerMock = $this->getMockBuilder(StoreManagerInterface::class)
->getMockForAbstractClass();
}

public function testConstructMethod()
{
$data = [];
$attribute = ['customer_id', 'status'];
$attribute = ['customer_id', 'store_id', 'status'];
$customerId = 25;
$layout = $this->createPartialMock(\Magento\Framework\View\Layout::class, ['getBlock']);
$storeId = 4;
$layout = $this->createPartialMock(Layout::class, ['getBlock']);
$this->context->expects($this->once())
->method('getLayout')
->will($this->returnValue($layout));
Expand All @@ -64,14 +80,20 @@ public function testConstructMethod()
->method('getVisibleOnFrontStatuses')
->will($this->returnValue($statuses));

$orderCollection = $this->createPartialMock(\Magento\Sales\Model\ResourceModel\Order\Collection::class, [
'addAttributeToSelect',
'addFieldToFilter',
'addAttributeToFilter',
'addAttributeToSort',
'setPageSize',
'load'
]);
$this->storeManagerMock = $this->getMockBuilder(StoreManagerInterface::class)
->getMockForAbstractClass();
$storeMock = $this->getMockBuilder(StoreInterface::class)->getMockForAbstractClass();
$this->storeManagerMock->expects($this->once())->method('getStore')->willReturn($storeMock);
$storeMock->expects($this->any())->method('getId')->willReturn($storeId);

$orderCollection = $this->createPartialMock(Collection::class, [
'addAttributeToSelect',
'addFieldToFilter',
'addAttributeToFilter',
'addAttributeToSort',
'setPageSize',
'load'
]);
$this->orderCollectionFactory->expects($this->once())
->method('create')
->will($this->returnValue($orderCollection));
Expand All @@ -85,25 +107,30 @@ public function testConstructMethod()
->willReturnSelf();
$orderCollection->expects($this->at(2))
->method('addAttributeToFilter')
->with($attribute[1], $this->equalTo(['in' => $statuses]))
->will($this->returnSelf());
->with($attribute[1], $this->equalTo($storeId))
->willReturnSelf();
$orderCollection->expects($this->at(3))
->method('addAttributeToFilter')
->with($attribute[2], $this->equalTo(['in' => $statuses]))
->will($this->returnSelf());
$orderCollection->expects($this->at(4))
->method('addAttributeToSort')
->with('created_at', 'desc')
->will($this->returnSelf());
$orderCollection->expects($this->at(4))
$orderCollection->expects($this->at(5))
->method('setPageSize')
->with('5')
->will($this->returnSelf());
$orderCollection->expects($this->at(5))
$orderCollection->expects($this->at(6))
->method('load')
->will($this->returnSelf());
$this->block = new \Magento\Sales\Block\Order\Recent(
$this->context,
$this->orderCollectionFactory,
$this->customerSession,
$this->orderConfig,
$data
[],
$this->storeManagerMock
);
$this->assertEquals($orderCollection, $this->block->getOrders());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,21 @@

?>
<div class="block block-dashboard-orders">
<?php $_orders = $block->getOrders(); ?>
<?php
$_orders = $block->getOrders();
$count = count($_orders);
?>
<div class="block-title order">
<strong><?= /* @escapeNotVerified */ __('Recent Orders') ?></strong>
<?php if (sizeof($_orders->getItems()) > 0): ?>
<?php if ($count > 0): ?>
<a class="action view" href="<?= /* @escapeNotVerified */ $block->getUrl('sales/order/history') ?>">
<span><?= /* @escapeNotVerified */ __('View All') ?></span>
</a>
<?php endif; ?>
</div>
<div class="block-content">
<?= $block->getChildHtml() ?>
<?php if (sizeof($_orders->getItems()) > 0): ?>
<?php if ($count > 0): ?>
<div class="table-wrapper orders-recent">
<table class="data table table-order-items recent" id="my-orders-table">
<caption class="table-caption"><?= /* @escapeNotVerified */ __('Recent Orders') ?></caption>
Expand Down