Skip to content

Commit

Permalink
Add more options for sales rule labels
Browse files Browse the repository at this point in the history
  • Loading branch information
BobWez98 committed Sep 13, 2024
1 parent c2f7896 commit e94fd83
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"magento/framework": "*",
"magento/module-sales-rule": "*",
"magento/module-catalog": "*",
"magento/module-graph-ql": "*",
"magento/module-quote-graph-ql": "*"
},
"require-dev": {
Expand Down
25 changes: 22 additions & 3 deletions src/Model/Resolver/Quote/Data/SalesRuleLabel.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Magento\SalesRule\Api\RuleRepositoryInterface;
use Magento\SalesRule\Api\Data\RuleInterface;
use Magento\Quote\Model\Quote\Item;
use Magento\GraphQl\Model\Query\Context;

class SalesRuleLabel implements ResolverInterface
{
Expand All @@ -23,11 +24,29 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value

$labels = [];

foreach (explode(',', (string) $cartItem->getAppliedRuleIds()) as $ruleId) {
foreach (explode(',', (string) $cartItem->getAppliedRuleIds()) as $key=>$ruleId) {
/** @var RuleInterface $rule */
$rule = $this->ruleRepository->getById((int) $ruleId);

$labels[] = $rule->getName();

// @phpstan-ignore-next-line
$store = $context->getExtensionAttributes()->getStore();
$storeId = $store->getId();

foreach($rule->getStoreLabels() as $storeLabel) {
if ((int) $storeLabel->getStoreId() === (int) $storeId) {
$storeLabel = $storeLabel->getStoreLabel();
break;
}
}

$labels[] = [
'name' => $rule->getName(),
'description' => $rule->getDescription(),
'discount_amount' => $rule->getDiscountAmount(),
'from_date' => $rule->getFromDate(),
'to_date' => $rule->getToDate(),
'store_label' => $storeLabel ?? null,
];
}

return $labels;
Expand Down
11 changes: 10 additions & 1 deletion src/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,16 @@ type ProductStockItem {
qty_increments: Int @doc(description: "Quantity increments in which you can buy the item")
}

type SalesRuleLabel {
name: String @doc(description: "Name of the label")
description: String @doc(description: "Description of the label")
store_label: String @doc(description: "Label of the current store")
discount_amount: Float @doc(description: "Amount of discount")
from_date: String @doc(description: "Date the sales rule becomes active")
to_date: String @doc(description: "Date the sales rule stops being active")
}

interface CartItemInterface {
qty_backordered: Int @resolver(class: "Rapidez\\Compadre\\Model\\Resolver\\Quote\\Backorder") @doc(description: "Amount of items that are backordered")
sales_rule_labels: [String] @resolver(class: "Rapidez\\Compadre\\Model\\Resolver\\Quote\\Data\\SalesRuleLabel") @doc(descipriont: "Labels of the sales rules")
sales_rule_labels: [SalesRuleLabel] @resolver(class: "Rapidez\\Compadre\\Model\\Resolver\\Quote\\Data\\SalesRuleLabel") @doc(descipriont: "Labels of the sales rules")
}

0 comments on commit e94fd83

Please sign in to comment.