Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for grouped products. #31

Open
wants to merge 1 commit 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
11 changes: 9 additions & 2 deletions app/code/Meta/Catalog/Model/Product/Feed/Builder/Tools.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,16 @@ public function replaceLocalUrlWithDummyUrl($url)
*/
public function getProductPrice(Product $product)
{
$price = $product->getPrice();
// Prevent zero prices from configurable or grouped products.
if ($product->getTypeId() != Product\Type::TYPE_SIMPLE) {
$price = $product->getPriceInfo()->getPrice('final_price')->getAmount()->getValue();
}

$price = $this->systemConfig->isPriceInclTax()
? $this->catalogHelper->getTaxPrice($product, $product->getPrice(), true)
: $product->getPrice();
? $this->catalogHelper->getTaxPrice($product, $price, true)
: $price;

return $this->formatPrice($price, $product->getStoreId());
}

Expand Down
23 changes: 3 additions & 20 deletions app/code/Meta/Catalog/Model/Product/Feed/Method/BatchApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Meta\BusinessExtension\Model\System\Config as SystemConfig;
use Meta\Catalog\Model\Product\Feed\Builder;
use Meta\Catalog\Model\Product\Feed\ProductRetriever\Configurable as ConfigurableProductRetriever;
use Meta\Catalog\Model\Product\Feed\ProductRetriever\Grouped as GroupedProductRetriever;
use Meta\Catalog\Model\Product\Feed\ProductRetriever\Simple as SimpleProductRetriever;
use Meta\Catalog\Model\Product\Feed\ProductRetriever\Other as OtherProductRetriever;
use Meta\Catalog\Model\Product\Feed\ProductRetrieverInterface;
Expand All @@ -38,26 +39,6 @@ class BatchApi
private const ATTR_DELETE = 'DELETE';
private const ATTR_DATA = 'data';

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

/**
* @var GraphAPIAdapter
*/
private $graphApiAdapter;

/**
* @var SystemConfig
*/
private $systemConfig;

/**
* @var ProductRetrieverInterface[]
*/
private $productRetrievers;

/**
* @var Builder
*/
Expand All @@ -77,6 +58,7 @@ public function __construct(
GraphAPIAdapter $graphApiAdapter,
SystemConfig $systemConfig,
SimpleProductRetriever $simpleProductRetriever,
GroupedProductRetriever $groupedProductRetriever,
ConfigurableProductRetriever $configurableProductRetriever,
OtherProductRetriever $otherProductRetriever,
Builder $builder
Expand All @@ -86,6 +68,7 @@ public function __construct(
$this->systemConfig = $systemConfig;
$this->productRetrievers = [
$simpleProductRetriever,
$groupedProductRetriever,
$configurableProductRetriever,
$otherProductRetriever
];
Expand Down
155 changes: 155 additions & 0 deletions app/code/Meta/Catalog/Model/Product/Feed/ProductRetriever/Grouped.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
<?php

declare(strict_types=1);

/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Meta\Catalog\Model\Product\Feed\ProductRetriever;

use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Model\Product\Visibility;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Meta\BusinessExtension\Helper\FBEHelper;
use Meta\Catalog\Model\Product\Feed\ProductRetrieverInterface;
use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
use Magento\GroupedProduct\Model\Product\Type\Grouped as GroupedType;
use Magento\Framework\Exception\LocalizedException;
use Meta\Catalog\Model\ProductRepository;

class Grouped implements ProductRetrieverInterface
{
private const LIMIT = 200;

/**
* @var ProductRepositoryInterface
*/
private $productRepo;

/**
* @var SearchCriteriaBuilder
*/
private $searchCriteriaBuilder;

/**
* @var int
*/
private $storeId;

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

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

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

/**
* Set store id
*
* @param int $storeId
* @return ProductRetrieverInterface
*/
public function setStoreId($storeId): ProductRetrieverInterface
{
$this->storeId = $storeId;
return $this;
}

/**
* @inheritDoc
*
* @throws LocalizedException
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function retrieve($offset = 1, $limit = self::LIMIT): array
{
$storeId = $this->storeId ?? $this->fbeHelper->getStore()->getId();

$configurableCollection = $this->productCollectionFactory->create();
$configurableCollection->addAttributeToSelect('*')
->addAttributeToFilter('visibility', ['neq' => Visibility::VISIBILITY_NOT_VISIBLE])
->addAttributeToFilter([
[
'attribute' => 'send_to_facebook',
'neq' => \Magento\Eav\Model\Entity\Attribute\Source\Boolean::VALUE_NO
],
[
'attribute' => 'send_to_facebook',
'null' => true
]
], null, 'left')
->addAttributeToFilter('type_id', GroupedType::TYPE_CODE)
->addStoreFilter($storeId)
->setStoreId($storeId);

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

$search = $this
->searchCriteriaBuilder
->addFilter('entity_id', array_keys($configurableCollection->getItems()), 'in')
->create();

$products = $this->productRepo->getList($search)->getItems();

$simpleProducts = [];
foreach ($products 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;
}
}