Skip to content

Commit

Permalink
Merge branch 'develop' into PR1-final
Browse files Browse the repository at this point in the history
# Conflicts:
#	composer.lock
  • Loading branch information
Vadim Zubovich committed Jan 28, 2016
2 parents 45800ea + 03ec736 commit 9f0e3d9
Show file tree
Hide file tree
Showing 225 changed files with 5,595 additions and 2,806 deletions.
35 changes: 23 additions & 12 deletions app/code/Magento/Bundle/Model/LinkManagement.php
Expand Up @@ -6,9 +6,11 @@
*/
namespace Magento\Bundle\Model;

use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Framework\Exception\CouldNotSaveException;
use Magento\Framework\Exception\InputException;
use Magento\Framework\Model\Entity\MetadataPool;

/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
Expand Down Expand Up @@ -45,14 +47,20 @@ class LinkManagement implements \Magento\Bundle\Api\ProductLinkManagementInterfa
*/
protected $dataObjectHelper;

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

/**
* @param ProductRepositoryInterface $productRepository
* @param \Magento\Bundle\Api\Data\LinkInterfaceFactory $linkFactory
* @param \Magento\Bundle\Model\ResourceModel\BundleFactory $bundleFactory
* @param \Magento\Bundle\Model\SelectionFactory $bundleSelection
* @param \Magento\Bundle\Model\ResourceModel\BundleFactory $bundleFactory
* @param \Magento\Bundle\Model\ResourceModel\Option\CollectionFactory $optionCollection
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param \Magento\Framework\Api\DataObjectHelper $dataObjectHelper
* @param MetadataPool $metadataPool
*/
public function __construct(
ProductRepositoryInterface $productRepository,
Expand All @@ -61,7 +69,8 @@ public function __construct(
\Magento\Bundle\Model\ResourceModel\BundleFactory $bundleFactory,
\Magento\Bundle\Model\ResourceModel\Option\CollectionFactory $optionCollection,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Framework\Api\DataObjectHelper $dataObjectHelper
\Magento\Framework\Api\DataObjectHelper $dataObjectHelper,
MetadataPool $metadataPool
) {
$this->productRepository = $productRepository;
$this->linkFactory = $linkFactory;
Expand All @@ -70,6 +79,7 @@ public function __construct(
$this->optionCollection = $optionCollection;
$this->storeManager = $storeManager;
$this->dataObjectHelper = $dataObjectHelper;
$this->metadataPool = $metadataPool;
}

/**
Expand Down Expand Up @@ -137,12 +147,12 @@ public function saveChild(
if (!$selectionModel->getId()) {
throw new InputException(__('Can not find product link with id "%1"', [$linkedProduct->getId()]));
}

$linkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField();
$selectionModel = $this->mapProductLinkToSelectionModel(
$selectionModel,
$linkedProduct,
$linkProductModel->getId(),
$product->getId()
$product->getData($linkField)
);

try {
Expand Down Expand Up @@ -221,9 +231,10 @@ public function addChild(
);
}

$linkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField();
/* @var $resource \Magento\Bundle\Model\ResourceModel\Bundle */
$resource = $this->bundleFactory->create();
$selections = $resource->getSelectionsData($product->getId());
$selections = $resource->getSelectionsData($product->getData($linkField));
/** @var \Magento\Catalog\Model\Product $linkProductModel */
$linkProductModel = $this->productRepository->get($linkedProduct->getSku());
if ($linkProductModel->isComposite()) {
Expand All @@ -232,7 +243,7 @@ public function addChild(
if ($selections) {
foreach ($selections as $selection) {
if ($selection['option_id'] == $optionId &&
$selection['product_id'] == $linkProductModel->getId()) {
$selection['product_id'] == $linkProductModel->getEntityId()) {
throw new CouldNotSaveException(
__(
'Child with specified sku: "%1" already assigned to product: "%2"',
Expand All @@ -242,19 +253,18 @@ public function addChild(
}
}
}

$selectionModel = $this->bundleSelection->create();
$selectionModel = $this->mapProductLinkToSelectionModel(
$selectionModel,
$linkedProduct,
$linkProductModel->getId(),
$product->getId()
$linkProductModel->getEntityId(),
$product->getData($linkField)
);
$selectionModel->setOptionId($optionId);

try {
$selectionModel->save();
$resource->addProductRelation($product->getId(), $linkProductModel->getId());
$resource->addProductRelation($product->getData($linkField), $linkProductModel->getEntityId());
} catch (\Exception $e) {
throw new CouldNotSaveException(__('Could not save child: "%1"', $e->getMessage()), $e);
}
Expand Down Expand Up @@ -293,10 +303,11 @@ public function removeChild($sku, $optionId, $childSku)
__('Requested bundle option product doesn\'t exist')
);
}
$linkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField();
/* @var $resource \Magento\Bundle\Model\ResourceModel\Bundle */
$resource = $this->bundleFactory->create();
$resource->dropAllUnneededSelections($product->getId(), $excludeSelectionIds);
$resource->removeProductRelations($product->getId(), array_unique($usedProductIds));
$resource->dropAllUnneededSelections($product->getData($linkField), $excludeSelectionIds);
$resource->removeProductRelations($product->getData($linkField), array_unique($usedProductIds));

return true;
}
Expand Down
2 changes: 1 addition & 1 deletion app/code/Magento/Bundle/Model/Plugin/Product.php
Expand Up @@ -32,7 +32,7 @@ public function afterGetIdentities(
CatalogProduct $product,
array $identities
) {
foreach ($this->type->getParentIdsByChild($product->getId()) as $parentId) {
foreach ($this->type->getParentIdsByChild($product->getEntityId()) as $parentId) {
$identities[] = CatalogProduct::CACHE_TAG . '_' . $parentId;
}
return $identities;
Expand Down
2 changes: 1 addition & 1 deletion app/code/Magento/Bundle/Model/Product/Type.php
Expand Up @@ -425,7 +425,7 @@ public function getOptionsCollection($product)
/** @var \Magento\Bundle\Model\ResourceModel\Option\Collection $optionsCollection */
$optionsCollection = $this->_bundleOption->create()
->getResourceCollection();
$optionsCollection->setProductIdFilter($product->getId());
$optionsCollection->setProductIdFilter($product->getEntityId());
$this->setStoreFilter($product->getStoreId(), $product);
$optionsCollection->setPositionOrder();
$storeId = $this->getStoreFilter($product);
Expand Down
49 changes: 23 additions & 26 deletions app/code/Magento/Bundle/Model/ResourceModel/Indexer/Price.php
Expand Up @@ -5,6 +5,8 @@
*/
namespace Magento\Bundle\Model\ResourceModel\Indexer;

use Magento\Catalog\Api\Data\ProductInterface;

/**
* Bundle products Price indexer resource model
*
Expand Down Expand Up @@ -132,7 +134,7 @@ protected function _prepareBundlePriceByType($priceType, $entityIds = null)
['customer_group_id']
);
$this->_addWebsiteJoinToSelect($select, true);
$this->_addProductWebsiteJoinToSelect($select, 'cw.website_id', 'e.entity_id');
$this->_addProductWebsiteJoinToSelect($select, 'cw.website_id', "e.entity_id");
$select->columns(
'website_id',
'cw'
Expand All @@ -155,9 +157,10 @@ protected function _prepareBundlePriceByType($priceType, $entityIds = null)
'=?',
\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED
);
$this->_addAttributeToSelect($select, 'status', 'e.entity_id', 'cs.store_id', $statusCond, true);
$linkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField();
$this->_addAttributeToSelect($select, 'status', "e.$linkField", 'cs.store_id', $statusCond, true);
if ($this->moduleManager->isEnabled('Magento_Tax')) {
$taxClassId = $this->_addAttributeToSelect($select, 'tax_class_id', 'e.entity_id', 'cs.store_id');
$taxClassId = $this->_addAttributeToSelect($select, 'tax_class_id', "e.$linkField", 'cs.store_id');
} else {
$taxClassId = new \Zend_Db_Expr('0');
}
Expand All @@ -171,12 +174,12 @@ protected function _prepareBundlePriceByType($priceType, $entityIds = null)
}

$priceTypeCond = $connection->quoteInto('=?', $priceType);
$this->_addAttributeToSelect($select, 'price_type', 'e.entity_id', 'cs.store_id', $priceTypeCond);
$this->_addAttributeToSelect($select, 'price_type', "e.$linkField", 'cs.store_id', $priceTypeCond);

$price = $this->_addAttributeToSelect($select, 'price', 'e.entity_id', 'cs.store_id');
$specialPrice = $this->_addAttributeToSelect($select, 'special_price', 'e.entity_id', 'cs.store_id');
$specialFrom = $this->_addAttributeToSelect($select, 'special_from_date', 'e.entity_id', 'cs.store_id');
$specialTo = $this->_addAttributeToSelect($select, 'special_to_date', 'e.entity_id', 'cs.store_id');
$price = $this->_addAttributeToSelect($select, 'price', "e.$linkField", 'cs.store_id');
$specialPrice = $this->_addAttributeToSelect($select, 'special_price', "e.$linkField", 'cs.store_id');
$specialFrom = $this->_addAttributeToSelect($select, 'special_from_date', "e.$linkField", 'cs.store_id');
$specialTo = $this->_addAttributeToSelect($select, 'special_to_date', "e.$linkField", 'cs.store_id');
$curentDate = new \Zend_Db_Expr('cwd.website_date');

$specialExpr = $connection->getCheckSql(
Expand Down Expand Up @@ -401,12 +404,17 @@ protected function _calculateBundleSelectionPrice($priceType)
);
}

$linkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField();
$select = $connection->select()->from(
['i' => $this->_getBundlePriceTable()],
['entity_id', 'customer_group_id', 'website_id']
)->join(
['parent_product' => $this->getTable('catalog_product_entity')],
'parent_product.entity_id = i.entity_id',
[]
)->join(
['bo' => $this->getTable('catalog_product_bundle_option')],
'bo.parent_id = i.entity_id',
"bo.parent_id = parent_product.$linkField",
['option_id']
)->join(
['bs' => $this->getTable('catalog_product_bundle_selection')],
Expand Down Expand Up @@ -476,14 +484,14 @@ protected function _prepareBundlePrice($entityIds = null)
protected function _prepareTierPriceIndex($entityIds = null)
{
$connection = $this->getConnection();

$linkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField();
// remove index by bundle products
$select = $connection->select()->from(
['i' => $this->_getTierPriceIndexTable()],
null
)->join(
['e' => $this->getTable('catalog_product_entity')],
'i.entity_id=e.entity_id',
"i.entity_id=e.$linkField",
[]
)->where(
'e.type_id=?',
Expand All @@ -492,13 +500,12 @@ protected function _prepareTierPriceIndex($entityIds = null)
$query = $select->deleteFromSelect('i');
$connection->query($query);

$productIdField = $this->getProductIdFieldName();
$select = $connection->select()->from(
['tp' => $this->getTable('catalog_product_entity_tier_price')],
[$productIdField]
[$linkField]
)->join(
['e' => $this->getTable('catalog_product_entity')],
"tp.{$productIdField} = e.{$productIdField}",
"tp.{$linkField} = e.{$linkField}",
[]
)->join(
['cg' => $this->getTable('customer_group')],
Expand All @@ -516,26 +523,16 @@ protected function _prepareTierPriceIndex($entityIds = null)
)->columns(
new \Zend_Db_Expr('MIN(tp.value)')
)->group(
["tp.{$productIdField}", 'cg.customer_group_id', 'cw.website_id']
["tp.{$linkField}", 'cg.customer_group_id', 'cw.website_id']
);

if (!empty($entityIds)) {
$select->where("tp.{$productIdField} IN(?)", $entityIds);
$select->where("tp.{$linkField} IN(?)", $entityIds);
}

$query = $select->insertFromSelect($this->_getTierPriceIndexTable());
$connection->query($query);

return $this;
}

/**
* @return string
*/
protected function getProductIdFieldName()
{
$table = $this->getTable('catalog_product_entity');
$indexList = $this->getConnection()->getIndexList($table);
return $indexList[$this->getConnection()->getPrimaryKeyName($table)]['COLUMNS_LIST'][0];
}
}
18 changes: 13 additions & 5 deletions app/code/Magento/Bundle/Model/ResourceModel/Indexer/Stock.php
Expand Up @@ -5,6 +5,8 @@
*/
namespace Magento\Bundle\Model\ResourceModel\Indexer;

use Magento\Catalog\Api\Data\ProductInterface;

/**
* Bundle Stock Status Indexer Resource Model
*
Expand Down Expand Up @@ -45,11 +47,17 @@ protected function _getBundleOptionTable()
protected function _prepareBundleOptionStockData($entityIds = null, $usePrimaryTable = false)
{
$this->_cleanBundleOptionStockData();
$linkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField();
$idxTable = $usePrimaryTable ? $this->getMainTable() : $this->getIdxTable();
$connection = $this->getConnection();
$select = $connection->select()->from(
['product' => $this->getTable('catalog_product_entity')],
['entity_id']
);
$select->join(
['bo' => $this->getTable('catalog_product_bundle_option')],
['parent_id']
"bo.parent_id = product.$linkField",
[]
);
$this->_addWebsiteJoinToSelect($select, false);
$status = new \Zend_Db_Expr(
Expand Down Expand Up @@ -77,13 +85,13 @@ protected function _prepareBundleOptionStockData($entityIds = null, $usePrimaryT
)->where(
'cw.website_id != 0'
)->group(
['bo.parent_id', 'cw.website_id', 'cis.stock_id', 'bo.option_id']
['product.entity_id', 'cw.website_id', 'cis.stock_id', 'bo.option_id']
)->columns(
['option_id' => 'bo.option_id', 'status' => $status]
);

if ($entityIds !== null) {
$select->where('bo.parent_id IN(?)', $entityIds);
$select->where('product.entity_id IN(?)', $entityIds);
}

// clone select for bundle product without required bundle options
Expand All @@ -110,7 +118,7 @@ protected function _prepareBundleOptionStockData($entityIds = null, $usePrimaryT
protected function _getStockStatusSelect($entityIds = null, $usePrimaryTable = false)
{
$this->_prepareBundleOptionStockData($entityIds, $usePrimaryTable);

$linkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField();
$connection = $this->getConnection();
$select = $connection->select()->from(
['e' => $this->getTable('catalog_product_entity')],
Expand Down Expand Up @@ -148,7 +156,7 @@ protected function _getStockStatusSelect($entityIds = null, $usePrimaryTable = f
'=?',
\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED
);
$this->_addAttributeToSelect($select, 'status', 'e.entity_id', 'cs.store_id', $condition);
$this->_addAttributeToSelect($select, 'status', "e.$linkField", 'cs.store_id', $condition);

if ($this->_isManageStock()) {
$statusExpr = $connection->getCheckSql(
Expand Down
19 changes: 18 additions & 1 deletion app/code/Magento/Bundle/Model/ResourceModel/Option.php
Expand Up @@ -5,6 +5,9 @@
*/
namespace Magento\Bundle\Model\ResourceModel;

use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Framework\Model\Entity\MetadataPool;

/**
* Bundle Option Resource Model
*
Expand All @@ -17,18 +20,26 @@ class Option extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
*/
private $validator;

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

/**
* @param \Magento\Framework\Model\ResourceModel\Db\Context $context
* @param \Magento\Bundle\Model\Option\Validator $validator
* @param MetadataPool $metadataPool
* @param string $connectionName
*/
public function __construct(
\Magento\Framework\Model\ResourceModel\Db\Context $context,
\Magento\Bundle\Model\Option\Validator $validator,
MetadataPool $metadataPool,
$connectionName = null
) {
parent::__construct($context, $connectionName);
$this->validator = $validator;
$this->metadataPool = $metadataPool;
}

/**
Expand Down Expand Up @@ -127,6 +138,7 @@ public function getSearchableData($productId, $storeId)
'option_title_default.title'
);
$bind = ['store_id' => $storeId, 'product_id' => $productId];
$linkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField();
$select = $connection->select()
->from(
['opt' => $this->getMainTable()],
Expand All @@ -142,8 +154,13 @@ public function getSearchableData($productId, $storeId)
'option_title_store.option_id = opt.option_id AND option_title_store.store_id = :store_id',
['title' => $title]
)
->join(
['e' => $this->getTable('catalog_product_entity')],
"e.$linkField = opt.parent_id",
[]
)
->where(
'opt.parent_id=:product_id'
'e.entity_id=:product_id'
);
if (!($searchData = $connection->fetchCol($select, $bind))) {
$searchData = [];
Expand Down

0 comments on commit 9f0e3d9

Please sign in to comment.