Skip to content
This repository has been archived by the owner on Apr 30, 2023. It is now read-only.

Add grouped products #47

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions Model/Product/Feed/Builder/Inventory.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class Inventory

const STATUS_OUT_OF_STOCK = 'out of stock';

const UNMANAGED_STOCK_QTY = 9999;

/**
* @var StockItemRepositoryInterface
*/
Expand Down Expand Up @@ -94,8 +96,8 @@ public function initInventoryForProduct(Product $product)
*/
public function getAvailability()
{
return $this->product && $this->productStock && $this->productStock->getIsInStock()
&& ($this->productStock->getQty() - $this->systemConfig->getOutOfStockThreshold() > 0)
return $this->productStock && $this->productStock->getIsInStock()
&& ($this->getInventory() - $this->systemConfig->getOutOfStockThreshold() > 0)
? self::STATUS_IN_STOCK : self::STATUS_OUT_OF_STOCK;
}

Expand All @@ -104,6 +106,14 @@ public function getAvailability()
*/
public function getInventory()
{
return $this->product && $this->productStock ? (int)$this->productStock->getQty() : 0;
if (!$this->productStock) {
return 0;
}

if (!$this->productStock->getManageStock()) {
return self::UNMANAGED_STOCK_QTY; // Fake Quantity to make product available if Manage Stock is off.
}

return (int)$this->productStock->getQty();
}
}
4 changes: 4 additions & 0 deletions Model/Product/Feed/Method/BatchApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Facebook\BusinessExtension\Helper\FBEHelper;
use Facebook\BusinessExtension\Model\Product\Feed\Builder;
use Facebook\BusinessExtension\Model\Product\Feed\ProductRetriever\Configurable as ConfigurableProductRetriever;
use Facebook\BusinessExtension\Model\Product\Feed\ProductRetriever\Grouped as GroupedProductRetriever;
use Facebook\BusinessExtension\Model\Product\Feed\ProductRetriever\Simple as SimpleProductRetriever;
use Facebook\BusinessExtension\Model\Product\Feed\ProductRetrieverInterface;
use Magento\Catalog\Model\Product;
Expand Down Expand Up @@ -39,18 +40,21 @@ class BatchApi
/**
* @param FBEHelper $helper
* @param SimpleProductRetriever $simpleProductRetriever
* @param GroupedProductRetriever $groupedProductRetriever
* @param ConfigurableProductRetriever $configurableProductRetriever
* @param Builder $builder
*/
public function __construct(
FBEHelper $helper,
SimpleProductRetriever $simpleProductRetriever,
GroupedProductRetriever $groupedProductRetriever,
ConfigurableProductRetriever $configurableProductRetriever,
Builder $builder
) {
$this->fbeHelper = $helper;
$this->productRetrievers = [
$simpleProductRetriever,
$groupedProductRetriever,
$configurableProductRetriever
];
$this->builder = $builder;
Expand Down
87 changes: 87 additions & 0 deletions Model/Product/Feed/ProductRetriever/Grouped.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
*/
namespace Facebook\BusinessExtension\Model\Product\Feed\ProductRetriever;

use Facebook\BusinessExtension\Helper\FBEHelper;
use Facebook\BusinessExtension\Model\Product\Feed\ProductRetrieverInterface;
use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\Product\Attribute\Source\Status;
use Magento\Catalog\Model\Product\Visibility;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
use Magento\GroupedProduct\Model\Product\Type\Grouped as GroupedType;
use Magento\Framework\Exception\LocalizedException;

class Grouped implements ProductRetrieverInterface
{
const LIMIT = 200;

/**
* @var FBEHelper
*/
protected $fbeHelper;

/**
* @var CollectionFactory
*/
protected $productCollectionFactory;

/**
* @param FBEHelper $fbeHelper
* @param CollectionFactory $productCollectionFactory
*/
public function __construct(FBEHelper $fbeHelper, CollectionFactory $productCollectionFactory)
{
$this->fbeHelper = $fbeHelper;
$this->productCollectionFactory = $productCollectionFactory;
}

/**
* @inheritDoc
*/
public function getProductType()
{
return GroupedType::TYPE_CODE;
}

/**
* @inheritDoc
* @throws LocalizedException
*/
public function retrieve($offset = 1, $limit = self::LIMIT): array
{
$groupedCollection = $this->productCollectionFactory->create();
$groupedCollection->addAttributeToSelect('*')
->addAttributeToFilter('status', Status::STATUS_ENABLED)
->addAttributeToFilter('visibility', ['neq' => Visibility::VISIBILITY_NOT_VISIBLE])
->addAttributeToFilter('type_id', $this->getProductType())
->setStoreId($this->fbeHelper->getStore()->getId());

$groupedCollection->getSelect()->limit($limit, $offset);

$simpleProducts = [];
foreach ($groupedCollection as $product) {
/** @var Product $product */
/** @var GroupedType $groupedType */
$groupedType = $product->getTypeInstance();
$groupedProduct = ['item_group_id' => $product->getId()];
foreach ($groupedType->getAssociatedProducts($product) as $childProduct) {
/** @var Product $childProduct */
$childProduct->setConfigurableSettings($groupedProduct);
$childProduct->setParentProductUrl($product->getProductUrl());
$simpleProducts[] = $childProduct;
}
}

return $simpleProducts;
}

/**
* @inheritDoc
*/
public function getLimit()
{
return self::LIMIT;
}
}
2 changes: 1 addition & 1 deletion Observer/ProcessProductAfterSaveEventObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function execute(Observer $observer)
$requestData = $this->batchApi->buildProductRequest($product);
$requestParams = [];
$requestParams[0] = $requestData;
$response = $this->fbeHelper->makeHttpRequest($requestParams, null);
$this->fbeHelper->makeHttpRequest($requestParams, null);
} catch (Exception $e) {
$this->fbeHelper->logException($e);
}
Expand Down