Skip to content

Commit

Permalink
ENGCOM-1217: Configurable product price options by store #13933
Browse files Browse the repository at this point in the history
  • Loading branch information
Stanislav Idolov authored Apr 4, 2018
2 parents b7ca049 + d2657f1 commit 4e429c4
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Magento\Catalog\Model\ResourceModel\Product\LinkedProductSelectBuilderInterface;
use Magento\Framework\App\ResourceConnection;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
use Magento\Store\Model\StoreManagerInterface;

/**
* Retrieve list of products where each product contains lower price than others at least for one possible price type
Expand All @@ -31,7 +32,12 @@ class LowestPriceOptionsProvider implements LowestPriceOptionsProviderInterface
private $collectionFactory;

/**
* Key is product id. Value is array of prepared linked products
* @var StoreManagerInterface
*/
private $storeManager;

/**
* Key is product id and store id. Value is array of prepared linked products
*
* @var array
*/
Expand All @@ -41,34 +47,38 @@ class LowestPriceOptionsProvider implements LowestPriceOptionsProviderInterface
* @param ResourceConnection $resourceConnection
* @param LinkedProductSelectBuilderInterface $linkedProductSelectBuilder
* @param CollectionFactory $collectionFactory
* @param StoreManagerInterface $storeManager
*/
public function __construct(
ResourceConnection $resourceConnection,
LinkedProductSelectBuilderInterface $linkedProductSelectBuilder,
CollectionFactory $collectionFactory
CollectionFactory $collectionFactory,
StoreManagerInterface $storeManager
) {
$this->resource = $resourceConnection;
$this->linkedProductSelectBuilder = $linkedProductSelectBuilder;
$this->collectionFactory = $collectionFactory;
$this->storeManager = $storeManager;
}

/**
* {@inheritdoc}
*/
public function getProducts(ProductInterface $product)
{
if (!isset($this->linkedProductMap[$product->getId()])) {
$key = $this->storeManager->getStore()->getId() . '-' . $product->getId();
if (!isset($this->linkedProductMap[$key])) {
$productIds = $this->resource->getConnection()->fetchCol(
'(' . implode(') UNION (', $this->linkedProductSelectBuilder->build($product->getId())) . ')'
);

$this->linkedProductMap[$product->getId()] = $this->collectionFactory->create()
$this->linkedProductMap[$key] = $this->collectionFactory->create()
->addAttributeToSelect(
['price', 'special_price', 'special_from_date', 'special_to_date', 'tax_class_id']
)
->addIdFilter($productIds)
->getItems();
}
return $this->linkedProductMap[$product->getId()];
return $this->linkedProductMap[$key];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Model\ResourceModel\Product\LinkedProductSelectBuilderInterface;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Store\Api\Data\StoreInterface;
use Magento\Store\Model\Store;

class LowestPriceOptionsProviderTest extends \PHPUnit\Framework\TestCase
{
Expand Down Expand Up @@ -42,6 +45,16 @@ class LowestPriceOptionsProviderTest extends \PHPUnit\Framework\TestCase
*/
private $productCollection;

/**
* @var StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $storeManagerMock;

/**
* @var StoreInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $storeMock;

protected function setUp()
{
$this->connection = $this
Expand All @@ -68,6 +81,11 @@ protected function setUp()
->setMethods(['create'])
->getMock();
$this->collectionFactory->expects($this->once())->method('create')->willReturn($this->productCollection);
$this->storeManagerMock = $this->getMockBuilder(StoreManagerInterface::class)
->getMockForAbstractClass();
$this->storeMock = $this->getMockBuilder(StoreInterface::class)
->setMethods(['getId'])
->getMockForAbstractClass();

$objectManager = new ObjectManager($this);
$this->model = $objectManager->getObject(
Expand All @@ -76,6 +94,7 @@ protected function setUp()
'resourceConnection' => $this->resourceConnection,
'linkedProductSelectBuilder' => $this->linkedProductSelectBuilder,
'collectionFactory' => $this->collectionFactory,
'storeManager' => $this->storeManagerMock,
]
);
}
Expand All @@ -94,6 +113,13 @@ public function testGetProducts()
->willReturnSelf();
$this->productCollection->expects($this->once())->method('addIdFilter')->willReturnSelf();
$this->productCollection->expects($this->once())->method('getItems')->willReturn($linkedProducts);
$this->storeManagerMock->expects($this->any())
->method('getStore')
->with(Store::DEFAULT_STORE_ID)
->willReturn($this->storeMock);
$this->storeMock->expects($this->any())
->method('getId')
->willReturn(Store::DEFAULT_STORE_ID);

$this->assertEquals($linkedProducts, $this->model->getProducts($product));
}
Expand Down

0 comments on commit 4e429c4

Please sign in to comment.