Skip to content

Commit

Permalink
Shopping cart discount concept
Browse files Browse the repository at this point in the history
  • Loading branch information
rogyar committed May 17, 2019
1 parent 02bca98 commit 31b34ad
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 5 deletions.
16 changes: 16 additions & 0 deletions app/code/Magento/QuoteGraphQl/Model/Resolver/CartPrices.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
'value' => $cartTotals->getSubtotalWithDiscount(), 'currency' => $currency
],
'applied_taxes' => $this->getAppliedTaxes($cartTotals, $currency),
'discount' => $this->getDiscount($cartTotals, $currency),
'model' => $quote
];
}
Expand Down Expand Up @@ -84,4 +85,19 @@ private function getAppliedTaxes(Total $total, string $currency): array
}
return $appliedTaxesData;
}

/**
* Returns information about an applied discount
*
* @param Total $total
* @param string $currency
* @return array
*/
private function getDiscount(Total $total, string $currency)
{
return [
'label' => $total->getDiscountDescription(),
'amount' => ['value' => $total->getDiscountAmount(), "currency" => $currency]
];
}
}
6 changes: 6 additions & 0 deletions app/code/Magento/QuoteGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ type CartPrices {
grand_total: Money
subtotal_including_tax: Money
subtotal_excluding_tax: Money
discount: CartDiscountItem
subtotal_with_discount_excluding_tax: Money
applied_taxes: [CartTaxItem]
}
Expand All @@ -151,6 +152,11 @@ type CartTaxItem {
label: String!
}

type CartDiscountItem {
amount: Money!
label: String!
}

type SetPaymentMethodOnCartOutput {
cart: Cart!
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ public function testGetTotalsWithNoTaxApplied()
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @group recent
*/
public function testGetCartTotalsWithNoAddressSet()
{
Expand Down Expand Up @@ -125,6 +124,34 @@ public function testGetSelectedShippingMethodFromCustomerCart()
$this->graphQlQuery($query);
}

/**
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/cart_rule_discount_no_coupon.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @group recent
*/
public function testGetDiscountInformation()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId);
$response = $this->graphQlQuery($query);

$discountResponse = $response['cart']['prices']['discount'];
self::assertEquals(-20, $discountResponse['amount']['value']);
self::assertEquals('100% Off for all orders', $discountResponse['label']);
}

public function testGetDiscountInformationWithTwoRulesApplied()
{
self::fail();
}

public function testGetDiscountInformationForRuleWithNoLabel()
{
self::fail();
}

/**
* Generates GraphQl query for retrieving cart totals
*
Expand Down Expand Up @@ -160,6 +187,13 @@ private function getQuery(string $maskedQuoteId): string
currency
}
}
discount {
label
amount {
value
currency
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

use Magento\Customer\Model\GroupManagement as CustomerGroupManagement;
use Magento\Framework\Api\DataObjectHelper;
use Magento\SalesRule\Api\RuleRepositoryInterface;
use Magento\SalesRule\Model\Data\Rule as RuleData;
use Magento\Store\Model\StoreManagerInterface as StoreManagerInterface;
use Magento\TestFramework\Helper\Bootstrap;


$objectManager = Bootstrap::getObjectManager();
/** @var RuleRepositoryInterface $ruleRepository */
$ruleRepository = $objectManager->get(RuleRepositoryInterface::class);
/** @var DataObjectHelper $dataObjectHelper */
$dataObjectHelper = Bootstrap::getObjectManager()->get(DataObjectHelper::class);
$ruleLabel = $objectManager->create(\Magento\SalesRule\Api\Data\RuleLabelInterface::class);
$ruleLabelFactory = $objectManager->get(\Magento\SalesRule\Model\Data\RuleLabelFactory::class);


/** @var RuleData $salesRule */
$salesRule = $objectManager->create(RuleData::class);
/** @var \Magento\SalesRule\Api\Data\RuleLabelInterface $ruleLabel */
$ruleLabel = $ruleLabelFactory->create();
$ruleLabel->setStoreId(0);
$ruleLabel->setStoreLabel('50% Off for all orders');
$ruleData = [
'name' => '50% Off for all orders',
'is_active' => 1,
'customer_group_ids' => [CustomerGroupManagement::NOT_LOGGED_IN_ID],
'coupon_type' => RuleData::COUPON_TYPE_NO_COUPON,
'conditions' => [],
'simple_action' => 'by_percent',
'discount_amount' => 50,
'discount_step' => 0,
'website_ids' => [
$objectManager->get(
StoreManagerInterface::class
)->getWebsite()->getId(),
],
'discount_qty' => 0,
'apply_to_shipping' => 1,
'simple_free_shipping' => 1,
];
$dataObjectHelper->populateWithArray($salesRule, $ruleData, \Magento\SalesRule\Api\Data\RuleInterface::class);
$salesRule->setStoreLabels([$ruleLabel]);

$ruleRepository->save($salesRule);
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

use Magento\SalesRule\Api\RuleRepositoryInterface;
use Magento\SalesRule\Model\ResourceModel\Rule as RuleResource;
use Magento\SalesRule\Model\RuleFactory as RuleFactory;
use Magento\TestFramework\Helper\Bootstrap;

$objectManager = Bootstrap::getObjectManager();
/** @var RuleRepositoryInterface $ruleRepository */
$ruleRepository = $objectManager->get(RuleRepositoryInterface::class);
/** @var RuleResource $ruleResource */
$ruleResource = $objectManager->get(RuleResource::class);
/** @var RuleFactory $ruleFactory */
$ruleFactory = $objectManager->get(RuleFactory::class);
$salesRule = $ruleFactory->create();

$ruleResource->load($salesRule, '50% Off for all orders', 'name');
// FIXME: the rule cannot be found for some reason
if ($salesRule->getRuleId()) {
$ruleRepository->deleteById($salesRule->getRuleId());
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,19 @@
*/
declare(strict_types=1);

use Magento\Framework\Registry;
use Magento\SalesRule\Api\RuleRepositoryInterface;
use Magento\TestFramework\Helper\Bootstrap;

$objectManager = Bootstrap::getObjectManager();
/** @var Magento\Framework\Registry $registry */
$registry = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(\Magento\Framework\Registry::class);
$registry = Bootstrap::getObjectManager()->get(Registry::class);
/** @var RuleRepositoryInterface $ruleRepository */
$ruleRepository = $objectManager->get(RuleRepositoryInterface::class);

/** @var Magento\SalesRule\Model\Rule $rule */
$rule = $registry->registry('cart_rule_100_percent_off');
if ($rule) {
$rule->delete();
$ruleId = $registry->registry('Magento/SalesRule/_files/cart_rule_100_percent_off');

if ($ruleId) {
$ruleRepository->deleteById($ruleId);
}

0 comments on commit 31b34ad

Please sign in to comment.