Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/2.2-develop' into MAGETWO-75086
Browse files Browse the repository at this point in the history
# 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
OlgaVasyltsun committed Jul 27, 2018
2 parents fdd90fd + c8c2791 commit 037b810
Show file tree
Hide file tree
Showing 88 changed files with 2,315 additions and 393 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
<actionGroup name="CreateBundleProductForTwoSimpleProducts">
<arguments>
<argument name="bundleProduct"/>
<argument name="simpleProductFirst"/>
<argument name="simpleProductSecond"/>
</arguments>
<fillField userInput="{{bundleProduct.sku}}" selector="{{AdminProductFormSection.productSku}}" stepKey="fillProductSku"/>
<fillField userInput="{{bundleProduct.name}}" selector="{{AdminProductFormSection.productName}}" stepKey="fillProductName"/>
Expand All @@ -19,7 +21,22 @@
<click selector="{{AdminProductFormBundleSection.AddOption}}" stepKey="clickAddOptionButton"/>
<fillField userInput="{{bundleProduct.option_title}}" selector="{{AdminProductFormBundleSection.OptionTitle}}" stepKey="fillOptionTitle"/>
<click selector="{{AdminProductFormBundleSection.AddProductsToOption}}" stepKey="clickAddProductsToOption"/>
<click selector="{{AdminProductFormBundleSection.selectAll}}" stepKey="tickSelectAllCheckbox"/>

<!-- Select First Simple -->
<click selector="{{AdminProductGridFilterSection.filters}}" stepKey="clickOnFiltersButton"/>
<conditionalClick selector="{{AdminProductGridFilterSection.clearAll}}" dependentSelector="{{AdminProductGridFilterSection.clearAll}}" visible="true" stepKey="clearFilters"/>
<fillField selector="{{AdminProductGridFilterSection.name}}" userInput="{{simpleProductFirst.name}}" stepKey="fillNameFilter"/>
<click selector="{{AdminProductGridFilterSection.applyFilters}}" stepKey="applyFilters"/>
<click selector="{{AdminProductFormBundleSection.firstRowCheckbox}}" stepKey="selectFirstSimple"/>

<!-- Select Second Simple -->
<click selector="{{AdminProductGridFilterSection.filters}}" stepKey="clickOnFiltersButton2"/>
<conditionalClick selector="{{AdminProductGridFilterSection.clearAll}}" dependentSelector="{{AdminProductGridFilterSection.clearAll}}" visible="true" stepKey="clearFilters2"/>
<fillField selector="{{AdminProductGridFilterSection.name}}" userInput="{{simpleProductSecond.name}}" stepKey="fillNameFilter2"/>
<click selector="{{AdminProductGridFilterSection.applyFilters}}" stepKey="applyFilters2"/>
<click selector="{{AdminProductFormBundleSection.firstRowCheckbox}}" stepKey="selectSecondSimple"/>

<!-- Add Selected Simples To Bundle -->
<click selector="{{AdminProductFormBundleSection.AddSelectedProducts}}" stepKey="clickAddSelectedProductsButton"/>
<fillField userInput="{{bundleProduct.default_quantity1}}" selector="{{AdminProductFormBundleSection.DefaultQuantity1}}" stepKey="fillDefaultQuantityForFirstProduct"/>
<fillField userInput="{{bundleProduct.default_quantity2}}" selector="{{AdminProductFormBundleSection.DefaultQuantity2}}" stepKey="fillDefaultQuantityForSecondProduct"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@
<!--Add Products to Option-->
<element name="selectAll" type="input" selector="//label[@data-bind='attr: {for: ko.uid}']"/>
<element name="AddSelectedProducts" type="button" selector="//span[contains(text(),'Add Selected Products')]/ancestor::button" timeout="30"/>
<element name="firstRowCheckbox" type="checkbox" selector="tr.data-row:nth-of-type(1) input.admin__control-checkbox[data-action='select-row']"/>
</section>
</sections>
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
<before>
<createData entity="_defaultCategory" stepKey="createPreReqCategory"/>
<createData entity="SimpleProduct" stepKey="createPreReqSimpleProduct1">
<requiredEntity createDataKey="createPreReqCategory"/>
<requiredEntity createDataKey="createPreReqCategory"/>
</createData>
<createData entity="SimpleProduct2" stepKey="createPreReqSimpleProduct2">
<requiredEntity createDataKey="createPreReqCategory"/>
<requiredEntity createDataKey="createPreReqCategory"/>
</createData>
</before>
<after>
Expand All @@ -45,6 +45,8 @@
<!--Step4. Fill in all data according to data set-->
<actionGroup ref="CreateBundleProductForTwoSimpleProducts" stepKey="CreateBundleProductForTwoSimpleProducts">
<argument name="bundleProduct" value="BundleProduct"/>
<argument name="simpleProductFirst" value="$$createPreReqSimpleProduct1$$"/>
<argument name="simpleProductSecond" value="$$createPreReqSimpleProduct2$$"/>
</actionGroup>

<!--Step5. Save product-->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
</annotations>
<before>
<actionGroup ref="LoginAsAdmin" stepKey="loginAsAdmin"/>
<createData entity="DefaultTaxConfig" stepKey="defaultTaxConfiguration"/>
<createData entity="WeeeConfigDisable" stepKey="disableFPT"/>
<createData entity="SimpleProduct3" stepKey="simpleProduct1"/>
<createData entity="SimpleProduct3" stepKey="simpleProduct2"/>
</before>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -375,118 +375,10 @@ protected function modifyPriceData($object, $data)
*
* @param \Magento\Catalog\Model\Product $object
* @return $this
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function afterSave($object)
{
$websiteId = $this->_storeManager->getStore($object->getStoreId())->getWebsiteId();
$isGlobal = $this->getAttribute()->isScopeGlobal() || $websiteId == 0;

$priceRows = $object->getData($this->getAttribute()->getName());
if (null === $priceRows) {
return $this;
}

$priceRows = array_filter((array)$priceRows);

$old = [];
$new = [];

// prepare original data for compare
$origPrices = $object->getOrigData($this->getAttribute()->getName());
if (!is_array($origPrices)) {
$origPrices = [];
}
foreach ($origPrices as $data) {
if ($data['website_id'] > 0 || $data['website_id'] == '0' && $isGlobal) {
$key = implode(
'-',
array_merge(
[$data['website_id'], $data['cust_group']],
$this->_getAdditionalUniqueFields($data)
)
);
$old[$key] = $data;
}
}

// prepare data for save
foreach ($priceRows as $data) {
$hasEmptyData = false;
foreach ($this->_getAdditionalUniqueFields($data) as $field) {
if (empty($field)) {
$hasEmptyData = true;
break;
}
}

if ($hasEmptyData || !isset($data['cust_group']) || !empty($data['delete'])) {
continue;
}
if ($this->getAttribute()->isScopeGlobal() && $data['website_id'] > 0) {
continue;
}
if (!$isGlobal && (int)$data['website_id'] == 0) {
continue;
}

$key = implode(
'-',
array_merge([$data['website_id'], $data['cust_group']], $this->_getAdditionalUniqueFields($data))
);

$useForAllGroups = $data['cust_group'] == $this->_groupManagement->getAllCustomersGroup()->getId();
$customerGroupId = !$useForAllGroups ? $data['cust_group'] : 0;
$new[$key] = array_merge(
$this->getAdditionalFields($data),
[
'website_id' => $data['website_id'],
'all_groups' => $useForAllGroups ? 1 : 0,
'customer_group_id' => $customerGroupId,
'value' => isset($data['price']) ? $data['price'] : null,
],
$this->_getAdditionalUniqueFields($data)
);
}

$delete = array_diff_key($old, $new);
$insert = array_diff_key($new, $old);
$update = array_intersect_key($new, $old);

$isChanged = false;
$productId = $object->getData($this->getMetadataPool()->getMetadata(ProductInterface::class)->getLinkField());

if (!empty($delete)) {
foreach ($delete as $data) {
$this->_getResource()->deletePriceData($productId, null, $data['price_id']);
$isChanged = true;
}
}

if (!empty($insert)) {
foreach ($insert as $data) {
$price = new \Magento\Framework\DataObject($data);
$price->setData(
$this->getMetadataPool()->getMetadata(ProductInterface::class)->getLinkField(),
$productId
);
$this->_getResource()->savePriceData($price);

$isChanged = true;
}
}

if (!empty($update)) {
$isChanged |= $this->updateValues($update, $old);
}

if ($isChanged) {
$valueChangedKey = $this->getAttribute()->getName() . '_changed';
$object->setData($valueChangedKey, 1);
}

return $this;
}

Expand Down
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;
}
}
Loading

0 comments on commit 037b810

Please sign in to comment.