diff --git a/Helper/Product.php b/Helper/Product.php index c96a828..290d1a7 100644 --- a/Helper/Product.php +++ b/Helper/Product.php @@ -335,6 +335,9 @@ public function getAttributeValue($type, $attribute, $config, $product, $simple) if (!empty($attribute['source']) && ($attribute['source'] == 'category_ids')) { $type = 'category_ids'; } + if (!empty($attribute['source']) && ($attribute['source'] == 'tier_price')) { + $type = 'tier_price'; + } switch ($type) { case 'link': @@ -372,6 +375,9 @@ public function getAttributeValue($type, $attribute, $config, $product, $simple) case 'category_ids': $value = $product->getCategoryIds(); break; + case 'tier_price': + $value = $this->processTierPrice($product, $config); + break; default: $value = $this->getValue($attribute, $product); break; @@ -1161,6 +1167,36 @@ public function getGroupedPrices($product, $config) return ['min_price' => $minPrice, 'max_price' => $maxPrice, 'total_price' => $totalPrice]; } + /** + * @param $product + * @param array $config + * @return array|null + */ + public function processTierPrice($product, array $config): ?array + { + if (!$product->getData('tier_price')) { + return null; + } + + $reformattedTierPriced = []; + foreach ($product->getData('tier_price') as $priceTier) { + $price = $priceTier['percentage_value'] + ? $product->getPrice() * ($priceTier['percentage_value'] / 100) + : $priceTier['value']; + + $reformattedTierPriced[] = [ + 'price_id' => $priceTier['value_id'], + 'website_id' => $priceTier['website_id'], + 'all_groups' => $priceTier['all_groups'], + 'cust_group' => $priceTier['customer_group_id'], + 'qty' => $priceTier['qty'], + 'price' => $this->formatPrice($price, $config) + ]; + } + + return $reformattedTierPriced; + } + /** * @param \Magento\Catalog\Model\Product $product * @param $price diff --git a/Model/Collection/Products.php b/Model/Collection/Products.php index 6317084..207a293 100755 --- a/Model/Collection/Products.php +++ b/Model/Collection/Products.php @@ -121,11 +121,7 @@ public function getCollection($config, $page, $productIds) ->addStoreFilter($config['store_id']) ->addAttributeToSelect($attributes) ->addUrlRewrite() - ->setOrder('product_website.product_id', 'ASC'); - - if (in_array('tier_price', $attributes)) { - $collection->addTierPriceData(); - } + ->setOrder('entity_id', 'ASC'); if (!empty($filters['visibility'])) { $collection->addAttributeToFilter('visibility', ['in' => $filters['visibility']]); diff --git a/Model/Generate.php b/Model/Generate.php index 456572e..a7657b8 100644 --- a/Model/Generate.php +++ b/Model/Generate.php @@ -8,15 +8,16 @@ use Magento\Catalog\Model\Product; use Magento\Catalog\Model\ResourceModel\Product\Collection as ProductCollection; +use Magento\Framework\App\Area; use Magento\Framework\Exception\LocalizedException; +use Magento\Store\Model\App\Emulation; +use Magmodules\Channable\Helper\Feed as FeedHelper; +use Magmodules\Channable\Helper\General as GeneralHelper; +use Magmodules\Channable\Helper\Product as ProductHelper; +use Magmodules\Channable\Helper\Source as SourceHelper; use Magmodules\Channable\Model\Collection\Products as ProductsModel; use Magmodules\Channable\Model\Item as ItemModel; -use Magmodules\Channable\Helper\Source as SourceHelper; -use Magmodules\Channable\Helper\Product as ProductHelper; -use Magmodules\Channable\Helper\General as GeneralHelper; -use Magmodules\Channable\Helper\Feed as FeedHelper; -use Magento\Framework\App\Area; -use Magento\Store\Model\App\Emulation; +use Magmodules\Channable\Service\Product\TierPriceData; class Generate { @@ -48,6 +49,10 @@ class Generate * @var FeedHelper */ private $feedHelper; + /** + * @var TierPriceData + */ + private $tierPriceData; /** * @var Emulation */ @@ -57,12 +62,13 @@ class Generate * Generate constructor. * * @param ProductsModel $productModel - * @param Item $itemModel - * @param SourceHelper $sourceHelper + * @param Item $itemModel + * @param SourceHelper $sourceHelper * @param ProductHelper $productHelper * @param GeneralHelper $generalHelper - * @param FeedHelper $feedHelper - * @param Emulation $appEmulation + * @param FeedHelper $feedHelper + * @param TierPriceData $tierPriceData + * @param Emulation $appEmulation */ public function __construct( ProductsModel $productModel, @@ -71,6 +77,7 @@ public function __construct( ProductHelper $productHelper, GeneralHelper $generalHelper, FeedHelper $feedHelper, + TierPriceData $tierPriceData, Emulation $appEmulation ) { $this->productModel = $productModel; @@ -79,6 +86,7 @@ public function __construct( $this->sourceHelper = $sourceHelper; $this->generalHelper = $generalHelper; $this->feedHelper = $feedHelper; + $this->tierPriceData = $tierPriceData; $this->appEmulation = $appEmulation; } @@ -121,7 +129,7 @@ public function generateByStore( $parentRelations = $this->productHelper->getParentsFromCollection($products, $config); $parents = $this->productModel->getParents($parentRelations, $config); - $this->prefetchData($products, $parents, $parentRelations, $config); + $this->prefetchData($products, $parents, $config); foreach ($products as $product) { /** @var Product $product */ @@ -218,17 +226,18 @@ public function getDataRow($product, $parent, $config): ?array * * @param ProductCollection $products * @param ProductCollection $parents - * @param array $parentRelations * @param array $config * @return void */ private function prefetchData( ProductCollection $products, ProductCollection $parents, - array $parentRelations, array $config ) { $this->productHelper->getInventoryData()->load($products->getColumnValues('sku'), $config); $this->productHelper->getMediaData()->load($products, $parents); + if (in_array('tier_price', array_column($config['attributes'], 'source'))) { + $this->tierPriceData->load($products, $parents, $config['website_id']); + } } } diff --git a/Plugin/CreditmemoSaveAfter.php b/Plugin/CreditmemoSaveAfter.php index 7865de5..9344bbb 100644 --- a/Plugin/CreditmemoSaveAfter.php +++ b/Plugin/CreditmemoSaveAfter.php @@ -89,6 +89,9 @@ public function afterSave( } foreach ($creditmemo->getItems() as $creditmemoItem) { + if (!$creditmemoItem->getQty()) { + continue; + } if (!array_key_exists($creditmemoItem->getSku(), $selectedReturns) && !$automate) { continue; } diff --git a/Service/Product/TierPriceData.php b/Service/Product/TierPriceData.php new file mode 100644 index 0000000..340579f --- /dev/null +++ b/Service/Product/TierPriceData.php @@ -0,0 +1,93 @@ +resourceConnection = $resourceConnection; + $this->metadata = $metadataPool->getMetadata(ProductInterface::class); + } + + /** + * @param ProductCollection $products + * @param ProductCollection $parents + * @return void + */ + public function load(ProductCollection $products, ProductCollection $parents, $websiteId) + { + /** @var Product $product */ + $product = $products->getFirstItem(); + + if ($product->isEmpty()) { + return; + } + + $productIds = array_merge( + $products->getColumnValues('entity_id'), + $parents->getColumnValues('entity_id') + ); + + $connection = $this->resourceConnection->getConnection(); + $tableName = $this->resourceConnection->getTableName('catalog_product_entity_tier_price'); + + $select = $connection->select() + ->from($tableName) + ->where('website_id IN (?)', [0, $websiteId]) + ->where($this->metadata->getLinkField() . ' IN (?)', $productIds); + + $result = $connection->fetchAll($select); + + $tierPriceData = []; + foreach ($result as $record) { + $tierPriceData[$record[$this->metadata->getLinkField()]][] = $record; + } + + $this->addTierPriceDataToCollection($products, $tierPriceData); + $this->addTierPriceDataToCollection($parents, $tierPriceData); + } + + /** + * @param ProductCollection $productCollection + * @param array $tierPriceData + * @return void + */ + private function addTierPriceDataToCollection(ProductCollection $productCollection, array $tierPriceData) + { + foreach ($productCollection as $product) { + $tierPrice = $tierPriceData[$product->getData($this->metadata->getLinkField())] ?? []; + $product->setData('tier_price', $tierPrice); + } + } +} \ No newline at end of file diff --git a/composer.json b/composer.json index 39f4246..524389a 100755 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "magmodules/magento2-channable", "description": "Channable integration for Magento 2", "type": "magento2-module", - "version": "1.18.0", + "version": "1.18.1", "license": "BSD-2-Clause", "homepage": "https://github.com/magmodules/magento2-channable", "require": { diff --git a/etc/config.xml b/etc/config.xml index 3eca57c..b4f2519 100644 --- a/etc/config.xml +++ b/etc/config.xml @@ -12,7 +12,7 @@ 0 250 - v1.18.0 + v1.18.1 name