-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShopwareBlogShippingCosts.php
58 lines (46 loc) · 2.13 KB
/
ShopwareBlogShippingCosts.php
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
<?php
namespace ShopwareBlogShippingCosts;
use Shopware\Components\Plugin;
class ShopwareBlogShippingCosts extends Plugin
{
public static function getSubscribedEvents()
{
return [
'Shopware_Controllers_Frontend_Checkout::getTaxRates::after' => 'afterCheckoutGetTaxRates',
];
}
public function afterCheckoutGetTaxRates(\Enlight_Hook_HookArgs $args)
{
/** @var \Shopware_Controllers_Frontend_Checkout $subject */
$subject = $args->getSubject();
$return = $args->getReturn();
$basket = $args->get('basket');
// only change behaviour if more than one tax value exists
if (count($return) > 1) {
if (!empty($basket['sShippingcostsTax'])) {
$basket['sShippingcostsTax'] = number_format(floatval($basket['sShippingcostsTax']), 2);
$return[$basket['sShippingcostsTax']] -= $basket['sShippingcostsWithTax'] - $basket['sShippingcostsNet'];
if (empty($result[$basket['sShippingcostsTax']])) {
unset($result[$basket['sShippingcostsTax']]);
}
} elseif ($basket['sShippingcostsWithTax']) {
$shippingCostTax = $basket['sShippingcostsWithTax'] - $basket['sShippingcostsNet'];
$return[number_format((float) Shopware()->Config()->get('sTAXSHIPPING'), 2)] -= $shippingCostTax;
if (empty($result[number_format((float) Shopware()->Config()->get('sTAXSHIPPING'), 2)])) {
unset($result[number_format((float) Shopware()->Config()->get('sTAXSHIPPING'), 2)]);
}
}
$basketSum = 0.00;
$shippingCostTaxes = [];
foreach ($return as $tax => $taxValue) {
$basketSum += $shippingCostTaxes[$tax] = $taxValue / $tax * ($tax + 100);
}
foreach ($shippingCostTaxes as $tax => $value) {
$taxPercentage = 100 / $basketSum * $value / 100;
$return[$tax] += $basket['sShippingcostsNet'] * $taxPercentage * $tax / 100;
}
}
$args->setReturn($return);
return $return;
}
}