Skip to content
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

Fix for #16069: Configurable product price is not displayed if all children are out of stock and even if Display Out of Stock Products is set to "yes" #18776

Closed
wants to merge 1 commit into from
Closed
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
15 changes: 14 additions & 1 deletion app/code/Magento/Catalog/Pricing/Render/FinalPriceBox.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Catalog\Pricing\Render;

Expand Down Expand Up @@ -64,7 +65,9 @@ public function __construct(
*/
protected function _toHtml()
{
if (!$this->salableResolver->isSalable($this->getSaleableItem())) {
if ($this->isApplySalableCheck($this->getSaleableItem()) &&
!$this->salableResolver->isSalable($this->getSaleableItem())
) {
return '';
}

Expand All @@ -86,6 +89,16 @@ protected function _toHtml()
return $this->wrapResult($result);
}

/**
* @param SaleableInterface $salableItem
* @return bool
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
protected function isApplySalableCheck(SaleableInterface $salableItem): bool
{
return true;
}

/**
* Check is MSRP applicable for the current product.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
*
* Extends functionality of the linked product select builder to allow perform
* some additional processing of built Select objects.
*
* @deprecated
* @see \Magento\ConfigurableProduct\Model\ResourceModel\Product\LinkedSelectByParent\Builder
*/
class LinkedProductSelectBuilder implements LinkedProductSelectBuilderInterface
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\ConfigurableProduct\Model\ResourceModel\Product\LinkedSelectByParent;

use Magento\Framework\DB\Select;

/**
* Interface BaseSelectProcessorInterface
*/
interface BaseSelectProcessorInterface
{
/**
* Product table alias
*/
const PRODUCT_TABLE_ALIAS = 'child';

/**
* @param Select $select
* @param int $productId
* @return Select
*/
public function process(Select $select, int $productId): Select;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\ConfigurableProduct\Model\ResourceModel\Product\LinkedSelectByParent;

use Magento\Catalog\Model\ResourceModel\Product\LinkedProductSelectBuilderInterface;

/**
* A decorator for a linked product select builder by parent product.
*
* Extends functionality of the linked product select builder to allow perform
* some additional processing of built Select objects.
*/
class Builder implements LinkedProductSelectBuilderInterface
{
/**
* @var BaseSelectProcessorInterface
*/
private $baseSelectProcessor;

/**
* @var LinkedProductSelectBuilderInterface
*/
private $linkedProductSelectBuilder;

/**
* @param BaseSelectProcessorInterface $baseSelectProcessor
* @param LinkedProductSelectBuilderInterface $linkedProductSelectBuilder
*/
public function __construct(
BaseSelectProcessorInterface $baseSelectProcessor,
LinkedProductSelectBuilderInterface $linkedProductSelectBuilder
) {
$this->baseSelectProcessor = $baseSelectProcessor;
$this->linkedProductSelectBuilder = $linkedProductSelectBuilder;
}

/**
* @inheritdoc
*/
public function build($productId): array
{
$selects = $this->linkedProductSelectBuilder->build($productId);

foreach ($selects as $select) {
$this->baseSelectProcessor->process($select, (int)$productId);
}

return $selects;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\ConfigurableProduct\Model\ResourceModel\Product\LinkedSelectByParent;

use Magento\Framework\DB\Select;
use Magento\Framework\App\ObjectManager;
use Magento\CatalogInventory\Api\StockConfigurationInterface;
use Magento\CatalogInventory\Model\Stock\Status as StockStatus;
use Magento\CatalogInventory\Model\ResourceModel\Stock\Status as StockStatusResource;
use Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable\StockStatusInterface
as StockStatusConfigurableInterface;

/**
* A Select object processor.
*
* Adds stock status limitations to a given Select object.
*/
class StockStatusBaseSelectProcessor implements BaseSelectProcessorInterface
{
/**
* @var StockConfigurationInterface
*/
private $stockConfig;

/**
* @var StockStatusResource
*/
private $stockStatusResource;

/**
* @var StockStatusConfigurableInterface
*/
private $stockConfigurable;

/**
* @param StockConfigurationInterface $stockConfig
* @param StockStatusResource $stockStatusResource
* @param StockStatusConfigurableInterface $stockConfigurable
*/
public function __construct(
StockConfigurationInterface $stockConfig,
StockStatusResource $stockStatusResource,
StockStatusConfigurableInterface $stockConfigurable = null
) {
$this->stockConfig = $stockConfig;
$this->stockStatusResource = $stockStatusResource;
$this->stockConfigurable = $stockConfigurable ?:
ObjectManager::getInstance()->get(StockStatusConfigurableInterface::class);
}

/**
* @inheritdoc
*/
public function process(Select $select, int $productId): Select
{
if ($this->stockConfig->isShowOutOfStock() &&
!$this->isAllChildOutOfStock($productId)
) {
$select->joinInner(
['stock' => $this->stockStatusResource->getMainTable()],
sprintf(
'stock.product_id = %s.entity_id',
BaseSelectProcessorInterface::PRODUCT_TABLE_ALIAS
),
[]
)->where(
'stock.stock_status = ?',
StockStatus::STATUS_IN_STOCK
);
}

return $select;
}

/**
* @param int $productId
* @return bool
* @throws \Exception
*/
private function isAllChildOutOfStock(int $productId): bool
{
return $this->stockConfigurable->isAllChildOutOfStock($productId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
* A Select object processor.
*
* Adds stock status limitations to a given Select object.
*
* @deprecated
* @see \Magento\ConfigurableProduct\Model\ResourceModel\Product\LinkedSelectByParent\StockStatusBaseSelectProcessor
*/
class StockStatusBaseSelectProcessor implements BaseSelectProcessorInterface
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable;

use Magento\Framework\EntityManager\MetadataPool;
use Magento\Framework\App\ResourceConnection;
use Magento\Catalog\Api\Data\ProductInterface;
use Magento\CatalogInventory\Api\Data\StockStatusInterface as CatalogInventoryStockStatusInterface;
use Magento\Framework\DB\Adapter\AdapterInterface;

class StockStatus implements StockStatusInterface
{
/**
* @var ResourceConnection
*/
private $resource;

/**
* @var MetadataPool
*/
private $metadataPool;

/**
* StockStatus constructor.
* @param ResourceConnection $resource
* @param MetadataPool $metadataPool
*/
public function __construct(ResourceConnection $resource, MetadataPool $metadataPool)
{
$this->resource = $resource;
$this->metadataPool = $metadataPool;
}

/**
* @var array
*/
private $allChildOutOfStockInfo = [];

/**
* @inheritdoc
*/
public function isAllChildOutOfStock(int $productId): bool
{
if (isset($this->allChildOutOfStockInfo[$productId])) {
return $this->allChildOutOfStockInfo[$productId];
}

$statuses = $this->getAllChildStockInfo($productId);
$isAllChildOutOfStock = true;
foreach ($statuses as $status) {
if ($status == CatalogInventoryStockStatusInterface::STATUS_IN_STOCK) {
$isAllChildOutOfStock = false;
break;
}
}

$this->allChildOutOfStockInfo[$productId] = $isAllChildOutOfStock;

return $this->allChildOutOfStockInfo[$productId];
}

/**
* @return AdapterInterface
*/
protected function getConnection(): AdapterInterface
davidverholen marked this conversation as resolved.
Show resolved Hide resolved
{
return $this->resource->getConnection();
}

/**
* @param int $productId
* @return array
* @throws \Exception
*/
private function getAllChildStockInfo(int $productId): array
{
$linkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField();
$productTable = $this->resource->getTableName('catalog_product_entity');
$productRelationTable = $this->resource->getTableName('catalog_product_relation');

$select = $this->getConnection()->select()
->from(['parent' => $productTable], '', [])
->joinInner(
['link' => $productRelationTable],
"link.parent_id = parent.$linkField",
['id' => 'child_id']
)->joinInner(
['stock' => $this->resource->getTableName('cataloginventory_stock_status')],
'stock.product_id = link.child_id',
['stock_status']
)->where(sprintf('parent.%s = ?', $linkField), $productId);

return $this->getConnection()->fetchPairs($select);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable;

/**
* Interface StockStatusInterface
*/
interface StockStatusInterface
{
/**
* @param int $productId
* @return bool
* @throws \Exception
*/
public function isAllChildOutOfStock(int $productId): bool;
}
Loading