Skip to content

Commit

Permalink
Add SalesRuleLabels to StockItems (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
BobWez98 authored Sep 16, 2024
1 parent 88c88dc commit ad1f05c
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 4 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/analyse.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: PHPStan

on: ['push', 'pull_request']

jobs:
test:
runs-on: ubuntu-latest
name: analyse

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.2
extensions: dom, curl, libxml, mbstring, zip, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick, fileinfo
coverage: none

- name: Install dependencies
run: composer install --no-interaction

- name: Analyse
run: vendor/bin/phpstan analyse
22 changes: 20 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,22 @@
"minimum-stability": "dev",
"prefer-stable": true,
"require": {
"php": "^8.0.2|^8.1"
"php": "^8.0.2|^8.1",
"magento/framework": "*",
"magento/module-sales-rule": "*",
"magento/module-catalog": "*",
"magento/module-graph-ql": "*",
"magento/module-quote-graph-ql": "*"
},
"require-dev": {
"bitexpert/phpstan-magento": "^0.11.0",
"phpstan/phpstan": "^1.10"
},
"repositories": {
"magento": {
"type": "composer",
"url": "https://repo-magento-mirror.fooman.co.nz/"
}
},
"autoload": {
"psr-4": {
Expand All @@ -19,6 +34,9 @@
]
},
"config": {
"sort-packages": true
"sort-packages": true,
"allow-plugins": {
"magento/composer-dependency-version-audit-plugin": true
}
}
}
9 changes: 9 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
includes:
- vendor/bitexpert/phpstan-magento/extension.neon
parameters:
paths:
- .
excludePaths:
- vendor
- Test/*
level: 5
2 changes: 1 addition & 1 deletion src/Model/Resolver/Inventory/StockItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
/** @var ProductInterface $product */
$product = $value['model'];
/** @var StockStatusInterface $stockStatus */
$stockStatus = $this->stockStatusRepository->get($product->getId());
$stockStatus = $this->stockStatusRepository->get((string) $product->getId());
/** @var StockItemInterface $stockItem */
$stockItem = $stockStatus->getStockItem();

Expand Down
4 changes: 3 additions & 1 deletion src/Model/Resolver/Quote/Backorder.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
if ($configuredItems) {
/** @var Option $item */
$item = reset($configuredItems);
$backorderStatus = $item?->getProduct()?->getExtensionAttributes()?->getStockItem()?->getBackorders() ?? 0;
// @phpstan-ignore-next-line
$backorderStatus = $item?->getProduct()?->getExtensionAttributes()->getStockItem()->getBackorders() ?? 0;
if ($backorderStatus != 2) {
return 0;
}
Expand All @@ -48,6 +49,7 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value

/** @var ProductInterface $product */
$product = $this->productRepositoryInterface->get($cartItem->getSku());
// @phpstan-ignore-next-line
$stockItem = $product->getExtensionAttributes()->getStockItem();
if (!$stockItem || $stockItem->getBackorders() != 2) {
return 0;
Expand Down
54 changes: 54 additions & 0 deletions src/Model/Resolver/Quote/Data/SalesRuleLabel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace Rapidez\Compadre\Model\Resolver\Quote\Data;

use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Framework\GraphQl\Query\ResolverInterface;
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
{
public function __construct(
protected RuleRepositoryInterface $ruleRepository
)
{}

public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
{
/** @var Item */
$cartItem = $value['model'];

$labels = [];

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

// @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;
}
}
2 changes: 2 additions & 0 deletions src/etc/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Rapidez_Compadre" setup_version="0.0.2">
<sequence>
<module name="Magento_Catalog"/>
<module name="Magento_CatalogInventory"/>
<module name="Magento_GraphQl"/>
<module name="Magento_SalesRule"/>
</sequence>
</module>
</config>
10 changes: 10 additions & 0 deletions src/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +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: [SalesRuleLabel] @resolver(class: "Rapidez\\Compadre\\Model\\Resolver\\Quote\\Data\\SalesRuleLabel") @doc(descipriont: "Labels of the sales rules")
}

0 comments on commit ad1f05c

Please sign in to comment.