-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4948 from magento-engcom/2.3-develop-graphql-dail…
…y-prs - Community Contributions - GraphQL daily delivery
- Loading branch information
Showing
19 changed files
with
855 additions
and
14 deletions.
There are no files selected for viewing
145 changes: 145 additions & 0 deletions
145
app/code/Magento/BundleGraphQl/Model/Cart/BundleOptionDataProvider.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\BundleGraphQl\Model\Cart; | ||
|
||
use Magento\Bundle\Helper\Catalog\Product\Configuration; | ||
use Magento\Catalog\Model\Product; | ||
use Magento\Quote\Model\Quote\Item; | ||
use Magento\Framework\Pricing\Helper\Data; | ||
use Magento\Framework\Serialize\SerializerInterface; | ||
|
||
/** | ||
* Data provider for bundled product options | ||
*/ | ||
class BundleOptionDataProvider | ||
{ | ||
/** | ||
* @var Data | ||
*/ | ||
private $pricingHelper; | ||
|
||
/** | ||
* @var SerializerInterface | ||
*/ | ||
private $serializer; | ||
|
||
/** | ||
* @var Configuration | ||
*/ | ||
private $configuration; | ||
|
||
/** | ||
* @param Data $pricingHelper | ||
* @param SerializerInterface $serializer | ||
* @param Configuration $configuration | ||
*/ | ||
public function __construct( | ||
Data $pricingHelper, | ||
SerializerInterface $serializer, | ||
Configuration $configuration | ||
) { | ||
$this->pricingHelper = $pricingHelper; | ||
$this->serializer = $serializer; | ||
$this->configuration = $configuration; | ||
} | ||
|
||
/** | ||
* Extract data for a bundled cart item | ||
* | ||
* @param Item $item | ||
* @return array | ||
*/ | ||
public function getData(Item $item): array | ||
{ | ||
$options = []; | ||
$product = $item->getProduct(); | ||
|
||
/** @var \Magento\Bundle\Model\Product\Type $typeInstance */ | ||
$typeInstance = $product->getTypeInstance(); | ||
|
||
$optionsQuoteItemOption = $item->getOptionByCode('bundle_option_ids'); | ||
$bundleOptionsIds = $optionsQuoteItemOption | ||
? $this->serializer->unserialize($optionsQuoteItemOption->getValue()) | ||
: []; | ||
|
||
if ($bundleOptionsIds) { | ||
/** @var \Magento\Bundle\Model\ResourceModel\Option\Collection $optionsCollection */ | ||
$optionsCollection = $typeInstance->getOptionsByIds($bundleOptionsIds, $product); | ||
|
||
$selectionsQuoteItemOption = $item->getOptionByCode('bundle_selection_ids'); | ||
|
||
$bundleSelectionIds = $this->serializer->unserialize($selectionsQuoteItemOption->getValue()); | ||
|
||
if (!empty($bundleSelectionIds)) { | ||
$selectionsCollection = $typeInstance->getSelectionsByIds($bundleSelectionIds, $product); | ||
$bundleOptions = $optionsCollection->appendSelections($selectionsCollection, true); | ||
|
||
$options = $this->buildBundleOptions($bundleOptions, $item); | ||
} | ||
} | ||
|
||
return $options; | ||
} | ||
|
||
/** | ||
* Build bundle product options based on current selection | ||
* | ||
* @param \Magento\Bundle\Model\Option[] $bundleOptions | ||
* @param Item $item | ||
* @return array | ||
*/ | ||
private function buildBundleOptions(array $bundleOptions, Item $item): array | ||
{ | ||
$options = []; | ||
foreach ($bundleOptions as $bundleOption) { | ||
if (!$bundleOption->getSelections()) { | ||
continue; | ||
} | ||
|
||
$options[] = [ | ||
'id' => $bundleOption->getId(), | ||
'label' => $bundleOption->getTitle(), | ||
'type' => $bundleOption->getType(), | ||
'values' => $this->buildBundleOptionValues($bundleOption->getSelections(), $item), | ||
]; | ||
} | ||
|
||
return $options; | ||
} | ||
|
||
/** | ||
* Build bundle product option values based on current selection | ||
* | ||
* @param Product[] $selections | ||
* @param Item $item | ||
* @return array | ||
*/ | ||
private function buildBundleOptionValues(array $selections, Item $item): array | ||
{ | ||
$values = []; | ||
|
||
$product = $item->getProduct(); | ||
foreach ($selections as $selection) { | ||
$qty = (float) $this->configuration->getSelectionQty($product, $selection->getSelectionId()); | ||
if (!$qty) { | ||
continue; | ||
} | ||
|
||
$selectionPrice = $this->configuration->getSelectionFinalPrice($item, $selection); | ||
|
||
$values[] = [ | ||
'id' => $selection->getSelectionId(), | ||
'label' => $selection->getName(), | ||
'quantity' => $qty, | ||
'price' => $this->pricingHelper->currency($selectionPrice, false, false), | ||
]; | ||
} | ||
|
||
return $values; | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
app/code/Magento/BundleGraphQl/Model/Cart/BuyRequest/BundleDataProvider.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\BundleGraphQl\Model\Cart\BuyRequest; | ||
|
||
use Magento\Framework\Stdlib\ArrayManager; | ||
use Magento\QuoteGraphQl\Model\Cart\BuyRequest\BuyRequestDataProviderInterface; | ||
|
||
/** | ||
* Data provider for bundle product buy requests | ||
*/ | ||
class BundleDataProvider implements BuyRequestDataProviderInterface | ||
{ | ||
/** | ||
* @var ArrayManager | ||
*/ | ||
private $arrayManager; | ||
|
||
/** | ||
* @param ArrayManager $arrayManager | ||
*/ | ||
public function __construct( | ||
ArrayManager $arrayManager | ||
) { | ||
$this->arrayManager = $arrayManager; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function execute(array $cartItemData): array | ||
{ | ||
$bundleOptions = []; | ||
$bundleInputs = $this->arrayManager->get('bundle_options', $cartItemData) ?? []; | ||
foreach ($bundleInputs as $bundleInput) { | ||
$bundleOptions['bundle_option'][$bundleInput['id']] = $bundleInput['value']; | ||
$bundleOptions['bundle_option_qty'][$bundleInput['id']] = $bundleInput['quantity']; | ||
} | ||
|
||
return $bundleOptions; | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
app/code/Magento/BundleGraphQl/Model/Resolver/BundleOption.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\BundleGraphQl\Model\Resolver; | ||
|
||
use Magento\BundleGraphQl\Model\Cart\BundleOptionDataProvider; | ||
use Magento\Framework\GraphQl\Config\Element\Field; | ||
use Magento\Framework\GraphQl\Exception\GraphQlInputException; | ||
use Magento\Framework\GraphQl\Query\ResolverInterface; | ||
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; | ||
|
||
/** | ||
* Resolver for bundle product options | ||
*/ | ||
class BundleOption implements ResolverInterface | ||
{ | ||
/** | ||
* @var BundleOptionDataProvider | ||
*/ | ||
private $dataProvider; | ||
|
||
/** | ||
* @param BundleOptionDataProvider $bundleOptionDataProvider | ||
*/ | ||
public function __construct( | ||
BundleOptionDataProvider $bundleOptionDataProvider | ||
) { | ||
$this->dataProvider = $bundleOptionDataProvider; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
* @SuppressWarnings(PHPMD.UnusedFormalParameter) | ||
*/ | ||
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null) | ||
{ | ||
if (!isset($value['model'])) { | ||
throw new GraphQlInputException(__('Value must contain "model" property.')); | ||
} | ||
return $this->dataProvider->getData($value['model']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.