Skip to content

Commit

Permalink
Merge pull request #217 from magmodules/release/1.17.0
Browse files Browse the repository at this point in the history
Release/1.17.0
  • Loading branch information
Marvin-Magmodules committed Feb 27, 2024
2 parents 3903fdf + 4c4ae93 commit d9665a8
Show file tree
Hide file tree
Showing 25 changed files with 602 additions and 126 deletions.
10 changes: 10 additions & 0 deletions Api/Config/System/OrderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ interface OrderInterface extends ReturnsInterface
const XML_PATH_LVB_SKIP_STOCK = 'magmodules_channable_marketplace/order/lvb_stock';
const XML_PATH_LVB_AUTO_SHIP = 'magmodules_channable_marketplace/order/lvb_ship';
const XML_PATH_DEDUCT_FPT = 'magmodules_channable_marketplace/order/deduct_fpt';
const XML_PATH_BUSINESS_ORDER = 'magmodules_channable_marketplace/order/business_order';
const XML_PATH_TRANSACTION_FEE = 'magmodules_channable_marketplace/order/transaction_fee';
const XML_PATH_LOG = 'magmodules_channable_marketplace/order/log';
const XML_PATH_CARRIER_TITLE = 'carriers/channable/title';
Expand Down Expand Up @@ -285,6 +286,15 @@ public function autoShipLvbOrders(int $storeId = null): bool;
*/
public function deductFptTax(int $storeId = null): bool;

/**
* Check if business orders enabled.
*
* @param null|int $storeId
*
* @return bool
*/
public function isBusinessOrderEnabled(int $storeId = null): bool;

/**
* Check if need to add transaction fee
*
Expand Down
48 changes: 48 additions & 0 deletions Api/Returns/Data/DataInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ interface DataInterface extends ExtensibleDataInterface
public const REASON = 'reason';
public const COMMENT = 'comment';
public const STATUS = 'status';
public const CHANNEL_RETURN_ID = 'channel_return_id';
public const CHANNEL_ORDER_ID = 'channel_order_id';
public const CHANNEL_ORDER_ID_INTERNAL = 'channel_order_id_internal';
public const PLATFORM_ORDER_ID = 'platform_order_id';
public const CREATED_AT = 'created_at';
public const UPDATED_AT = 'updated_at';

Expand Down Expand Up @@ -217,6 +221,50 @@ public function getStatus(): string;
*/
public function setStatus(string $status): self;

/**
* @return string|null
*/
public function getChannelReturnId(): ?string;

/**
* @param string|null $channelReturnId
* @return $this
*/
public function setChannelReturnId(?string $channelReturnId): self;

/**
* @return string|null
*/
public function getChannelOrderId(): ?string;

/**
* @param string|null $channelOrderId
* @return $this
*/
public function setChannelOrderId(?string $channelOrderId): self;

/**
* @return string|null
*/
public function getChannelOrderIdInternal(): ?string;

/**
* @param string|null $channelOrderIdInternal
* @return $this
*/
public function setChannelOrderIdInternal(?string $channelOrderIdInternal): self;

/**
* @return string|null
*/
public function getPlatformOrderId(): ?string;

/**
* @param string|null $platformOrderId
* @return $this
*/
public function setPlatformOrderId(?string $platformOrderId): self;

/**
* @return string
*/
Expand Down
47 changes: 44 additions & 3 deletions Helper/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Magento\Bundle\Model\ResourceModel\Selection as BundleResource;
use Magmodules\Channable\Api\Log\RepositoryInterface as LogRepository;
use Magmodules\Channable\Service\Product\InventoryData;
use Magmodules\Channable\Service\Product\MediaData;

/**
* Class Product
Expand Down Expand Up @@ -84,6 +85,12 @@ class Product extends AbstractHelper
* @var CatalogPrice
*/
private $commonPriceModel;

/**
* @var MediaData
*/
private $mediaData;

/**
* @var LogRepository
*/
Expand Down Expand Up @@ -128,6 +135,7 @@ public function __construct(
ConfigurableResource $catalogProductTypeConfigurable,
CatalogPrice $commonPriceModel,
InventoryData $inventoryData,
MediaData $mediaData,
LogRepository $logger
) {
$this->galleryReadHandler = $galleryReadHandler;
Expand All @@ -143,6 +151,7 @@ public function __construct(
$this->catalogProductTypeBundle = $catalogProductTypeBundle;
$this->commonPriceModel = $commonPriceModel;
$this->inventoryData = $inventoryData;
$this->mediaData = $mediaData;
$this->logger = $logger;
parent::__construct($context);
}
Expand Down Expand Up @@ -254,6 +263,11 @@ public function validateProduct($product, $parent, $config)
}
}

$visibilityFilter = $config['filters']['visibility'] ?? [];
if (!empty($visibilityFilter) && in_array($product->getVisibility(), $visibilityFilter)) {
return true;
}

if ($product->getVisibility() == Visibility::VISIBILITY_NOT_VISIBLE) {
if (empty($parent)) {
return false;
Expand Down Expand Up @@ -519,8 +533,13 @@ public function getImageData($attribute, $config, $product)
}
}
}
$this->galleryReadHandler->execute($product);

$galleryImages = $product->getMediaGallery('images');
if (!$galleryImages) {
$this->galleryReadHandler->execute($product);
$galleryImages = $product->getMediaGallery('images');
}

foreach ($galleryImages as $image) {
if (empty($image['disabled']) || !empty($config['inc_hidden_image'])) {
$images[] = $this->catalogProductMediaConfig->getMediaUrl($image['file']);
Expand Down Expand Up @@ -608,9 +627,15 @@ public function getResizedImage($product, $source, $size)
*/
public function getAttributeSetName($product)
{
static $attributeSets = [];

try {
$attributeSetRepository = $this->attributeSet->get($product->getAttributeSetId());
return $attributeSetRepository->getAttributeSetName();
if (!isset($attributeSets[$product->getAttributeSetId()])) {
$attributeSetName = $this->attributeSet->get($product->getAttributeSetId())->getAttributeSetName();
$attributeSets[$product->getAttributeSetId()] = $attributeSetName;
}

return $attributeSets[$product->getAttributeSetId()];
} catch (\Exception $e) {
$this->logger->addErrorLog('getAttributeSetName', $e->getMessage());
}
Expand Down Expand Up @@ -1313,4 +1338,20 @@ public function getParentId($product, $filters)

return array_unique($parentIds);
}

/**
* @return InventoryData
*/
public function getInventoryData()
{
return $this->inventoryData;
}

/**
* @return MediaData
*/
public function getMediaData()
{
return $this->mediaData;
}
}
6 changes: 5 additions & 1 deletion Helper/Source.php
Original file line number Diff line number Diff line change
Expand Up @@ -844,7 +844,11 @@ public function getCategoryData($product, $parent, $categories)
if (!empty($categories[$catId])) {
$category = $categories[$catId];
if (!empty($category['path'])) {
$path[] = ['level' => $category['level'], 'path' => implode(' > ', $category['path'])];
$path[] = [
'level' => $category['level'],
'id' => $catId,
'path' => implode(' > ', $category['path'])
];
}
}
}
Expand Down
76 changes: 45 additions & 31 deletions Model/Collection/Products.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
namespace Magmodules\Channable\Model\Collection;

use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory as ProductCollectionFactory;
use Magento\Catalog\Model\ResourceModel\Product\Collection as ProductCollection;
use Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory as ProductAttributeCollectionFactory;
use Magento\Eav\Model\Config as EavConfig;
use Magento\Catalog\Model\Indexer\Product\Flat\StateFactory;
Expand All @@ -27,6 +28,10 @@ class Products
* @var ProductCollectionFactory
*/
private $productCollectionFactory;
/**
* @var ProductCollection
*/
private $productCollection;
/**
* @var ProductAttributeCollectionFactory
*/
Expand Down Expand Up @@ -70,6 +75,7 @@ class Products
*/
public function __construct(
ProductCollectionFactory $productCollectionFactory,
ProductCollection $productCollection,
ProductAttributeCollectionFactory $productAttributeCollectionFactory,
EavConfig $eavConfig,
StockHelper $stockHelper,
Expand All @@ -79,6 +85,7 @@ public function __construct(
ResourceConnection $resource
) {
$this->productCollectionFactory = $productCollectionFactory;
$this->productCollection = $productCollection;
$this->productAttributeCollectionFactory = $productAttributeCollectionFactory;
$this->eavConfig = $eavConfig;
$this->productFlatState = $productFlatState;
Expand Down Expand Up @@ -116,6 +123,10 @@ public function getCollection($config, $page, $productIds)
->addUrlRewrite()
->setOrder('product_website.product_id', 'ASC');

if (in_array('tier_price', $attributes)) {
$collection->addTierPriceData();
}

if (!empty($filters['visibility'])) {
$collection->addAttributeToFilter('visibility', ['in' => $filters['visibility']]);
}
Expand Down Expand Up @@ -373,49 +384,52 @@ public function joinPriceIndexLeft($collection, $websiteId)
* @param $parentRelations
* @param $config
*
* @return \Magento\Catalog\Model\ResourceModel\Product\Collection
* @return ProductCollection
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function getParents($parentRelations, $config)
public function getParents($parentRelations, $config): ProductCollection
{
if (!empty($parentRelations)) {
$filters = $config['filters'];
if (empty($parentRelations)) {
return $this->productCollection; //return empty product collection
}

if (!$config['flat']) {
$productFlatState = $this->productFlatState->create(['isAvailable' => false]);
} else {
$productFlatState = $this->productFlatState->create(['isAvailable' => true]);
}
$filters = $config['filters'];

$entityField = $this->generalHelper->getLinkField();
$attributes = $this->getAttributes($config['attributes']);
if (!$config['flat']) {
$productFlatState = $this->productFlatState->create(['isAvailable' => false]);
} else {
$productFlatState = $this->productFlatState->create(['isAvailable' => true]);
}

$collection = $this->productCollectionFactory
->create(['catalogProductFlatState' => $productFlatState])
->addStoreFilter($config['store_id'])
->addAttributeToFilter($entityField, ['in' => array_values($parentRelations)])
->addAttributeToSelect($attributes)
->addUrlRewrite()
->setRowIdFieldName($entityField);
$entityField = $this->generalHelper->getLinkField();
$attributes = $this->getAttributes($config['attributes']);

if (!empty($filters['category_ids'])) {
if (!empty($filters['category_type'])) {
$collection->addCategoriesFilter([$filters['category_type'] => $filters['category_ids']]);
}
}
$collection = $this->productCollectionFactory
->create(['catalogProductFlatState' => $productFlatState])
->addStoreFilter($config['store_id'])
->addAttributeToFilter($entityField, ['in' => array_values($parentRelations)])
->addAttributeToSelect($attributes)
->addUrlRewrite()
->setRowIdFieldName($entityField);

if (!empty($filters['visibility'])) {
$collection->addAttributeToFilter('visibility', ['in' => $filters['visibility']]);
if (!empty($filters['category_ids'])) {
if (!empty($filters['category_type'])) {
$collection->addCategoriesFilter([$filters['category_type'] => $filters['category_ids']]);
}
}

if (!empty($config['inventory']['attributes'])) {
$this->joinCatalogInventoryLeft($collection, $config);
}
if (!empty($filters['visibility'])) {
$collection->addAttributeToFilter('visibility', ['in' => $filters['visibility']]);
}

$this->addFilters($filters, $collection, 'parent');
$this->joinPriceIndexLeft($collection, $config['website_id']);
return $collection->load();
if (!empty($config['inventory']['attributes'])) {
$this->joinCatalogInventoryLeft($collection, $config);
}

$this->addFilters($filters, $collection, 'parent');
$this->joinPriceIndexLeft($collection, $config['website_id']);
return $collection->load();

}

/**
Expand Down
8 changes: 8 additions & 0 deletions Model/Config/System/OrderRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,14 @@ public function deductFptTax(int $storeId = null): bool
return $this->isSetFlag(self::XML_PATH_DEDUCT_FPT, $storeId);
}

/**
* @inheritDoc
*/
public function isBusinessOrderEnabled(int $storeId = null): bool
{
return $this->isSetFlag(self::XML_PATH_BUSINESS_ORDER, $storeId);
}

/**
* @inheritDoc
*/
Expand Down
23 changes: 23 additions & 0 deletions Model/Generate.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
namespace Magmodules\Channable\Model;

use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\ResourceModel\Product\Collection as ProductCollection;
use Magento\Framework\Exception\LocalizedException;
use Magmodules\Channable\Model\Collection\Products as ProductsModel;
use Magmodules\Channable\Model\Item as ItemModel;
Expand Down Expand Up @@ -120,6 +121,8 @@ public function generateByStore(
$parentRelations = $this->productHelper->getParentsFromCollection($products, $config);
$parents = $this->productModel->getParents($parentRelations, $config);

$this->prefetchData($products, $parents, $parentRelations, $config);

foreach ($products as $product) {
/** @var Product $product */
$parent = null;
Expand Down Expand Up @@ -208,4 +211,24 @@ public function getDataRow($product, $parent, $config): ?array

return null;
}

/**
* Prefetches data to reduce amount of queries required.
* This increases performance by a lot for environments with >1ms latency to database.
*
* @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);
}
}
Loading

0 comments on commit d9665a8

Please sign in to comment.