Skip to content

Commit

Permalink
Merge branch '2.4-develop' into no-author/framework-01
Browse files Browse the repository at this point in the history
  • Loading branch information
engcom-Charlie authored Sep 16, 2024
2 parents 4a3bdcf + d01ee51 commit 0a808ab
Show file tree
Hide file tree
Showing 73 changed files with 6,571 additions and 366 deletions.
Original file line number Diff line number Diff line change
@@ -1,20 +1,45 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
/************************************************************************
*
* Copyright 2024 Adobe
* All Rights Reserved.
*
* NOTICE: All information contained herein is, and remains
* the property of Adobe and its suppliers, if any. The intellectual
* and technical concepts contained herein are proprietary to Adobe
* and its suppliers and are protected by all applicable intellectual
* property laws, including trade secret and copyright laws.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from Adobe.
* ************************************************************************
*/
declare(strict_types=1);

namespace Magento\BundleGraphQl\Model\Resolver;

use Magento\Bundle\Model\Product\Price;
use Magento\Catalog\Model\Product;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Quote\Api\Data\CartItemInterface;

class BundlePriceDetails implements ResolverInterface
{
/**
* BundlePriceDetails Constructor
*
* @param CartRepositoryInterface $cartRepository
*/
public function __construct(
private readonly CartRepositoryInterface $cartRepository
) {
}

/**
* @inheritdoc
*/
Expand All @@ -25,14 +50,44 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
}
/** @var Product $product */
$product = $value['model'];

$price = $product->getPrice();
$finalPrice = $product->getFinalPrice();
$discountPercentage = ($price) ? (100 - (($finalPrice * 100) / $price)) : 0;
if ((int)$product->getPriceType() === Price::PRICE_TYPE_DYNAMIC && isset($value['cart_item'])) {
$discountPercentage = $this->getDiscountPercentageForBundleProduct($value['cart_item']);
}
return [
'main_price' => $price,
'main_final_price' => $finalPrice,
'discount_percentage' => $discountPercentage
];
}

/**
* Calculate discount percentage for bundle product with dynamic pricing enabled
*
* @param CartItemInterface $cartItem
* @return float
* @throws NoSuchEntityException
*/
private function getDiscountPercentageForBundleProduct(CartItemInterface $cartItem): float
{
if (empty($cartItem->getAppliedRuleIds())) {
return 0;
}
$itemAmount = 0;
$discountAmount = 0;
$cart = $this->cartRepository->get($cartItem->getQuoteId());
foreach ($cart->getAllItems() as $item) {
if ($item->getParentItemId() == $cartItem->getId()) {
$itemAmount += $item->getPrice();
$discountAmount += $item->getDiscountAmount();
}
}
if ($itemAmount && $discountAmount) {
return ($discountAmount / $itemAmount) * 100;
}

return 0;
}
}
33 changes: 23 additions & 10 deletions app/code/Magento/Catalog/Block/Rss/Product/NewProducts.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@
*/
namespace Magento\Catalog\Block\Rss\Product;

use Magento\Framework\App\Rss\DataProviderInterface;
use Magento\Framework\App\Rss\DataProviderInterface as DProviderInterface;
use Magento\Framework\DataObject\IdentityInterface as IdInterface;

/**
* Class NewProducts
* @package Magento\Catalog\Block\Rss\Product
*/
class NewProducts extends \Magento\Framework\View\Element\AbstractBlock implements DataProviderInterface
class NewProducts extends \Magento\Framework\View\Element\AbstractBlock implements DProviderInterface, IdInterface
{
public const CACHE_TAG = 'rss_p_new';

/**
* @var \Magento\Catalog\Helper\Image
*/
Expand Down Expand Up @@ -55,6 +54,8 @@ public function __construct(
}

/**
* Configure class
*
* @return void
*/
protected function _construct()
Expand All @@ -64,15 +65,15 @@ protected function _construct()
}

/**
* {@inheritdoc}
* @inheritdoc
*/
public function isAllowed()
{
return $this->_scopeConfig->isSetFlag('rss/catalog/new', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
}

/**
* {@inheritdoc}
* @inheritdoc
*/
public function getRssData()
{
Expand Down Expand Up @@ -132,6 +133,8 @@ public function getRssData()
}

/**
* Get current store id
*
* @return int
*/
protected function getStoreId()
Expand Down Expand Up @@ -177,14 +180,16 @@ protected function renderPriceHtml(\Magento\Catalog\Model\Product $product)
}

/**
* {@inheritdoc}
* @inheritdoc
*/
public function getCacheLifetime()
{
return 600;
}

/**
* Generate rss feed
*
* @return array
*/
public function getFeeds()
Expand All @@ -199,10 +204,18 @@ public function getFeeds()
}

/**
* {@inheritdoc}
* @inheritdoc
*/
public function isAuthRequired()
{
return false;
}

/**
* @inheritdoc
*/
public function getIdentities()
{
return [self::CACHE_TAG];
}
}
1 change: 1 addition & 0 deletions app/code/Magento/Catalog/Model/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -2413,6 +2413,7 @@ public function getIdentities()
|| $this->isObjectNew();
if ($isProductNew && ($isStatusChanged || $this->getStatus() == Status::STATUS_ENABLED)) {
$identities[] = \Magento\Catalog\Block\Product\NewProduct::CACHE_TAG;
$identities[] = \Magento\Catalog\Block\Rss\Product\NewProducts::CACHE_TAG;
}

return array_unique($identities);
Expand Down
2 changes: 1 addition & 1 deletion app/code/Magento/Catalog/Test/Unit/Model/ProductTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -983,7 +983,7 @@ private function getNoStockStatusChangesData(MockObject $extensionAttributesMock
private function getNewProductProviderData(): array
{
return [
['cat_p_1', 'cat_c_p_1', 'cat_p_new'],
['cat_p_1', 'cat_c_p_1', 'cat_p_new', 'rss_p_new'],
null,
[
'id' => 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
if ((int) $this->scopeConfig->getValue('cataloginventory/options/not_available_message') === 1) {
return sprintf(
'Only %s available for sale. Please adjust the quantity to continue',
(string) $this->productStock->getProductAvailableStock($cartItem)
(string) $this->productStock->getProductSaleableQty($cartItem)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,51 @@
namespace Magento\CatalogInventoryGraphQl\Model\Resolver;

use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\CatalogInventory\Api\Data\StockStatusInterface;
use Magento\CatalogInventory\Api\StockStatusRepositoryInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Quote\Model\Quote\Item;

/**
* @inheritdoc
*/
class StockStatusProvider implements ResolverInterface
{
/**
* @var StockStatusRepositoryInterface
* Bundle product type code
*/
private $stockStatusRepository;
private const PRODUCT_TYPE_BUNDLE = "bundle";

/**
* Configurable product type code
*/
private const PRODUCT_TYPE_CONFIGURABLE = "configurable";

/**
* In Stock return code
*/
private const IN_STOCK = "IN_STOCK";

/**
* Out of Stock return code
*/
private const OUT_OF_STOCK = "OUT_OF_STOCK";

/**
* StockStatusProvider Constructor
*
* @param StockStatusRepositoryInterface $stockStatusRepository
* @param ProductRepositoryInterface $productRepositoryInterface
*/
public function __construct(StockStatusRepositoryInterface $stockStatusRepository)
{
$this->stockStatusRepository = $stockStatusRepository;
public function __construct(
private readonly StockStatusRepositoryInterface $stockStatusRepository,
private readonly ProductRepositoryInterface $productRepositoryInterface,
) {
}

/**
Expand All @@ -41,13 +63,58 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
if (!array_key_exists('model', $value) || !$value['model'] instanceof ProductInterface) {
throw new LocalizedException(__('"model" value should be specified'));
}
/** @var Item $cartItem */
$cartItem = $value['cart_item'] ?? [];
if (!$cartItem instanceof Item) {
$product = $value['model'];
$stockStatus = $this->stockStatusRepository->get($product->getId());

/* @var $product ProductInterface */
$product = $value['model'];
return ((int)$stockStatus->getStockStatus()) ? self::IN_STOCK : self::OUT_OF_STOCK;
}

if ($cartItem->getProductType() === self::PRODUCT_TYPE_BUNDLE) {
return $this->getBundleProductStockStatus($cartItem);
}

$product = $this->getVariantProduct($cartItem) ?? $cartItem->getProduct();
$stockStatus = $this->stockStatusRepository->get($product->getId());
$productStockStatus = (int)$stockStatus->getStockStatus();

return $productStockStatus === StockStatusInterface::STATUS_IN_STOCK ? 'IN_STOCK' : 'OUT_OF_STOCK';
return ((int)$stockStatus->getStockStatus()) ? self::IN_STOCK : self::OUT_OF_STOCK;
}

/**
* Get stock status of added bundle options
*
* @param Item $cartItem
* @return string
*/
private function getBundleProductStockStatus(Item $cartItem): string
{
$qtyOptions = $cartItem->getQtyOptions();
foreach ($qtyOptions as $qtyOption) {
$stockStatus = $this->stockStatusRepository->get($qtyOption->getProduct()->getId());
if (!(int)$stockStatus->getStockStatus()) {
return self::OUT_OF_STOCK;
}
}

return self::IN_STOCK;
}

/**
* Returns variant product if available
*
* @param Item $cartItem
* @return ProductInterface|null
* @throws NoSuchEntityException
*/
private function getVariantProduct(Item $cartItem): ?ProductInterface
{
if ($cartItem->getProductType() === self::PRODUCT_TYPE_CONFIGURABLE) {
if ($cartItem->getChildren()[0] !== null) {
return $this->productRepositoryInterface->get($cartItem->getSku());
}
}
return null;
}
}
Loading

0 comments on commit 0a808ab

Please sign in to comment.