Skip to content

Commit

Permalink
Merge pull request #64 from magmodules/release/2.0.5
Browse files Browse the repository at this point in the history
Release/2.0.5
  • Loading branch information
Marvin-Magmodules committed Mar 13, 2024
2 parents 1868d2b + ad102b7 commit af5d1bd
Show file tree
Hide file tree
Showing 9 changed files with 137 additions and 28 deletions.
2 changes: 1 addition & 1 deletion Model/Config/Source/Attributes.php
Expand Up @@ -95,7 +95,7 @@ function ($a, $b) {
*/
public function getNonAvailableAttributes(): array
{
return ['categories', 'gallery', 'category_ids', 'quantity_and_stock_status'];
return ['categories', 'gallery', 'category_ids', 'quantity_and_stock_status', 'price', 'special_price'];
}

/**
Expand Down
56 changes: 49 additions & 7 deletions Model/ProductData/Repository.php
Expand Up @@ -80,6 +80,10 @@ class Repository implements ProductData
* @var array
*/
private $entityIds;
/**
* @var array
*/
private $parentSimples;
/**
* @var Type
*/
Expand Down Expand Up @@ -134,6 +138,7 @@ public function getProductData(int $storeId = 0, ?array $entityIds = null, int $
{
$this->collectIds($storeId, $entityIds);
$this->collectAttributes($storeId);
$this->parentSimples = [];
$this->staticFields = $this->dataConfigRepository->getStaticFields($storeId);
$this->imageData = $this->image->execute($this->entityIds, $storeId);

Expand All @@ -148,17 +153,54 @@ public function getProductData(int $storeId = 0, ?array $entityIds = null, int $
$result[$entityId][$index] = $this->prepareAttribute($attr, $productData);
}
$result[$entityId] += $this->categoryData($productData);

if (!empty($productData['parent_id'])) {
$this->parentSimples[$productData['parent_id']][] = $productData['product_id'];
}
}

if ($this->dataConfigRepository->getFilters($storeId)['exclude_out_of_stock']) {
foreach ($result as $id => &$row) {
if ($row['sqr:availability'] == 'out of stock') {
unset($result[$id]);
}
return $this->postProcess($result, $storeId);
}

/**
* @param array $result
* @param int $storeId
* @return array
*/
private function postProcess(array $result, int $storeId = 0): array
{
if (!$this->dataConfigRepository->getFilters($storeId)['exclude_out_of_stock'] || empty($result)) {
return $result;
}

$unsetSimples = [];
foreach ($result as $id => &$row) {

// Remove parent products without simples
if ($row['sqr:id'] == $row['sqr:assoc_id'] && $row['sqr:price'] == 0.00) {
$unsetSimples[] = $id;
continue;
}

// Remove out of stock products
if ($row['sqr:availability'] != 'out of stock') {
continue;
}
$unsetSimples[] = $id;

if (!empty($row['sqr:assoc_id']) && isset($this->parentSimples[$row['sqr:assoc_id']])) {
$this->parentSimples[$row['sqr:assoc_id']] = array_diff(
$this->parentSimples[$row['sqr:assoc_id']],
[$id]
);
}
}

return $result;
$emptyParents = array_keys(array_filter($this->parentSimples), function ($value) {
return empty($value);
});

return array_diff_key($result, array_flip($emptyParents) + array_flip($unsetSimples));
}

/**
Expand Down Expand Up @@ -354,7 +396,7 @@ private function prepareAttribute(string $attribute, array $productData)
}
break;
case 'status':
return ($value) ? 'Enabled' : 'Disabled';
return ($value == 1) ? 'Enabled' : 'Disabled';
case 'is_in_stock':
return ($value) ? 'in stock' : 'out of stock';
case 'manage_stock':
Expand Down
70 changes: 63 additions & 7 deletions Service/ProductData/AttributeCollector/Data/Parents.php
Expand Up @@ -9,6 +9,7 @@

use Exception;
use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Model\Product\Attribute\Source\Status;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\EntityManager\MetadataPool;

Expand Down Expand Up @@ -50,20 +51,20 @@ public function __construct(
* @param array[] $entityIds array of product IDs
* @return array[]
*/
public function execute($entityIds = []): array
public function execute($entityIds = [], bool $excludeDisabled = true): array
{
if (empty($entityIds)) {
return $this->collectAllParents();
return $this->collectAllParents($excludeDisabled);
}
return $this->collectParents($entityIds);
return $this->collectParents($entityIds, $excludeDisabled);
}

/**
* Get parent product IDs
*
* @return array[]
*/
private function collectAllParents(): array
private function collectAllParents(bool $excludeDisabled): array
{
$result = [];
$select = $this->resource->getConnection()
Expand All @@ -75,9 +76,24 @@ private function collectAllParents(): array
"catalog_product_entity.{$this->linkField} = catalog_product_relation.parent_id",
'type_id'
);

if ($excludeDisabled && $attributeId = $this->getStatusAttributeId()) {
$select->joinLeft(
['catalog_product_entity_int' => $this->resource->getTableName('catalog_product_entity_int')],
"catalog_product_entity_int.entity_id = catalog_product_entity.{$this->linkField}"
)->where(
'catalog_product_entity_int.value = ?',
Status::STATUS_ENABLED
)->where(
'catalog_product_entity_int.attribute_id = ?',
$attributeId
);
}

foreach ($this->resource->getConnection()->fetchAll($select) as $item) {
$result[$item['child_id']][$item['parent_id']] = $item['type_id'];
}

return $result;
}

Expand All @@ -87,7 +103,7 @@ private function collectAllParents(): array
* @param array[] $entityIds array of product IDs
* @return array[]
*/
private function collectParents(array $entityIds): array
private function collectParents(array $entityIds, bool $excludeDisabled): array
{
$all = $entityIds;
$result = [];
Expand All @@ -100,11 +116,51 @@ private function collectParents(array $entityIds): array
"catalog_product_entity.{$this->linkField} = catalog_product_relation.parent_id",
'type_id'
)->where('child_id IN (?)', $entityIds);
$relations = $this->resource->getConnection()->fetchAll($select);
foreach ($relations as $item) {

if ($excludeDisabled && $attributeId = $this->getStatusAttributeId()) {
$select->joinLeft(
['catalog_product_entity_int' => $this->resource->getTableName('catalog_product_entity_int')],
"catalog_product_entity_int.entity_id = catalog_product_entity.{$this->linkField}"
)->where(
'catalog_product_entity_int.value = ?',
Status::STATUS_ENABLED
)->where(
'catalog_product_entity_int.attribute_id = ?',
$attributeId
);
}

foreach ($this->resource->getConnection()->fetchAll($select) as $item) {
$result[$item['child_id']][$item['parent_id']] = $item['type_id'];
$all += [$item['child_id'], $item['parent_id']];
}

return ['all' => array_unique($all), 'relations' => $result];
}

/**
* Get attribute id for status attribute
*
* @return int
*/
private function getStatusAttributeId(): int
{
$connection = $this->resource->getConnection();
$selectAttributeId = $connection->select()->from(
['eav_attribute' => $this->resource->getTableName('eav_attribute')],
['attribute_id']
)->joinLeft(
['eav_entity_type' => $this->resource->getTableName('eav_entity_type')],
'eav_entity_type.entity_type_id = eav_attribute.entity_type_id',
[]
)->where(
'entity_type_code = ?',
'catalog_product'
)->where(
'attribute_code = ?',
'status'
);

return (int)$connection->fetchOne($selectAttributeId);
}
}
6 changes: 5 additions & 1 deletion Service/ProductData/AttributeCollector/Data/Price.php
Expand Up @@ -192,7 +192,7 @@ private function getProductData(array $productIds = [])
'price_index.customer_group_id = 0'
]
),
['final_price', 'min_price', 'max_price']
['final_price', 'min_price', 'max_price', 'price']
);

return $products;
Expand Down Expand Up @@ -246,6 +246,10 @@ private function setPrices(Product $product, ?string $groupedPriceType, ?string
if ($this->finalPrice === null && $this->price !== null) {
$this->finalPrice = $this->price;
}

if ($this->price == '0.0000' && $this->finalPrice > 0) {
$this->price = $this->finalPrice;
}
}

/**
Expand Down
8 changes: 6 additions & 2 deletions Service/ProductData/AttributeCollector/Data/Stock.php
Expand Up @@ -231,6 +231,10 @@ private function getNoMsiStock(bool $addMsi = false): array
['catalog_product_entity' => $this->resource->getTableName('catalog_product_entity')],
"catalog_product_entity.entity_id = cataloginventory_stock_item.product_id",
['sku']
)->joinLeft(
['css' => $this->resource->getTableName('cataloginventory_stock_status')],
'css.product_id = catalog_product_entity.entity_id',
['stock_status']
);
if ($addMsi) {
$select->joinLeft(
Expand All @@ -247,8 +251,8 @@ private function getNoMsiStock(bool $addMsi = false): array
$result[$value['product_id']] =
[
'qty' => (int)$value['qty'],
'is_in_stock' => (int)$value['is_in_stock'],
'availability' => (int)$value['is_in_stock'],
'is_in_stock' => (int)$value['stock_status'],
'availability' => (int)$value['stock_status'],
'manage_stock' => (int)$value['manage_stock'],
'qty_increments' => (int)$value['qty_increments'],
'min_sale_qty' => (int)$value['min_sale_qty']
Expand Down
16 changes: 9 additions & 7 deletions Service/ProductData/Filter.php
Expand Up @@ -61,16 +61,13 @@ public function __construct(
public function execute(array $filter, int $storeId = 0): array
{
$entityIds = $this->filterVisibility($filter);
$entityIds = $this->filterStatus($entityIds, $filter['add_disabled_products']);

if ($storeId) {
$websiteId = $this->getWebsiteId($storeId);
$entityIds = $this->filterWebsite($entityIds, $websiteId);
}

if (!$filter['add_disabled_products']) {
$entityIds = $this->filterEnabledStatus($entityIds);
}

if ($filter['restrict_by_category']) {
$entityIds = $this->filterByCategories(
$entityIds,
Expand Down Expand Up @@ -169,10 +166,15 @@ private function filterWebsite(array $entityIds, int $websiteId): array
* Filter entity ids to exclude products with status disabled
*
* @param array $entityIds
* @param bool $addDisabled
* @return array
*/
private function filterEnabledStatus(array $entityIds): array
private function filterStatus(array $entityIds, bool $addDisabled = false): array
{
$status = $addDisabled
? [Status::STATUS_ENABLED, Status::STATUS_DISABLED]
: [Status::STATUS_ENABLED];

$connection = $this->resourceConnection->getConnection();
$select = $connection->select()->distinct()->from(
['catalog_product_entity_int' => $this->resourceConnection->getTableName('catalog_product_entity_int')],
Expand All @@ -182,8 +184,8 @@ private function filterEnabledStatus(array $entityIds): array
'eav_attribute.attribute_id = catalog_product_entity_int.attribute_id',
[]
)->where(
'value = ?',
Status::STATUS_ENABLED
'value in (?)',
$status
)->where(
'attribute_code = ?',
'status'
Expand Down
3 changes: 2 additions & 1 deletion Service/ProductData/Type.php
Expand Up @@ -49,6 +49,7 @@ public function __construct(
* @param array $attributeMap
* @param array $extraParameters
* @param int $storeId
* @param bool $addDisabled
* @return array
* @throws NoSuchEntityException
*/
Expand All @@ -62,7 +63,7 @@ public function execute(
return [];
}

$parents = $this->parents->execute();
$parents = $this->parents->execute($entityIds, $extraParameters['filters']['exclude_disabled']);
$toUnset = [];
$parentAttributeToUse = [];
$extraProductsToLoad = [];
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -2,7 +2,7 @@
"name": "magmodules/magento2-sooqr",
"description": "Sooqr integration for Magento 2",
"type": "magento2-module",
"version": "2.0.4",
"version": "2.0.5",
"license": [
"OSL-3.0",
"AFL-3.0"
Expand Down
2 changes: 1 addition & 1 deletion etc/config.xml
Expand Up @@ -11,7 +11,7 @@
<sooqr_general>
<general>
<enable>1</enable>
<version>v2.0.4</version>
<version>v2.0.5</version>
</general>
<credentials>
<environment>production</environment>
Expand Down

0 comments on commit af5d1bd

Please sign in to comment.