Skip to content

Commit fcd419f

Browse files
authored
Merge pull request #4813 from magento-honey-badgers/MC-18402
[honey] MC-18402: Cart promotions :: Implement new schema changes
2 parents 49b62a4 + 5484766 commit fcd419f

34 files changed

+2803
-197
lines changed
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\CatalogCustomerGraphQl\Model\Resolver;
9+
10+
use Magento\Catalog\Model\Product;
11+
use Magento\Catalog\Model\ResourceModel\Product\Collection;
12+
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
13+
use Magento\Customer\Api\CustomerRepositoryInterface;
14+
use Magento\Customer\Model\GroupManagement;
15+
use Magento\Framework\Exception\LocalizedException;
16+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
17+
use Magento\Framework\GraphQl\Config\Element\Field;
18+
use Magento\Framework\GraphQl\Query\ResolverInterface;
19+
use Magento\Framework\GraphQl\Query\Resolver\ValueFactory;
20+
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
21+
use Magento\Framework\Exception\NoSuchEntityException;
22+
23+
/**
24+
* @inheritdoc
25+
*/
26+
class TierPrices implements ResolverInterface
27+
{
28+
/**
29+
* @var Collection
30+
*/
31+
private $collection;
32+
33+
/**
34+
* @var CustomerRepositoryInterface
35+
*/
36+
private $customerRepository;
37+
38+
/**
39+
* @var ValueFactory
40+
*/
41+
private $valueFactory;
42+
43+
/**
44+
* @var int
45+
*/
46+
private $customerGroupId = null;
47+
48+
/**
49+
* @var array
50+
*/
51+
private $productIds = [];
52+
53+
/**
54+
* @param CollectionFactory $collectionFactory
55+
* @param ValueFactory $valueFactory
56+
* @param CustomerRepositoryInterface $customerRepository
57+
*/
58+
public function __construct(
59+
CollectionFactory $collectionFactory,
60+
ValueFactory $valueFactory,
61+
CustomerRepositoryInterface $customerRepository
62+
) {
63+
$this->collection = $collectionFactory->create();
64+
$this->valueFactory = $valueFactory;
65+
$this->customerRepository = $customerRepository;
66+
}
67+
68+
/**
69+
* @inheritdoc
70+
*/
71+
public function resolve(
72+
Field $field,
73+
$context,
74+
ResolveInfo $info,
75+
array $value = null,
76+
array $args = null
77+
) {
78+
if (!isset($value['model'])) {
79+
throw new LocalizedException(__('"model" value should be specified'));
80+
}
81+
82+
if (null === $this->customerGroupId) {
83+
$this->customerGroupId = $this->getCustomerGroupId($context);
84+
}
85+
86+
/** @var Product $product */
87+
$product = $value['model'];
88+
$productId = $product->getId();
89+
$this->productIds[] = $productId;
90+
$that = $this;
91+
92+
return $this->valueFactory->create(
93+
function () use ($that, $productId, $context) {
94+
$tierPrices = [];
95+
if (empty($that->productIds)) {
96+
return [];
97+
}
98+
if (!$that->collection->isLoaded()) {
99+
$that->collection->addIdFilter($that->productIds);
100+
$that->collection->addTierPriceDataByGroupId($that->customerGroupId);
101+
}
102+
/** @var \Magento\Catalog\Model\Product $item */
103+
foreach ($that->collection as $item) {
104+
if ($item->getId() === $productId) {
105+
// Try to extract all requested fields from the loaded collection data
106+
foreach ($item->getTierPrices() as $tierPrice) {
107+
$tierPrices[] = $tierPrice->getData();
108+
}
109+
}
110+
}
111+
return $tierPrices;
112+
}
113+
);
114+
}
115+
116+
/**
117+
* Get the customer group Id.
118+
*
119+
* @param \Magento\GraphQl\Model\Query\ContextInterface $context
120+
*
121+
* @return int
122+
*/
123+
private function getCustomerGroupId(\Magento\GraphQl\Model\Query\ContextInterface $context)
124+
{
125+
$currentUserId = $context->getUserId();
126+
if (!$currentUserId) {
127+
$customerGroupId = GroupManagement::NOT_LOGGED_IN_ID;
128+
} else {
129+
try {
130+
$customer = $this->customerRepository->getById($currentUserId);
131+
} catch (NoSuchEntityException $e) {
132+
throw new GraphQlNoSuchEntityException(
133+
__('Customer with id "%customer_id" does not exist.', ['customer_id' => $currentUserId]),
134+
$e
135+
);
136+
}
137+
$customerGroupId = $customer->getGroupId();
138+
}
139+
return $customerGroupId;
140+
}
141+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# CatalogCustomerGraphQl
2+
3+
**CatalogCustomerGraphQl** provides type and resolver information for GraphQL attributes that have dependences on the Catalog and Customer modules.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "magento/module-catalog-customer-graph-ql",
3+
"description": "N/A",
4+
"type": "magento2-module",
5+
"require": {
6+
"php": "~7.1.3||~7.2.0||~7.3.0",
7+
"magento/module-catalog": "*",
8+
"magento/module-customer": "*",
9+
"magento/framework": "*",
10+
"magento/module-graph-ql": "*"
11+
},
12+
"license": [
13+
"OSL-3.0",
14+
"AFL-3.0"
15+
],
16+
"autoload": {
17+
"files": [
18+
"registration.php"
19+
],
20+
"psr-4": {
21+
"Magento\\CatalogCustomerGraphQl\\": ""
22+
}
23+
}
24+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
9+
<module name="Magento_CatalogCustomerGraphQl" >
10+
<sequence>
11+
<module name="Magento_Catalog"/>
12+
<module name="Magento_Customer"/>
13+
<module name="Magento_GraphQl"/>
14+
</sequence>
15+
</module>
16+
</config>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Copyright © Magento, Inc. All rights reserved.
2+
# See COPYING.txt for license details.
3+
4+
interface ProductInterface {
5+
tier_prices: [ProductTierPrices] @doc(description: "An array of ProductTierPrices objects.") @resolver(class: "Magento\\CatalogCustomerGraphQl\\Model\\Resolver\\TierPrices")
6+
}
7+
8+
type ProductTierPrices @doc(description: "The ProductTierPrices object defines a tier price, which is a quantity discount offered to a specific customer group.") {
9+
customer_group_id: String @doc(description: "The ID of the customer group.")
10+
qty: Float @doc(description: "The number of items that must be purchased to qualify for tier pricing.")
11+
value: Float @doc(description: "The price of the fixed price item.")
12+
percentage_value: Float @doc(description: "The percentage discount of the item.")
13+
website_id: Float @doc(description: "The ID assigned to the website.")
14+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
use Magento\Framework\Component\ComponentRegistrar;
8+
9+
ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Magento_CatalogCustomerGraphQl', __DIR__);

app/code/Magento/CatalogGraphQl/Model/Resolver/Product/TierPrices.php

Lines changed: 0 additions & 63 deletions
This file was deleted.

app/code/Magento/CatalogGraphQl/etc/schema.graphqls

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,6 @@ interface ProductLinksInterface @typeResolver(class: "Magento\\CatalogGraphQl\\M
5858
position: Int @doc(description: "The position within the list of product links.")
5959
}
6060

61-
type ProductTierPrices @doc(description: "The ProductTierPrices object defines a tier price, which is a quantity discount offered to a specific customer group.") {
62-
customer_group_id: String @doc(description: "The ID of the customer group.")
63-
qty: Float @doc(description: "The number of items that must be purchased to qualify for tier pricing.")
64-
value: Float @doc(description: "The price of the fixed price item.")
65-
percentage_value: Float @doc(description: "The percentage discount of the item.")
66-
website_id: Float @doc(description: "The ID assigned to the website.")
67-
}
6861

6962
interface ProductInterface @typeResolver(class: "Magento\\CatalogGraphQl\\Model\\ProductInterfaceTypeResolverComposite") @doc(description: "The ProductInterface contains attributes that are common to all types of products. Note that descriptions may not be available for custom and EAV attributes.") {
7063
id: Int @doc(description: "The ID number assigned to the product.") @resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\Product\\EntityIdToId")
@@ -93,7 +86,6 @@ interface ProductInterface @typeResolver(class: "Magento\\CatalogGraphQl\\Model\
9386
websites: [Website] @doc(description: "An array of websites in which the product is available.") @resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\Product\\Websites")
9487
product_links: [ProductLinksInterface] @doc(description: "An array of ProductLinks objects.") @resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\Product\\ProductLinks")
9588
media_gallery_entries: [MediaGalleryEntry] @deprecated(reason: "Use product's `media_gallery` instead") @doc(description: "An array of MediaGalleryEntry objects.") @resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\Product\\MediaGalleryEntries")
96-
tier_prices: [ProductTierPrices] @doc(description: "An array of ProductTierPrices objects.") @resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\Product\\TierPrices")
9789
price: ProductPrices @doc(description: "A ProductPrices object, indicating the price of an item.") @resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\Product\\Price")
9890
gift_message_available: String @doc(description: "Indicates whether a gift message is available.")
9991
manufacturer: Int @doc(description: "A number representing the product's manufacturer.")

app/code/Magento/Quote/Model/Quote/Address/Total.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ public function getAllBaseTotalAmounts()
168168
{
169169
return $this->baseTotalAmounts;
170170
}
171-
171+
172172
//@codeCoverageIgnoreEnd
173173

174174
/**
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\QuoteGraphQl\Model\Cart;
9+
10+
use Magento\Quote\Model\Quote;
11+
12+
/**
13+
* Aggregate cart level discounts
14+
*
15+
* @package Magento\QuoteGraphQl\Model\Cart
16+
*/
17+
class DiscountAggregator
18+
{
19+
/**
20+
* Aggregate Discount per rule
21+
*
22+
* @param Quote $quote
23+
* @return array
24+
*/
25+
public function aggregateDiscountPerRule(
26+
Quote $quote
27+
) {
28+
$items = $quote->getItems();
29+
$discountPerRule = [];
30+
foreach ($items as $item) {
31+
$discountBreakdown = $item->getExtensionAttributes()->getDiscounts();
32+
if ($discountBreakdown) {
33+
foreach ($discountBreakdown as $key => $value) {
34+
/* @var \Magento\SalesRule\Model\Rule\Action\Discount\Data $discount */
35+
$discount = $value['discount'];
36+
$rule = $value['rule'];
37+
if (isset($discountPerRule[$key])) {
38+
$discountPerRule[$key]['discount'] += $discount->getAmount();
39+
} else {
40+
$discountPerRule[$key]['discount'] = $discount->getAmount();
41+
}
42+
$discountPerRule[$key]['rule'] = $rule;
43+
}
44+
}
45+
}
46+
return $discountPerRule;
47+
}
48+
}

0 commit comments

Comments
 (0)