Skip to content

Commit

Permalink
Merge branch 'mainline_develop' into api_coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
irenelagno committed Apr 14, 2017
2 parents 6e2fa56 + 6fe5623 commit b75a39d
Show file tree
Hide file tree
Showing 22 changed files with 786 additions and 194 deletions.
14 changes: 11 additions & 3 deletions app/code/Magento/Catalog/Model/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -1617,7 +1617,15 @@ public function setIsDuplicable($value)
*/
public function isSalable()
{
if ($this->hasData('salable') && !$this->_catalogProduct->getSkipSaleableCheck()) {
if ($this->_catalogProduct->getSkipSaleableCheck()) {
return true;
}
if (($this->getOrigData('status') != $this->getData('status'))
|| $this->isStockStatusChanged()) {
$this->unsetData('salable');
}

if ($this->hasData('salable')) {
return $this->getData('salable');
}
$this->_eventManager->dispatch('catalog_product_is_salable_before', ['product' => $this]);
Expand All @@ -1630,7 +1638,7 @@ public function isSalable()
['product' => $this, 'salable' => $object]
);
$this->setData('salable', $object->getIsSalable());
return $object->getIsSalable();
return $this->getData('salable');
}

/**
Expand All @@ -1640,7 +1648,7 @@ public function isSalable()
*/
public function isAvailable()
{
return $this->getTypeInstance()->isSalable($this) || $this->_catalogProduct->getSkipSaleableCheck();
return $this->_catalogProduct->getSkipSaleableCheck() || $this->getTypeInstance()->isSalable($this);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,11 @@ class Full
*/
private $iteratorFactory;

/**
* @var \Magento\Framework\EntityManager\MetadataPool
*/
private $metadataPool;

/**
* @param ResourceConnection $resource
* @param \Magento\Catalog\Model\Product\Type $catalogProductType
Expand All @@ -175,6 +180,7 @@ class Full
* @param \Magento\Framework\Search\Request\DimensionFactory $dimensionFactory
* @param \Magento\Framework\Indexer\ConfigInterface $indexerConfig
* @param \Magento\CatalogSearch\Model\Indexer\Fulltext\Action\IndexIteratorFactory $indexIteratorFactory
* @param \Magento\Framework\EntityManager\MetadataPool $metadataPool
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
Expand All @@ -195,7 +201,8 @@ public function __construct(
\Magento\CatalogSearch\Model\ResourceModel\Fulltext $fulltextResource,
\Magento\Framework\Search\Request\DimensionFactory $dimensionFactory,
\Magento\Framework\Indexer\ConfigInterface $indexerConfig,
\Magento\CatalogSearch\Model\Indexer\Fulltext\Action\IndexIteratorFactory $indexIteratorFactory
\Magento\CatalogSearch\Model\Indexer\Fulltext\Action\IndexIteratorFactory $indexIteratorFactory,
\Magento\Framework\EntityManager\MetadataPool $metadataPool = null
) {
$this->resource = $resource;
$this->connection = $resource->getConnection();
Expand All @@ -216,6 +223,8 @@ public function __construct(
$this->fulltextResource = $fulltextResource;
$this->dimensionFactory = $dimensionFactory;
$this->iteratorFactory = $indexIteratorFactory;
$this->metadataPool = $metadataPool ?: \Magento\Framework\App\ObjectManager::getInstance()
->get(\Magento\Framework\EntityManager\MetadataPool::class);
}

/**
Expand Down Expand Up @@ -252,14 +261,22 @@ protected function getTable($table)
*/
protected function getProductIdsFromParents(array $entityIds)
{
return $this->connection
/** @var \Magento\Framework\EntityManager\EntityMetadataInterface $metadata */
$metadata = $this->metadataPool->getMetadata(\Magento\Catalog\Api\Data\ProductInterface::class);
$fieldForParent = $metadata->getLinkField();

$select = $this->connection
->select()
->from($this->getTable('catalog_product_relation'), 'parent_id')
->from(['relation' => $this->getTable('catalog_product_relation')], [])
->distinct(true)
->where('child_id IN (?)', $entityIds)
->where('parent_id NOT IN (?)', $entityIds)
->query()
->fetchAll(\Zend_Db::FETCH_COLUMN);
->join(
['cpe' => $this->getTable('catalog_product_entity')],
'relation.parent_id = cpe.' . $fieldForParent,
['cpe.entity_id']
);
return $this->connection->fetchCol($select);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\ConfigurableProduct\Model\Product\Type\Collection;

use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Model\Product\Attribute\Source\Status;
use Magento\Catalog\Model\ResourceModel\Product\Collection;
use Magento\CatalogInventory\Model\ResourceModel\Stock\StatusFactory;

/**
* This class is responsible for adding additional filters for products collection
* to check if the product from this collection available to buy.
*/
class SalableProcessor
{
/**
* @var StatusFactory
*/
private $stockStatusFactory;

/**
* @param StatusFactory $stockStatusFactory
*/
public function __construct(StatusFactory $stockStatusFactory)
{
$this->stockStatusFactory = $stockStatusFactory;
}

/**
* Adds filters to the collection to help determine if product is available for sale.
*
* This method adds several additional checks for a children products availability.
* Children products should be enabled and available in stock to be sold.
* It also adds the specific flag to the collection to prevent the case
* when filter already added and therefore may break the collection.
*
* @param Collection $collection
* @return Collection
*/
public function process(Collection $collection)
{
$collection->addAttributeToFilter(
ProductInterface::STATUS,
Status::STATUS_ENABLED
);

$stockFlag = 'has_stock_status_filter';
if (!$collection->hasFlag($stockFlag)) {
$stockStatusResource = $this->stockStatusFactory->create();
$stockStatusResource->addStockDataToCollection($collection, true);
$collection->setFlag($stockFlag, true);
}

return $collection;
}
}
Loading

0 comments on commit b75a39d

Please sign in to comment.