|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\Catalog\Model\Product\Attribute\Backend\TierPrice; |
| 9 | + |
| 10 | +use Magento\Framework\EntityManager\Operation\ExtensionInterface; |
| 11 | +use Magento\Store\Model\StoreManagerInterface; |
| 12 | +use Magento\Catalog\Api\ProductAttributeRepositoryInterface; |
| 13 | +use Magento\Catalog\Api\Data\ProductInterface; |
| 14 | +use Magento\Customer\Api\GroupManagementInterface; |
| 15 | +use Magento\Framework\EntityManager\MetadataPool; |
| 16 | +use Magento\Catalog\Model\ResourceModel\Product\Attribute\Backend\Tierprice; |
| 17 | + |
| 18 | +/** |
| 19 | + * Process tier price data for handled new product |
| 20 | + */ |
| 21 | +class SaveHandler implements ExtensionInterface |
| 22 | +{ |
| 23 | + /** |
| 24 | + * @var \Magento\Store\Model\StoreManagerInterface |
| 25 | + */ |
| 26 | + private $storeManager; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var \Magento\Catalog\Api\ProductAttributeRepositoryInterface |
| 30 | + */ |
| 31 | + private $attributeRepository; |
| 32 | + |
| 33 | + /** |
| 34 | + * @var \Magento\Customer\Api\GroupManagementInterface |
| 35 | + */ |
| 36 | + private $groupManagement; |
| 37 | + |
| 38 | + /** |
| 39 | + * @var \Magento\Framework\EntityManager\MetadataPool |
| 40 | + */ |
| 41 | + private $metadataPoll; |
| 42 | + |
| 43 | + /** |
| 44 | + * @var \Magento\Catalog\Model\ResourceModel\Product\Attribute\Backend\Tierprice |
| 45 | + */ |
| 46 | + private $tierPriceResource; |
| 47 | + |
| 48 | + /** |
| 49 | + * @param \Magento\Store\Model\StoreManagerInterface $storeManager |
| 50 | + * @param \Magento\Catalog\Api\ProductAttributeRepositoryInterface $attributeRepository |
| 51 | + * @param \Magento\Customer\Api\GroupManagementInterface $groupManagement |
| 52 | + * @param \Magento\Framework\EntityManager\MetadataPool $metadataPool |
| 53 | + * @param \Magento\Catalog\Model\ResourceModel\Product\Attribute\Backend\Tierprice $tierPriceResource |
| 54 | + */ |
| 55 | + public function __construct( |
| 56 | + StoreManagerInterface $storeManager, |
| 57 | + ProductAttributeRepositoryInterface $attributeRepository, |
| 58 | + GroupManagementInterface $groupManagement, |
| 59 | + MetadataPool $metadataPool, |
| 60 | + Tierprice $tierPriceResource |
| 61 | + ) { |
| 62 | + $this->storeManager = $storeManager; |
| 63 | + $this->attributeRepository = $attributeRepository; |
| 64 | + $this->groupManagement = $groupManagement; |
| 65 | + $this->metadataPoll = $metadataPool; |
| 66 | + $this->tierPriceResource = $tierPriceResource; |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Set tier price data for product entity |
| 71 | + * |
| 72 | + * @param \Magento\Catalog\Api\Data\ProductInterface|object $entity |
| 73 | + * @param array $arguments |
| 74 | + * @return \Magento\Catalog\Api\Data\ProductInterface|object |
| 75 | + * @throws \Magento\Framework\Exception\NoSuchEntityException |
| 76 | + * @throws \Magento\Framework\Exception\LocalizedException |
| 77 | + * @throws \Magento\Framework\Exception\InputException |
| 78 | + * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
| 79 | + */ |
| 80 | + public function execute($entity, $arguments = []) |
| 81 | + { |
| 82 | + $attribute = $this->attributeRepository->get('tier_price'); |
| 83 | + $priceRows = $entity->getData($attribute->getName()); |
| 84 | + if (null !== $priceRows) { |
| 85 | + if (!is_array($priceRows)) { |
| 86 | + throw new \Magento\Framework\Exception\InputException( |
| 87 | + __('Tier prices data should be array, but actually other type is received') |
| 88 | + ); |
| 89 | + } |
| 90 | + $websiteId = $this->storeManager->getStore($entity->getStoreId())->getWebsiteId(); |
| 91 | + $isGlobal = $attribute->isScopeGlobal() || $websiteId === 0; |
| 92 | + $identifierField = $this->metadataPoll->getMetadata(ProductInterface::class)->getLinkField(); |
| 93 | + $priceRows = array_filter($priceRows); |
| 94 | + $productId = (int) $entity->getData($identifierField); |
| 95 | + |
| 96 | + // prepare and save data |
| 97 | + foreach ($priceRows as $data) { |
| 98 | + $isPriceWebsiteGlobal = (int)$data['website_id'] === 0; |
| 99 | + if ($isGlobal === $isPriceWebsiteGlobal |
| 100 | + || !empty($data['price_qty']) |
| 101 | + || isset($data['cust_group']) |
| 102 | + ) { |
| 103 | + $tierPrice = $this->prepareTierPrice($data); |
| 104 | + $price = new \Magento\Framework\DataObject($tierPrice); |
| 105 | + $price->setData( |
| 106 | + $identifierField, |
| 107 | + $productId |
| 108 | + ); |
| 109 | + $this->tierPriceResource->savePriceData($price); |
| 110 | + $valueChangedKey = $attribute->getName() . '_changed'; |
| 111 | + $entity->setData($valueChangedKey, 1); |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + return $entity; |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Get additional tier price fields |
| 121 | + * |
| 122 | + * @param array $objectArray |
| 123 | + * @return array |
| 124 | + */ |
| 125 | + private function getAdditionalFields(array $objectArray): array |
| 126 | + { |
| 127 | + $percentageValue = $this->getPercentage($objectArray); |
| 128 | + return [ |
| 129 | + 'value' => $percentageValue ? null : $objectArray['price'], |
| 130 | + 'percentage_value' => $percentageValue ?: null, |
| 131 | + ]; |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Check whether price has percentage value. |
| 136 | + * |
| 137 | + * @param array $priceRow |
| 138 | + * @return integer|null |
| 139 | + */ |
| 140 | + private function getPercentage(array $priceRow): ?int |
| 141 | + { |
| 142 | + return isset($priceRow['percentage_value']) && is_numeric($priceRow['percentage_value']) |
| 143 | + ? (int)$priceRow['percentage_value'] |
| 144 | + : null; |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Prepare tier price data by provided price row data |
| 149 | + * |
| 150 | + * @param array $data |
| 151 | + * @return array |
| 152 | + * @throws \Magento\Framework\Exception\LocalizedException |
| 153 | + */ |
| 154 | + private function prepareTierPrice(array $data): array |
| 155 | + { |
| 156 | + $useForAllGroups = (int)$data['cust_group'] === $this->groupManagement->getAllCustomersGroup()->getId(); |
| 157 | + $customerGroupId = $useForAllGroups ? 0 : $data['cust_group']; |
| 158 | + $tierPrice = array_merge( |
| 159 | + $this->getAdditionalFields($data), |
| 160 | + [ |
| 161 | + 'website_id' => $data['website_id'], |
| 162 | + 'all_groups' => (int)$useForAllGroups, |
| 163 | + 'customer_group_id' => $customerGroupId, |
| 164 | + 'value' => $data['price'] ?? null, |
| 165 | + 'qty' => (int)$data['price_qty'] |
| 166 | + ] |
| 167 | + ); |
| 168 | + |
| 169 | + return $tierPrice; |
| 170 | + } |
| 171 | +} |
0 commit comments