Skip to content

Commit d97b9f0

Browse files
author
Oleksii Korshenko
committed
Merge remote-tracking branch 'mainline/develop' into contribution-md-update
2 parents 2f4f331 + f6b6b4b commit d97b9f0

File tree

5 files changed

+422
-5
lines changed

5 files changed

+422
-5
lines changed

app/code/Magento/Tax/Observer/AfterAddressSaveObserver.php

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
namespace Magento\Tax\Observer;
77

8+
use Magento\Customer\Model\Address;
89
use Magento\Customer\Model\Session;
910
use Magento\Framework\Event\Observer;
1011
use Magento\Framework\Event\ObserverInterface;
@@ -69,7 +70,7 @@ public function execute(Observer $observer)
6970
$address = $observer->getCustomerAddress();
7071

7172
// Check if the address is either the default billing, shipping, or both
72-
if ($address->getIsPrimaryBilling() || $address->getIsDefaultBilling()) {
73+
if ($this->isDefaultBilling($address)) {
7374
$this->customerSession->setDefaultTaxBillingAddress(
7475
[
7576
'country_id' => $address->getCountryId(),
@@ -78,8 +79,8 @@ public function execute(Observer $observer)
7879
]
7980
);
8081
}
81-
82-
if ($address->getIsPrimaryShipping() || $address->getIsDefaultShipping()) {
82+
83+
if ($this->isDefaultShipping($address)) {
8384
$this->customerSession->setDefaultTaxShippingAddress(
8485
[
8586
'country_id' => $address->getCountryId(),
@@ -90,4 +91,30 @@ public function execute(Observer $observer)
9091
}
9192
}
9293
}
94+
95+
/**
96+
* Check whether specified billing address is default for its customer
97+
*
98+
* @param Address $address
99+
* @return bool
100+
*/
101+
protected function isDefaultBilling($address)
102+
{
103+
return $address->getId() && $address->getId() == $address->getCustomer()->getDefaultBilling()
104+
|| $address->getIsPrimaryBilling()
105+
|| $address->getIsDefaultBilling();
106+
}
107+
108+
/**
109+
* Check whether specified shipping address is default for its customer
110+
*
111+
* @param Address $address
112+
* @return bool
113+
*/
114+
protected function isDefaultShipping($address)
115+
{
116+
return $address->getId() && $address->getId() == $address->getCustomer()->getDefaultShipping()
117+
|| $address->getIsPrimaryShipping()
118+
|| $address->getIsDefaultShipping();
119+
}
93120
}
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Tax\Observer;
7+
8+
use Magento\Framework\Event\ObserverInterface;
9+
use Magento\Catalog\Pricing\Price\BasePrice;
10+
use Magento\Catalog\Pricing\Price\RegularPrice;
11+
12+
class GetPriceConfigurationObserver implements ObserverInterface
13+
{
14+
/**
15+
* Tax data
16+
*
17+
* @var \Magento\Tax\Helper\Data
18+
*/
19+
protected $taxData;
20+
21+
/** @var \Magento\Framework\Registry */
22+
protected $registry;
23+
24+
/**
25+
* @param \Magento\Framework\Registry $registry
26+
* @param \Magento\Tax\Helper\Data $taxData
27+
*/
28+
public function __construct(
29+
\Magento\Framework\Registry $registry,
30+
\Magento\Tax\Helper\Data $taxData
31+
) {
32+
$this->registry = $registry;
33+
$this->taxData = $taxData;
34+
}
35+
36+
/**
37+
* Modify the bundle config for the front end to resemble the tax included price when tax included prices
38+
*
39+
* @param \Magento\Framework\Event\Observer $observer
40+
* @return $this
41+
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
42+
*/
43+
public function execute(\Magento\Framework\Event\Observer $observer)
44+
{
45+
if ($this->taxData->displayPriceIncludingTax()) {
46+
/** @var \Magento\Catalog\Model\Product $product */
47+
$product = $this->registry->registry('current_product');
48+
if ($product->getTypeId() == \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE) {
49+
$priceConfigObj = $observer->getData('configObj');
50+
try {
51+
$priceConfig = $this->recurConfigAndUpdatePrice(
52+
$priceConfigObj->getConfig(),
53+
'prices'
54+
);
55+
$priceConfigObj->setConfig($priceConfig);
56+
} catch (\Exception $e) {
57+
return $this;
58+
}
59+
}
60+
}
61+
return $this;
62+
}
63+
64+
/**
65+
* Recurse through the config array and modify the base price
66+
*
67+
* @param array $input
68+
* @param string $searchKey
69+
* @return array
70+
*/
71+
private function recurConfigAndUpdatePrice($input, $searchKey)
72+
{
73+
$holder = [];
74+
if (is_array($input)) {
75+
foreach ($input as $key => $el) {
76+
if (is_array($el)) {
77+
$holder[$key] =
78+
$this->recurConfigAndUpdatePrice($el, $searchKey);
79+
if ($key === $searchKey) {
80+
if ((array_key_exists('basePrice', $holder[$key]))) {
81+
if (array_key_exists('optionId', $input)) {
82+
$holder = $this->updatePriceForBundle($holder, $key);
83+
}
84+
}
85+
}
86+
} else {
87+
$holder[$key] = $el;
88+
}
89+
}
90+
}
91+
return $holder;
92+
}
93+
94+
/**
95+
* Update the base price for bundle product option
96+
*
97+
* @param array $holder
98+
* @param int|string $key
99+
* @return array
100+
*/
101+
private function updatePriceForBundle($holder, $key)
102+
{
103+
if (array_key_exists($key, $holder)) {
104+
if (array_key_exists('basePrice', $holder[$key])) {
105+
/** @var \Magento\Catalog\Model\Product $product */
106+
$product = $this->registry->registry('current_product');
107+
if ($product->getTypeId() == \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE) {
108+
$typeInstance = $product->getTypeInstance();
109+
$typeInstance->setStoreFilter($product->getStoreId(), $product);
110+
111+
$selectionCollection = $typeInstance->getSelectionsCollection(
112+
$typeInstance->getOptionsIds($product),
113+
$product
114+
);
115+
116+
foreach ($selectionCollection->getItems() as $selectionItem) {
117+
if ($holder['optionId'] == $selectionItem->getId()) {
118+
/** @var \Magento\Framework\Pricing\Amount\Base $baseAmount */
119+
$baseAmount = $selectionItem->getPriceInfo()->getPrice(BasePrice::PRICE_CODE)->getAmount();
120+
/** @var \Magento\Framework\Pricing\Amount\Base $oldAmount */
121+
$oldAmount =
122+
$selectionItem->getPriceInfo()->getPrice(RegularPrice::PRICE_CODE)->getAmount();
123+
if ($baseAmount->hasAdjustment('tax')) {
124+
$holder[$key]['basePrice']['amount'] =
125+
$baseAmount->getBaseAmount() + $baseAmount->getAdjustmentAmount('tax');
126+
$holder[$key]['oldPrice']['amount'] =
127+
$oldAmount->getBaseAmount() + $oldAmount->getAdjustmentAmount('tax');
128+
}
129+
}
130+
}
131+
}
132+
}
133+
}
134+
return $holder;
135+
}
136+
}

0 commit comments

Comments
 (0)