diff --git a/Service/WebApi/Cart.php b/Service/WebApi/Cart.php index b07b360..2ffff74 100755 --- a/Service/WebApi/Cart.php +++ b/Service/WebApi/Cart.php @@ -7,6 +7,7 @@ namespace Magmodules\Reloadify\Service\WebApi; +use Magento\Catalog\Model\Product\Type; use Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface; use Magento\Framework\Api\SearchCriteriaInterface; use Magento\Framework\Encryption\EncryptorInterface; @@ -217,6 +218,7 @@ private function getProducts(Quote $quote) $quoteProduct = [ 'id' => $item->getProductId(), + 'product_type' => $item->getProductType(), 'quantity' => $item->getQty() ]; @@ -228,6 +230,14 @@ private function getProducts(Quote $quote) $quoteProduct['variant_id'] = $child->getProductId(); } } + + // If it is a simple product associated with a bundle, get the parent bundle product ID + if ($item->getProductType() == Type::TYPE_SIMPLE && + $item->getParentItem() && + $item->getParentItem()->getProductType() == Type::TYPE_BUNDLE) { + $quoteProduct['parent_id'] = $item->getParentItem()->getProductId(); + } + $quoteProducts[] = $quoteProduct; } return $quoteProducts; diff --git a/Service/WebApi/Order.php b/Service/WebApi/Order.php index 7f36b97..547bc62 100755 --- a/Service/WebApi/Order.php +++ b/Service/WebApi/Order.php @@ -7,6 +7,7 @@ namespace Magmodules\Reloadify\Service\WebApi; +use Magento\Catalog\Model\Product\Type; use Magento\Customer\Api\CustomerRepositoryInterface as CustomerRepository; use Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface; use Magento\Framework\Api\SearchCriteriaInterface; @@ -163,8 +164,10 @@ private function getProducts(OrderModel $order): array $orderedProduct = [ 'id' => $item->getProductId(), + 'product_type' => $item->getProductType(), 'quantity' => $item->getQtyOrdered(), ]; + if ($item->getProductType() == 'configurable') { $child = $item->getChildrenItems(); if (count($child) != 0) { @@ -172,6 +175,13 @@ private function getProducts(OrderModel $order): array $orderedProduct['variant_id'] = $child->getProductId(); } } + + // If it is a simple product associated with a bundle, get the parent bundle product ID + if ($item->getProductType() == Type::TYPE_SIMPLE && + $item->getParentItem() && + $item->getParentItem()->getProductType() == Type::TYPE_BUNDLE) { + $orderedProduct['parent_id'] = $item->getParentItem()->getProductId(); + } $orderedProducts[] = $orderedProduct; } diff --git a/Service/WebApi/Product.php b/Service/WebApi/Product.php index 7be934a..9651614 100755 --- a/Service/WebApi/Product.php +++ b/Service/WebApi/Product.php @@ -19,6 +19,7 @@ use Magmodules\Reloadify\Model\RequestLog\CollectionFactory as RequestLogCollectionFactory; use Magento\Catalog\Model\Product\Visibility; use Magento\Store\Model\StoreManagerInterface; +use Magento\Catalog\Model\Product\Type; /** * Product web API service class @@ -121,6 +122,7 @@ public function execute(int $storeId, array $extra = [], SearchCriteriaInterface $data[] = [ "id" => $product->getId(), "name" => $this->getAttributeValue($product, $name), + 'product_type' => $product->getTypeId(), "ean" => $this->getAttributeValue($product, $ean), "short_description" => $product->getShortDescription(), "description" => $this->getAttributeValue($product, $description), @@ -234,7 +236,7 @@ private function getMainImage($product) } /** - * Returns simple products array for configurable product type + * Returns simple products array for parent product * * @param ProductModel $product * @@ -243,13 +245,24 @@ private function getMainImage($product) private function getVariants(ProductModel $product) { $ids = []; - if ($product->getTypeId() == 'configurable') { - $products = $product->getTypeInstance()->getUsedProducts($product); - $ids = []; - foreach ($products as $product) { - $ids[] = $product->getId(); + $childProducts = null; + switch ($product->getTypeId()) { + case 'configurable': + $childProducts = $product->getTypeInstance()->getUsedProducts($product); + break; + case 'grouped': + $childProducts = $product->getTypeInstance()->getAssociatedProducts($product); + break; + } + + if ($childProducts) { + foreach ($childProducts as $childProduct) { + if ($childProduct->getTypeId() == 'simple') { + $ids[] = $childProduct->getId(); + } } } + return $ids; } diff --git a/Service/WebApi/Variants.php b/Service/WebApi/Variants.php index e7498ae..e31a98f 100755 --- a/Service/WebApi/Variants.php +++ b/Service/WebApi/Variants.php @@ -161,6 +161,7 @@ private function getAttributeValue($product, $attribute) */ private function getChildProducts(int $entityId = null) { + //configurable children $connection = $this->resourceConnection->getConnection(); $selectProducts = $connection->select()->from( $this->resourceConnection->getTableName('catalog_product_super_link'), @@ -169,7 +170,35 @@ private function getChildProducts(int $entityId = null) if ($entityId) { $selectProducts->where('product_id = ?', $entityId); } - return $connection->fetchPairs($selectProducts); + $configurable = $connection->fetchPairs($selectProducts); + + //grouped children + $selectProducts = $connection->select()->from( + $this->resourceConnection->getTableName('catalog_product_link'), + ['linked_product_id', 'product_id'] + )->where('link_type_id = 3'); + if ($entityId) { + $selectProducts->where('linked_product_id = ?', $entityId); + } + $grouped = $connection->fetchPairs($selectProducts); + + //bundle children + $selectProducts = $connection->select()->from( + ['r' => $this->resourceConnection->getTableName('catalog_product_relation')], + ['child_id', 'parent_id'] + )->joinLeft( + ['e' => $this->resourceConnection->getTableName('catalog_product_entity')], + 'r.parent_id = e.entity_id', + [] + )->where( + 'e.type_id = "bundle"' + ); + if ($entityId) { + $selectProducts->where('r.child_id = ?', $entityId); + } + $bundle = $connection->fetchPairs($selectProducts); + + return $configurable + $grouped + $bundle; } /** @@ -188,7 +217,6 @@ private function getCollection( $collection = $this->productsCollectionFactory->create() ->addAttributeToSelect('*') ->addFieldToFilter('entity_id', ['in' => array_keys($productIds)]) - ->addPriceData() ->setStore($storeId); $collection = $this->applyFilter($collection, $extra['filter']); diff --git a/composer.json b/composer.json index da42a23..0efa59e 100755 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "magmodules/magento2-reloadify", "description": "Reloadify extension for Magento 2", "type": "magento2-module", - "version": "1.9.0", + "version": "1.10.0", "license": [ "BSD-2-Clause" ], diff --git a/etc/config.xml b/etc/config.xml index d80f2d3..245500a 100755 --- a/etc/config.xml +++ b/etc/config.xml @@ -10,7 +10,7 @@ - v1.9.0 + v1.10.0 0 0 diff --git a/etc/db_schema_whitelist.json b/etc/db_schema_whitelist.json new file mode 100644 index 0000000..5dbcecb --- /dev/null +++ b/etc/db_schema_whitelist.json @@ -0,0 +1,13 @@ +{ + "reloadify_request_log": { + "column": { + "entity_id": true, + "type": true, + "store_id": true, + "created_at": true + }, + "constraint": { + "PRIMARY": true + } + } +} \ No newline at end of file