-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathData.php
More file actions
154 lines (135 loc) · 4.63 KB
/
Data.php
File metadata and controls
154 lines (135 loc) · 4.63 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
/**
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
*/
/**
* @category Magenerds
* @package Magenerds_BasePrice
* @subpackage Helper
* @copyright Copyright (c) 2017 TechDivision GmbH (http://www.techdivision.com)
* @link http://www.techdivision.com/
* @author Florian Sydekum <f.sydekum@techdivision.com>
* @author Jürgen "Atlan" Schuch <juergen@atmage.de>
*/
namespace Magenerds\BasePrice\Helper;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Serialize\Serializer\Json;
/**
* Class Data
* @package Magenerds\BasePrice\Helper
*/
class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
/**
* Holds the configuration path for conversion mapping
*/
const CONVERSION_CONFIG_PATH = 'baseprice/general/conversion';
/**
* @var \Magento\Framework\Pricing\Helper\Data
*/
protected $_priceHelper;
/**
* Constructor
*
* @param \Magento\Framework\App\Helper\Context $context
* @param \Magento\Framework\Pricing\Helper\Data $priceHelper
*/
public function __construct(
\Magento\Framework\App\Helper\Context $context,
\Magento\Framework\Pricing\Helper\Data $priceHelper
){
$this->_priceHelper = $priceHelper;
parent::__construct($context);
}
/**
* Returns the configured conversion rate
*
* @param $product \Magento\Catalog\Model\Product
* @return int
*/
public function getConversion($product)
{
$productUnit = $product->getData('baseprice_product_unit');
$referenceUnit = $product->getData('baseprice_reference_unit');
$scopeConfig = $this->scopeConfig->getValue(
self::CONVERSION_CONFIG_PATH,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
);
$objectManager = ObjectManager::getInstance();
$productMetadata = $objectManager->get('Magento\Framework\App\ProductMetadataInterface');
$version = $productMetadata->getVersion();
$objectManager->get('Psr\Log\LoggerInterface')->info('Version('.var_export($version,true).')');
if($version < 2.2) {
$configArray = unserialize($scopeConfig);
} else {
$configArray = $objectManager->get(Json::class)->unserialize($scopeConfig);
}
foreach ($configArray as $config) {
if ($config['product_unit'] == $productUnit
&& $config['reference_unit'] == $referenceUnit)
{
return $config['conversion_rate'];
}
}
return 1;
}
/**
* Returns the base price text according to the configured template
*
* @param \Magento\Catalog\Model\Product $product
* @return mixed
*/
public function getBasePriceText(\Magento\Catalog\Model\Product $product)
{
$template = $this->scopeConfig->getValue(
'baseprice/general/template',
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
);
$basePrice = $this->getBasePrice($product);
if (!$basePrice) return '';
return str_replace(
'{REF_UNIT}', $this->getReferenceUnit($product), str_replace(
'{REF_AMOUNT}', $this->getReferenceAmount($product), str_replace(
'{BASE_PRICE}', $this->_priceHelper->currency($basePrice), $template)
));
}
/**
* Returns the reference unit of current product
*
* @return string
*/
public function getReferenceUnit(\Magento\Catalog\Model\Product $product)
{
return $product->getAttributeText('baseprice_reference_unit');
}
/**
* Returns the reference amount of current product
*
* @return float
*/
public function getReferenceAmount(\Magento\Catalog\Model\Product $product)
{
return round($product->getData('baseprice_reference_amount'), 2);
}
/**
* Calculates the base price for given product
*
* @return float|string
*/
public function getBasePrice(\Magento\Catalog\Model\Product $product)
{
$productPrice = $product->getFinalPrice();
$conversion = $this->getConversion($product);
$referenceAmount = $product->getData('baseprice_reference_amount');
$productAmount = $product->getData('baseprice_product_amount');
$basePrice = 0;
if ($productPrice && $conversion && $referenceAmount && $productAmount) {
$basePrice = $productPrice * $conversion * $referenceAmount / $productAmount;
}
return $basePrice;
}
}