Skip to content

Commit

Permalink
Merge pull request #1368 from magento-tsg/2.1.8-develop-pr24
Browse files Browse the repository at this point in the history
[TSG] Backporting for 2.1 (pr24) (2.1.8)
  • Loading branch information
vzabaznov authored Jul 28, 2017
2 parents 0987a02 + 57a7aab commit 3302864
Show file tree
Hide file tree
Showing 68 changed files with 1,555 additions and 1,079 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,6 @@ class Helper
* @var \Magento\Framework\Stdlib\DateTime\Filter\DateTime
*/
private $dateTimeFilter;

/**
* @var \Magento\Catalog\Model\Product\LinkTypeProvider
*/
private $linkTypeProvider;

/**
* Helper constructor.
Expand All @@ -88,25 +83,21 @@ class Helper
* @param ProductLinks $productLinks
* @param \Magento\Backend\Helper\Js $jsHelper
* @param \Magento\Framework\Stdlib\DateTime\Filter\Date $dateFilter
* @param \Magento\Catalog\Model\Product\LinkTypeProvider $linkTypeProvider
*/
public function __construct(
\Magento\Framework\App\RequestInterface $request,
\Magento\Store\Model\StoreManagerInterface $storeManager,
StockDataFilter $stockFilter,
\Magento\Catalog\Model\Product\Initialization\Helper\ProductLinks $productLinks,
\Magento\Backend\Helper\Js $jsHelper,
\Magento\Framework\Stdlib\DateTime\Filter\Date $dateFilter,
\Magento\Catalog\Model\Product\LinkTypeProvider $linkTypeProvider = null
\Magento\Framework\Stdlib\DateTime\Filter\Date $dateFilter
) {
$this->request = $request;
$this->storeManager = $storeManager;
$this->stockFilter = $stockFilter;
$this->productLinks = $productLinks;
$this->jsHelper = $jsHelper;
$this->dateFilter = $dateFilter;
$this->linkTypeProvider = $linkTypeProvider ?: \Magento\Framework\App\ObjectManager::getInstance()
->get(\Magento\Catalog\Model\Product\LinkTypeProvider::class);
}

/**
Expand Down Expand Up @@ -208,18 +199,14 @@ public function initializeFromData(\Magento\Catalog\Model\Product $product, arra
$customOptions = [];
foreach ($options as $customOptionData) {
if (empty($customOptionData['is_delete'])) {
if (empty($customOptionData['option_id'])) {
$customOptionData['option_id'] = null;
}

if (isset($customOptionData['values'])) {
$customOptionData['values'] = array_filter($customOptionData['values'], function ($valueData) {
return empty($valueData['is_delete']);
});
}

$customOption = $this->getCustomOptionFactory()->create(['data' => $customOptionData]);
$customOption->setProductSku($product->getSku());
$customOption->setOptionId(null);
$customOptions[] = $customOption;
}
}
Expand Down Expand Up @@ -260,17 +247,11 @@ protected function setProductLinks(\Magento\Catalog\Model\Product $product)

$product = $this->productLinks->initializeLinks($product, $links);
$productLinks = $product->getProductLinks();
$linkTypes = [];

/** @var \Magento\Catalog\Api\Data\ProductLinkTypeInterface $linkTypeObject */
foreach ($this->linkTypeProvider->getItems() as $linkTypeObject) {
$linkTypes[$linkTypeObject->getName()] = $product->getData($linkTypeObject->getName() . '_readonly');
}

// skip linkTypes that were already processed on initializeLinks plugins
foreach ($productLinks as $productLink) {
unset($linkTypes[$productLink->getLinkType()]);
}
$linkTypes = [
'related' => $product->getRelatedReadonly(),
'upsell' => $product->getUpsellReadonly(),
'crosssell' => $product->getCrosssellReadonly()
];

foreach ($linkTypes as $linkType => $readonly) {
if (isset($links[$linkType]) && !$readonly) {
Expand Down Expand Up @@ -334,59 +315,21 @@ public function mergeProductOptions($productOptions, $overwriteOptions)
return $productOptions;
}

foreach ($productOptions as $optionIndex => $option) {
foreach ($productOptions as $index => $option) {
$optionId = $option['option_id'];
$option = $this->overwriteValue(
$optionId,
$option,
$overwriteOptions
);

if (isset($option['values']) && isset($overwriteOptions[$optionId]['values'])) {
foreach ($option['values'] as $valueIndex => $value) {
if (isset($value['option_type_id'])) {
$valueId = $value['option_type_id'];
$value = $this->overwriteValue(
$valueId,
$value,
$overwriteOptions[$optionId]['values']
);

$option['values'][$valueIndex] = $value;
}
}
if (!isset($overwriteOptions[$optionId])) {
continue;
}

$productOptions[$optionIndex] = $option;
}

return $productOptions;
}

/**
* Overwrite values of fields to default, if there are option id and field
* name in array overwriteOptions.
*
* @param int $optionId
* @param array $option
* @param array $overwriteOptions
*
* @return array
*/
private function overwriteValue($optionId, $option, $overwriteOptions)
{
if (isset($overwriteOptions[$optionId])) {
foreach ($overwriteOptions[$optionId] as $fieldName => $overwrite) {
if ($overwrite && isset($option[$fieldName]) && isset($option['default_' . $fieldName])) {
$option[$fieldName] = $option['default_' . $fieldName];
if ('title' == $fieldName) {
$option['is_delete_store_title'] = 1;
}
$productOptions[$index][$fieldName] = $option['default_' . $fieldName];
}
}
}

return $option;
return $productOptions;
}

/**
Expand All @@ -396,9 +339,8 @@ private function getCustomOptionFactory()
{
if (null === $this->customOptionFactory) {
$this->customOptionFactory = \Magento\Framework\App\ObjectManager::getInstance()
->get(\Magento\Catalog\Api\Data\ProductCustomOptionInterfaceFactory::class);
->get('Magento\Catalog\Api\Data\ProductCustomOptionInterfaceFactory');
}

return $this->customOptionFactory;
}

Expand All @@ -409,9 +351,8 @@ private function getProductLinkFactory()
{
if (null === $this->productLinkFactory) {
$this->productLinkFactory = \Magento\Framework\App\ObjectManager::getInstance()
->get(\Magento\Catalog\Api\Data\ProductLinkInterfaceFactory::class);
->get('Magento\Catalog\Api\Data\ProductLinkInterfaceFactory');
}

return $this->productLinkFactory;
}

Expand All @@ -422,9 +363,8 @@ private function getProductRepository()
{
if (null === $this->productRepository) {
$this->productRepository = \Magento\Framework\App\ObjectManager::getInstance()
->get('\Magento\Catalog\Api\ProductRepositoryInterface\Proxy');
->get('Magento\Catalog\Api\ProductRepositoryInterface\Proxy');
}

return $this->productRepository;
}

Expand All @@ -437,7 +377,6 @@ private function getLinkResolver()
if (!is_object($this->linkResolver)) {
$this->linkResolver = ObjectManager::getInstance()->get(LinkResolver::class);
}

return $this->linkResolver;
}

Expand All @@ -452,7 +391,6 @@ private function getDateTimeFilter()
$this->dateTimeFilter = \Magento\Framework\App\ObjectManager::getInstance()
->get(\Magento\Framework\Stdlib\DateTime\Filter\DateTime::class);
}

return $this->dateTimeFilter;
}
}
42 changes: 5 additions & 37 deletions app/code/Magento/Catalog/Model/Product/Option/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,47 +133,15 @@ public function duplicate(
*/
public function save(\Magento\Catalog\Api\Data\ProductCustomOptionInterface $option)
{
/** @var string $productSku */
$productSku = $option->getProductSku();
if (!$productSku) {
throw new CouldNotSaveException(__('ProductSku should be specified'));
}
/** @var \Magento\Catalog\Model\Product $product */
$product = $this->productRepository->get($productSku);
/** @var \Magento\Framework\EntityManager\EntityMetadataInterface $metadata */
$metadata = $this->getMetadataPool()->getMetadata(ProductInterface::class);
$option->setData('product_id', $product->getData($metadata->getLinkField()));
$option->setData('store_id', $product->getStoreId());

if ($option->getOptionId()) {
$options = $product->getOptions();
if (!$options) {
$options = $this->getProductOptions($product);
}
$persistedOption = array_filter(
$options,
function ($iOption) use ($option) {
return $option->getOptionId() == $iOption->getOptionId();
}
);
$persistedOption = reset($persistedOption);

if (!$persistedOption) {
throw new NoSuchEntityException();
}

/** @var array $originalValues */
$originalValues = $persistedOption->getValues();
/** @var array $newValues */
$newValues = $option->getData('values');
if ($newValues) {
$newValues = $this->markRemovedValues($newValues, $originalValues);
$option->setData('values', $newValues);
}
}

$option->setOptionId(null);
$option->save();

return $option;
}

Expand Down Expand Up @@ -235,7 +203,7 @@ private function getOptionFactory()
{
if (null === $this->optionFactory) {
$this->optionFactory = \Magento\Framework\App\ObjectManager::getInstance()
->get(\Magento\Catalog\Model\Product\OptionFactory::class);
->get('Magento\Catalog\Model\Product\OptionFactory');
}
return $this->optionFactory;
}
Expand All @@ -248,7 +216,7 @@ private function getCollectionFactory()
{
if (null === $this->collectionFactory) {
$this->collectionFactory = \Magento\Framework\App\ObjectManager::getInstance()
->get(\Magento\Catalog\Model\ResourceModel\Product\Option\CollectionFactory::class);
->get('Magento\Catalog\Model\ResourceModel\Product\Option\CollectionFactory');
}
return $this->collectionFactory;
}
Expand All @@ -261,7 +229,7 @@ private function getMetadataPool()
{
if (null === $this->metadataPool) {
$this->metadataPool = \Magento\Framework\App\ObjectManager::getInstance()
->get(\Magento\Framework\EntityManager\MetadataPool::class);
->get('Magento\Framework\EntityManager\MetadataPool');
}
return $this->metadataPool;
}
Expand All @@ -274,7 +242,7 @@ private function getHydratorPool()
{
if (null === $this->hydratorPool) {
$this->hydratorPool = \Magento\Framework\App\ObjectManager::getInstance()
->get(\Magento\Framework\EntityManager\HydratorPool::class);
->get('Magento\Framework\EntityManager\HydratorPool');
}
return $this->hydratorPool;
}
Expand Down
24 changes: 4 additions & 20 deletions app/code/Magento/Catalog/Model/Product/Option/SaveHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class SaveHandler implements ExtensionInterface

/**
* @param OptionRepository $optionRepository
* @param MetadataPool $metadataPool
*/
public function __construct(
OptionRepository $optionRepository
Expand All @@ -35,32 +36,15 @@ public function __construct(
*/
public function execute($entity, $arguments = [])
{
$options = $entity->getOptions();
$optionIds = [];

if ($options) {
$optionIds = array_map(
function ($option) {
/** @var \Magento\Catalog\Model\Product\Option $option */
return $option->getOptionId();
},
$options
);
}

/** @var \Magento\Catalog\Api\Data\ProductInterface $entity */
foreach ($this->optionRepository->getProductOptions($entity) as $option) {
if (!in_array($option->getOptionId(), $optionIds)) {
$this->optionRepository->delete($option);
}
$this->optionRepository->delete($option);
}

if ($options) {
foreach ($options as $option) {
if ($entity->getOptions()) {
foreach ($entity->getOptions() as $option) {
$this->optionRepository->save($option);
}
}

return $entity;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,6 @@ protected function validateOptionValue(Option $option)
$storeId = $option->getProduct()->getStoreId();
}
foreach ($values as $value) {
if (isset($value['is_delete']) && (bool)$value['is_delete']) {
continue;
}
$type = isset($value['price_type']) ? $value['price_type'] : null;
$price = isset($value['price']) ? $value['price'] : null;
$title = isset($value['title']) ? $value['title'] : null;
Expand Down
2 changes: 1 addition & 1 deletion app/code/Magento/Catalog/Model/Product/Option/Value.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ public function saveValues()
'store_id',
$this->getOption()->getStoreId()
);

$this->unsetData('option_type_id');
if ($this->getData('is_delete') == '1') {
if ($this->getId()) {
$this->deleteValues($this->getId());
Expand Down
Loading

0 comments on commit 3302864

Please sign in to comment.