-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathSubscriptionAddProductToCart.php
More file actions
96 lines (78 loc) · 3 KB
/
Copy pathSubscriptionAddProductToCart.php
File metadata and controls
96 lines (78 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php
/*
* Copyright Magmodules.eu. All rights reserved.
* See COPYING.txt for license details.
*/
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;
use Magento\Quote\Model\Quote\Item;
use Magento\Tax\Model\Calculation as TaxCalculation;
use Mollie\Api\Resources\Subscription;
class SubscriptionAddProductToCart
{
/**
* @var ProductRepositoryInterface
*/
private $productRepository;
/**
* @var TaxCalculation
*/
private $taxCalculation;
public function __construct(
ProductRepositoryInterface $productRepository,
TaxCalculation $taxCalculation
) {
$this->productRepository = $productRepository;
$this->taxCalculation = $taxCalculation;
}
public function execute(CartInterface $cart, Subscription $subscription): ProductInterface
{
$metadata = $subscription->metadata;
$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) {
$item = $cart->addProduct($product, $quantity);
$this->setSubscriptionPrice($cart, $item, $subscription);
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']);
}
$item = $cart->addProduct($product, new DataObject([
'product' => $product->getId(),
'qty' => $quantity,
'super_attribute' => $options,
]));
$this->setSubscriptionPrice($cart, $item, $subscription);
return $product;
}
private function setSubscriptionPrice(CartInterface $cart, Item $item, Subscription $subscription): void
{
$request = $this->taxCalculation->getRateRequest(
$cart->getShippingAddress(),
$cart->getBillingAddress(),
$cart->getCustomerTaxClassId(),
$item->getStore()
);
$request->setProductClassId($item->getTaxClassId());
$taxRate = $this->taxCalculation->getRate($request);
$quantity = (float)($subscription->metadata->quantity ?? 1);
$priceIncl = $subscription->amount->value / $quantity;
$newPrice = $priceIncl;
if ($taxRate !== 0.0) {
$newPrice = $priceIncl / (1 + ($taxRate / 100));
}
$item->setCustomPrice($newPrice);
$item->setOriginalCustomPrice($newPrice);
}
}