-
Notifications
You must be signed in to change notification settings - Fork 32
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 #99 from IrinaZhadzinets/issue-3701
#3701 - Getting cart using default magento functions
- Loading branch information
Showing
15 changed files
with
517 additions
and
1,634 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,62 @@ | ||
<?php | ||
/** | ||
* ScandiPWA - Progressive Web App for Magento | ||
* | ||
* Copyright © Scandiweb, Inc. All rights reserved. | ||
* See LICENSE for license details. | ||
* | ||
* @license OSL-3.0 (Open Software License ("OSL") v. 3.0) | ||
* @package scandipwa/quote-graphql | ||
* @link https://github.com/scandipwa/quote-graphql | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ScandiPWA\QuoteGraphQl\Model\Resolver; | ||
|
||
use Magento\Framework\Exception\LocalizedException; | ||
use Magento\Framework\GraphQl\Config\Element\Field; | ||
use Magento\Framework\GraphQl\Query\ResolverInterface; | ||
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; | ||
use Magento\InventoryInStorePickupShippingApi\Model\IsInStorePickupDeliveryAvailableForCartInterface; | ||
|
||
/** | ||
* Class CartIsInStorePickupAvailable | ||
* @package ScandiPWA\QuoteGraphQl\Model\Resolver | ||
*/ | ||
class CartIsInStorePickupAvailable implements ResolverInterface | ||
{ | ||
/** | ||
* @var IsInStorePickupDeliveryAvailableForCartInterface | ||
*/ | ||
protected IsInStorePickupDeliveryAvailableForCartInterface $inStorePickupDeliveryAvailableForCart; | ||
|
||
/** | ||
* CartIsInStorePickupAvailable constructor. | ||
* @param ValidationResultFactory $validationResultFactory | ||
*/ | ||
public function __construct( | ||
IsInStorePickupDeliveryAvailableForCartInterface $inStorePickupDeliveryAvailableForCart | ||
) { | ||
$this->inStorePickupDeliveryAvailableForCart = $inStorePickupDeliveryAvailableForCart; | ||
} | ||
|
||
/** | ||
* @param Field $field | ||
* @param ContextInterface $context | ||
* @param ResolveInfo $info | ||
* @param array|null $value | ||
* @param array|null $args | ||
* @return array | ||
*/ | ||
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null) | ||
{ | ||
if (!isset($value['model'])) { | ||
throw new LocalizedException(__('"model" value should be specified')); | ||
} | ||
|
||
$cart = $value['model']; | ||
|
||
return $this->inStorePickupDeliveryAvailableForCart->execute((int) $cart->getId()); | ||
} | ||
} |
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,156 @@ | ||
<?php | ||
/** | ||
* ScandiPWA - Progressive Web App for Magento | ||
* | ||
* Copyright © Scandiweb, Inc. All rights reserved. | ||
* See LICENSE for license details. | ||
* | ||
* @license OSL-3.0 (Open Software License ("OSL") v. 3.0) | ||
* @package scandipwa/quote-graphql | ||
* @link https://github.com/scandipwa/quote-graphql | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ScandiPWA\QuoteGraphQl\Model\Resolver; | ||
|
||
use Magento\Catalog\Helper\Image as HelperImage; | ||
use Magento\Framework\App\Area; | ||
use Magento\Framework\GraphQl\Config\Element\Field; | ||
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; | ||
use Magento\Framework\GraphQl\Query\Uid; | ||
use Magento\QuoteGraphQl\Model\Cart\GetCartProducts; | ||
use Magento\QuoteGraphQl\Model\Resolver\CartItems as SourceCartItems; | ||
use Magento\Store\Model\App\Emulation; | ||
use Magento\Store\Model\StoreManagerInterface; | ||
|
||
/** | ||
* Class CartItems | ||
* @package ScandiPWA\QuoteGraphQl\Model\Resolver | ||
*/ | ||
class CartItems extends SourceCartItems | ||
{ | ||
/** | ||
* @var GetCartProducts | ||
*/ | ||
protected GetCartProducts $getCartProducts; | ||
|
||
/** | ||
* @var Uid | ||
*/ | ||
protected Uid $uidEncoder; | ||
|
||
/** | ||
* @var Emulation | ||
*/ | ||
protected Emulation $emulation; | ||
|
||
/** | ||
* @var HelperImage | ||
*/ | ||
protected HelperImage $helperImage; | ||
|
||
/** | ||
* @var StoreManagerInterface | ||
*/ | ||
protected StoreManagerInterface $storeManager; | ||
|
||
/** | ||
* @param GetCartProducts $getCartProducts | ||
* @param Uid $uidEncoder | ||
* @param Emulation $emulation | ||
* @param HelperImage $helperImage | ||
* @param StoreManagerInterface $storeManager | ||
*/ | ||
public function __construct( | ||
GetCartProducts $getCartProducts, | ||
Uid $uidEncoder, | ||
Emulation $emulation, | ||
HelperImage $helperImage, | ||
StoreManagerInterface $storeManager | ||
) { | ||
parent::__construct( | ||
$getCartProducts, | ||
$uidEncoder | ||
); | ||
|
||
$this->emulation = $emulation; | ||
$this->helperImage = $helperImage; | ||
$this->storeManager = $storeManager; | ||
} | ||
|
||
/** | ||
* @param Field $field | ||
* @param ContextInterface $context | ||
* @param ResolveInfo $info | ||
* @param array|null $value | ||
* @param array|null $args | ||
* @return array | ||
*/ | ||
public function resolve( | ||
Field $field, | ||
$context, | ||
ResolveInfo $info, | ||
array $value = null, | ||
array $args = null | ||
) { | ||
$result = parent::resolve($field, $context, $info, $value, $args); | ||
|
||
$cartItems = []; | ||
|
||
$storeId = $this->storeManager->getStore()->getId(); | ||
$this->emulation->startEnvironmentEmulation($storeId, Area::AREA_FRONTEND, true); | ||
|
||
foreach ($result as $itemData) { | ||
$cartItem = $itemData['model']; | ||
$product = $cartItem->getProduct(); | ||
|
||
$cartItems[] = array_merge($itemData, [ | ||
'sku' => $cartItem->getSku(), | ||
'product' => array_merge($itemData['product'], | ||
[ | ||
'thumbnail' => | ||
[ | ||
'path' => $product->getThumbnail(), | ||
'url' => $this->getImageUrl('thumbnail', $product->getThumbnail(), $product) | ||
] | ||
]) | ||
]); | ||
} | ||
|
||
$this->emulation->stopEnvironmentEmulation(); | ||
|
||
return $cartItems; | ||
} | ||
|
||
/** | ||
* @param string $imageType | ||
* @param string|null $imagePath | ||
* @param Product $product | ||
* @return string | ||
*/ | ||
protected function getImageUrl( | ||
string $imageType, | ||
?string $imagePath, | ||
$product | ||
): string { | ||
if (!isset($imagePath)) { | ||
return $this->helperImage->getDefaultPlaceholderUrl($imageType); | ||
} | ||
|
||
$imageId = sprintf('scandipwa_%s', $imageType); | ||
|
||
$image = $this->helperImage | ||
->init( | ||
$product, | ||
$imageId, | ||
['type' => $imageType] | ||
) | ||
->constrainOnly(true) | ||
->keepAspectRatio(true) | ||
->keepTransparency(true) | ||
->keepFrame(false); | ||
|
||
return $image->getUrl(); | ||
} | ||
} |
Oops, something went wrong.