Skip to content

Commit

Permalink
Merge pull request #221 from magmodules/release/1.18.1
Browse files Browse the repository at this point in the history
Release/1.18.1
  • Loading branch information
Marvin-Magmodules authored Apr 8, 2024
2 parents c7178e3 + c6cbd27 commit ffd4501
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 20 deletions.
36 changes: 36 additions & 0 deletions Helper/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
6 changes: 1 addition & 5 deletions Model/Collection/Products.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']]);
Expand Down
35 changes: 22 additions & 13 deletions Model/Generate.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -48,6 +49,10 @@ class Generate
* @var FeedHelper
*/
private $feedHelper;
/**
* @var TierPriceData
*/
private $tierPriceData;
/**
* @var Emulation
*/
Expand All @@ -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,
Expand All @@ -71,6 +77,7 @@ public function __construct(
ProductHelper $productHelper,
GeneralHelper $generalHelper,
FeedHelper $feedHelper,
TierPriceData $tierPriceData,
Emulation $appEmulation
) {
$this->productModel = $productModel;
Expand All @@ -79,6 +86,7 @@ public function __construct(
$this->sourceHelper = $sourceHelper;
$this->generalHelper = $generalHelper;
$this->feedHelper = $feedHelper;
$this->tierPriceData = $tierPriceData;
$this->appEmulation = $appEmulation;
}

Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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']);
}
}
}
3 changes: 3 additions & 0 deletions Plugin/CreditmemoSaveAfter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
93 changes: 93 additions & 0 deletions Service/Product/TierPriceData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php
/**
* Copyright © Magmodules.eu. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magmodules\Channable\Service\Product;

use Exception;
use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\ResourceModel\Product\Collection as ProductCollection;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\EntityManager\EntityMetadata;
use Magento\Framework\EntityManager\MetadataPool;

class TierPriceData
{

/**
* @var ResourceConnection
*/
private $resourceConnection;
/**
* @var EntityMetadata
*/
private $metadata;

/**
* @param ResourceConnection $resourceConnection
* @param MetadataPool $metadataPool
* @throws Exception
*/
public function __construct(
ResourceConnection $resourceConnection,
MetadataPool $metadataPool
) {
$this->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);
}
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<general>
<enable>0</enable>
<limit>250</limit>
<version>v1.18.0</version>
<version>v1.18.1</version>
</general>
<data>
<name_attribute>name</name_attribute>
Expand Down

0 comments on commit ffd4501

Please sign in to comment.