Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented entered options data provider #1

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php
/**
* Copyright © MageWorx. All rights reserved.
* See LICENSE.txt for license details.
*/
declare(strict_types=1);

namespace MageWorx\GiftCardsGraphQl\Model\Cart\BuyRequest;

use Magento\Framework\Exception\LocalizedException;
use Magento\Quote\Model\Cart\BuyRequest\BuyRequestDataProviderInterface;
use Magento\Quote\Model\Cart\Data\CartItem;

/**
* DataProvider for building gift card options in buy requests
*/
class GiftCardProductEnteredOptionsDataProvider implements BuyRequestDataProviderInterface
{
private const OPTION_TYPE = 'mw_giftcard';

/**
* @inheritdoc
*
* @throws LocalizedException
*/
public function execute(CartItem $cartItem): array
{
$giftCardProductData = [];
foreach ($cartItem->getEnteredOptions() as $option) {
// phpcs:ignore Magento2.Functions.DiscouragedFunction
$optionData = \explode('/', base64_decode($option->getUid()));

if ($this->isProviderApplicable($optionData) === false) {
continue;
}
$this->validateInput($optionData);

[$optionType, $optionId] = $optionData;
if ($optionType == self::OPTION_TYPE) {
$giftCardProductData[$optionId] = $option->getValue();
}
}

return $giftCardProductData;
}

/**
* Checks whether this provider is applicable for the current option
*
* @param array $optionData
* @return bool
*/
private function isProviderApplicable(array $optionData): bool
{
if ($optionData[0] !== self::OPTION_TYPE) {
return false;
}

return true;
}

/**
* Validates the provided options structure
*
* @param array $optionData
* @throws LocalizedException
*/
private function validateInput(array $optionData): void
{
if (count($optionData) !== 2) {
throw new LocalizedException(
__('Wrong format of the entered option data')
);
}
}
}
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -586,3 +586,36 @@ mutation {
}
}
```

**7.** The **addProductsToCart** mutation allows you to add any product to the cart.

Syntax:

```
mutation: {addProductsToCart(cartId: String! cartItems: [CartItemInput]!): AddProductsToCartOutput}
```

The CartItemInput object must contain the following attributes:

```
entered_options: [{uid: ID!, value: String!}]
quantity: Float!
sku: String!
```

**entered_options**

You need to add gift card product cart item attributes array here, as uid and value.

**uid**

Base64 encoded cart item attribute starting with 'mw_giftcard/' prefix. For example:

```
mw_giftcard/mail_to_email
```

Base64 encoded uid
```
bXdfZ2lmdGNhcmQvbWFpbF90b19lbWFpbA==
```
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"magento/module-url-rewrite-graph-ql": ">= 100.4.2 < 100.5"
},
"type": "magento2-module",
"version": "1.2.2",
"version": "1.2.3",
"license": [
"OSL-3.0",
"AFL-3.0"
Expand Down
7 changes: 7 additions & 0 deletions etc/graphql/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@
</argument>
</arguments>
</type>
<type name="Magento\Quote\Model\Cart\BuyRequest\BuyRequestBuilder">
<arguments>
<argument name="providers" xsi:type="array">
<item name="giftcard" xsi:type="object">MageWorx\GiftCardsGraphQl\Model\Cart\BuyRequest\GiftCardProductEnteredOptionsDataProvider</item>
</argument>
</arguments>
</type>
<type name="Magento\Framework\GraphQl\Schema\Type\Enum\DefaultDataMapper">
<arguments>
<argument name="map" xsi:type="array">
Expand Down