-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bugfix: Add support for configurable child products
- Loading branch information
1 parent
5dd2315
commit af1fcbc
Showing
5 changed files
with
159 additions
and
19 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Mollie\Subscriptions\Service\Magento; | ||
|
||
use Magento\Catalog\Api\Data\ProductInterface; | ||
use Magento\Catalog\Api\ProductRepositoryInterface; | ||
use Magento\Framework\DataObject; | ||
use Magento\Quote\Api\Data\CartInterface; | ||
|
||
class SubscriptionAddProductToCart | ||
{ | ||
/** | ||
* @var ProductRepositoryInterface | ||
*/ | ||
private $productRepository; | ||
|
||
public function __construct( | ||
ProductRepositoryInterface $productRepository | ||
) { | ||
$this->productRepository = $productRepository; | ||
} | ||
|
||
public function execute(CartInterface $cart, object $metadata): ProductInterface | ||
{ | ||
$sku = $metadata->sku; | ||
$parentSku = isset($metadata->parent_sku) ? $metadata->parent_sku : null; | ||
$quantity = isset($metadata->quantity) ? (float)$metadata->quantity : 1; | ||
|
||
$product = $this->productRepository->get($parentSku ?: $sku); | ||
$cart->setIsVirtual($product->getIsVirtual()); | ||
|
||
if (!$parentSku) { | ||
$cart->addProduct($product, $quantity); | ||
|
||
return $product; | ||
} | ||
|
||
$childProduct = $this->productRepository->get($sku); | ||
$productAttributeOptions = $product->getTypeInstance(true)->getConfigurableAttributesAsArray($product); | ||
|
||
$options = []; | ||
foreach($productAttributeOptions as $option) { | ||
$options[$option['attribute_id']] = $childProduct->getData($option['attribute_code']); | ||
} | ||
|
||
$cart->addProduct($product, new DataObject([ | ||
'product' => $product->getId(), | ||
'qty' => $quantity, | ||
'super_attribute' => $options, | ||
])); | ||
|
||
return $product; | ||
} | ||
} |
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