-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/2.2-develop' into MAGETWO-75086
# Conflicts: # app/code/Magento/Catalog/Test/Mftf/ActionGroup/AdminCategoryProductAttributeActionGroup.xml # app/code/Magento/Catalog/Test/Mftf/ActionGroup/ProductsOnAdminActionGroup.xml # app/code/Magento/Catalog/Test/Mftf/Section/AdminProductAttributeEditSection.xml # app/code/Magento/Catalog/Test/Mftf/Section/AdminProductGridFilterSection.xml
- Loading branch information
Showing
88 changed files
with
2,315 additions
and
393 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
168 changes: 168 additions & 0 deletions
168
app/code/Magento/Catalog/Model/Product/Attribute/Backend/TierPrice/SaveHandler.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
namespace Magento\Catalog\Model\Product\Attribute\Backend\TierPrice; | ||
|
||
use Magento\Framework\EntityManager\Operation\ExtensionInterface; | ||
use Magento\Store\Model\StoreManagerInterface; | ||
use Magento\Catalog\Api\ProductAttributeRepositoryInterface; | ||
use Magento\Catalog\Api\Data\ProductInterface; | ||
use Magento\Customer\Api\GroupManagementInterface; | ||
use Magento\Framework\EntityManager\MetadataPool; | ||
use Magento\Catalog\Model\ResourceModel\Product\Attribute\Backend\Tierprice; | ||
|
||
/** | ||
* Process tier price data for handled new product | ||
*/ | ||
class SaveHandler implements ExtensionInterface | ||
{ | ||
/** | ||
* @var \Magento\Store\Model\StoreManagerInterface | ||
*/ | ||
private $storeManager; | ||
|
||
/** | ||
* @var \Magento\Catalog\Api\ProductAttributeRepositoryInterface | ||
*/ | ||
private $attributeRepository; | ||
|
||
/** | ||
* @var \Magento\Customer\Api\GroupManagementInterface | ||
*/ | ||
private $groupManagement; | ||
|
||
/** | ||
* @var \Magento\Framework\EntityManager\MetadataPool | ||
*/ | ||
private $metadataPoll; | ||
|
||
/** | ||
* @var \Magento\Catalog\Model\ResourceModel\Product\Attribute\Backend\Tierprice | ||
*/ | ||
private $tierPriceResource; | ||
|
||
/** | ||
* @param \Magento\Store\Model\StoreManagerInterface $storeManager | ||
* @param \Magento\Catalog\Api\ProductAttributeRepositoryInterface $attributeRepository | ||
* @param \Magento\Customer\Api\GroupManagementInterface $groupManagement | ||
* @param \Magento\Framework\EntityManager\MetadataPool $metadataPool | ||
* @param \Magento\Catalog\Model\ResourceModel\Product\Attribute\Backend\Tierprice $tierPriceResource | ||
*/ | ||
public function __construct( | ||
StoreManagerInterface $storeManager, | ||
ProductAttributeRepositoryInterface $attributeRepository, | ||
GroupManagementInterface $groupManagement, | ||
MetadataPool $metadataPool, | ||
Tierprice $tierPriceResource | ||
) { | ||
$this->storeManager = $storeManager; | ||
$this->attributeRepository = $attributeRepository; | ||
$this->groupManagement = $groupManagement; | ||
$this->metadataPoll = $metadataPool; | ||
$this->tierPriceResource = $tierPriceResource; | ||
} | ||
|
||
/** | ||
* Set tier price data for product entity | ||
* | ||
* @param \Magento\Catalog\Api\Data\ProductInterface|object $entity | ||
* @param array $arguments | ||
* @return \Magento\Catalog\Api\Data\ProductInterface|object | ||
* @throws \Magento\Framework\Exception\NoSuchEntityException | ||
* @throws \Magento\Framework\Exception\LocalizedException | ||
* @throws \Magento\Framework\Exception\InputException | ||
* @SuppressWarnings(PHPMD.UnusedFormalParameter) | ||
*/ | ||
public function execute($entity, $arguments = []) | ||
{ | ||
$attribute = $this->attributeRepository->get('tier_price'); | ||
$priceRows = $entity->getData($attribute->getName()); | ||
if (null !== $priceRows) { | ||
if (!is_array($priceRows)) { | ||
throw new \Magento\Framework\Exception\InputException( | ||
__('Tier prices data should be array, but actually other type is received') | ||
); | ||
} | ||
$websiteId = $this->storeManager->getStore($entity->getStoreId())->getWebsiteId(); | ||
$isGlobal = $attribute->isScopeGlobal() || $websiteId === 0; | ||
$identifierField = $this->metadataPoll->getMetadata(ProductInterface::class)->getLinkField(); | ||
$priceRows = array_filter($priceRows); | ||
$productId = $entity->getData($identifierField); | ||
|
||
// prepare and save data | ||
foreach ($priceRows as $data) { | ||
$isPriceWebsiteGlobal = (int)$data['website_id'] === 0; | ||
if ($isGlobal === $isPriceWebsiteGlobal | ||
|| !empty($data['price_qty']) | ||
|| isset($data['cust_group']) | ||
) { | ||
$tierPrice = $this->prepareTierPrice($data); | ||
$price = new \Magento\Framework\DataObject($tierPrice); | ||
$price->setData( | ||
$identifierField, | ||
$productId | ||
); | ||
$this->tierPriceResource->savePriceData($price); | ||
$valueChangedKey = $attribute->getName() . '_changed'; | ||
$entity->setData($valueChangedKey, 1); | ||
} | ||
} | ||
} | ||
|
||
return $entity; | ||
} | ||
|
||
/** | ||
* Get additional tier price fields | ||
* | ||
* @return array | ||
*/ | ||
private function getAdditionalFields(array $objectArray): array | ||
{ | ||
$percentageValue = $this->getPercentage($objectArray); | ||
return [ | ||
'value' => $percentageValue ? null : $objectArray['price'], | ||
'percentage_value' => $percentageValue ?: null, | ||
]; | ||
} | ||
|
||
/** | ||
* Check whether price has percentage value. | ||
* | ||
* @param array $priceRow | ||
* @return integer|null | ||
*/ | ||
private function getPercentage(array $priceRow) | ||
{ | ||
return isset($priceRow['percentage_value']) && is_numeric($priceRow['percentage_value']) | ||
? (int)$priceRow['percentage_value'] | ||
: null; | ||
} | ||
|
||
/** | ||
* Prepare tier price data by provided price row data | ||
* | ||
* @param array $data | ||
* @return array | ||
* @throws \Magento\Framework\Exception\LocalizedException | ||
*/ | ||
private function prepareTierPrice(array $data): array | ||
{ | ||
$useForAllGroups = (int)$data['cust_group'] === $this->groupManagement->getAllCustomersGroup()->getId(); | ||
$customerGroupId = $useForAllGroups ? 0 : $data['cust_group']; | ||
$tierPrice = array_merge( | ||
$this->getAdditionalFields($data), | ||
[ | ||
'website_id' => $data['website_id'], | ||
'all_groups' => (int)$useForAllGroups, | ||
'customer_group_id' => $customerGroupId, | ||
'value' => $data['price'] ?? null, | ||
'qty' => (int)$data['price_qty'] | ||
] | ||
); | ||
|
||
return $tierPrice; | ||
} | ||
} |
Oops, something went wrong.