diff --git a/composer.json b/composer.json index fdaaca7..be40333 100644 --- a/composer.json +++ b/composer.json @@ -12,6 +12,7 @@ "magento/framework": "*", "magento/module-sales-rule": "*", "magento/module-catalog": "*", + "magento/module-graph-ql": "*", "magento/module-quote-graph-ql": "*" }, "require-dev": { diff --git a/src/Model/Resolver/Quote/Data/SalesRuleLabel.php b/src/Model/Resolver/Quote/Data/SalesRuleLabel.php index c56b20f..f6408e3 100644 --- a/src/Model/Resolver/Quote/Data/SalesRuleLabel.php +++ b/src/Model/Resolver/Quote/Data/SalesRuleLabel.php @@ -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 { @@ -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; diff --git a/src/etc/schema.graphqls b/src/etc/schema.graphqls index d60bbca..c91125a 100644 --- a/src/etc/schema.graphqls +++ b/src/etc/schema.graphqls @@ -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") }