Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Different decimal lenght for cart and checkout -- first version #57

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*.~
error_log
.gitignore
.htaccess~
1 change: 1 addition & 0 deletions Block/System/Config/Form/Field/Precision.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class Precision implements \Magento\Framework\Option\ArrayInterface
public function toOptionArray()
{
return [
['value' => 0, 'label' => __('0')],
['value' => 1, 'label' => __('1')],
['value' => 2, 'label' => __('2')],
['value' => 3, 'label' => __('3')],
Expand Down
30 changes: 30 additions & 0 deletions Block/System/Config/Form/Field/PrecisionCart.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
Patriboom marked this conversation as resolved.
Show resolved Hide resolved
/**
* @category Evozon
* @package Magento_Evozon
* @copyright Copyright (c) 2018 Evozon (http://www.evozon.com)
* @author Lilian Codreanu <[email protected]>
*/

namespace Lillik\PriceDecimal\Block\System\Config\Form\Field;


class Precision implements \Magento\Framework\Option\ArrayInterface
{

/**
* Return array of options as value-label pairs
*
* @return array Format: array(array('value' => '<value>', 'label' => '<label>'), ...)
*/
public function toOptionArray()
{
return [
['value' => 0, 'label' => __('0')],
['value' => 1, 'label' => __('1')],
['value' => 2, 'label' => __('2')],
['value' => 3, 'label' => __('3')],
['value' => 4, 'label' => __('4')],
];
}
}
30 changes: 30 additions & 0 deletions Block/System/Config/Form/Field/PrecisionCheckout.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
/**
* @category Evozon
* @package Magento_Evozon
* @copyright Copyright (c) 2018 Evozon (http://www.evozon.com)
* @author Lilian Codreanu <[email protected]>
*/

namespace Lillik\PriceDecimal\Block\System\Config\Form\Field;


class Precision implements \Magento\Framework\Option\ArrayInterface
Patriboom marked this conversation as resolved.
Show resolved Hide resolved
{

/**
* Return array of options as value-label pairs
*
* @return array Format: array(array('value' => '<value>', 'label' => '<label>'), ...)
*/
public function toOptionArray()
{
return [
['value' => 0, 'label' => __('0')],
['value' => 1, 'label' => __('1')],
['value' => 2, 'label' => __('2')],
['value' => 3, 'label' => __('3')],
['value' => 4, 'label' => __('4')],
];
}
}
Empty file modified LICENSE.txt
100644 → 100755
Empty file.
52 changes: 39 additions & 13 deletions Model/Config.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,46 @@
/**
*
* @package Lillik\PriceDecimal\Model
*
* @author Lilian Codreanu <[email protected]>
* includes Roy Nilsson's modifications ( Aug 2020 )
* includes Patriboom's modifications ( Mar 2021 )
*/

namespace Lillik\PriceDecimal\Model;

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\RequestInterface;

class Config implements ConfigInterface
{

const XML_PATH_PRICE_PRECISION
= 'catalog_price_decimal/general/price_precision';
const XML_PATH_GENERAL_ENABLE = 'catalog_price_decimal/general/enable';
const XML_PATH_CAN_SHOW_PRICE_DECIMAL = 'catalog_price_decimal/general/can_show_decimal';
const XML_PATH_DISABLE_FOR_ACTIONS = 'catalog_price_decimal/general/disable_for_actions';

const XML_PATH_CAN_SHOW_PRICE_DECIMAL
= 'catalog_price_decimal/general/can_show_decimal';
const XML_PATH_PRICE_PRECISION = 'catalog_price_decimal/general/price_precision';
const XML_PATH_PRICE_PRECISIONCART = 'catalog_price_decimal/general/price_precisionCart';
const XML_PATH_PRICE_PRECISIONCHECKOUT = 'catalog_price_decimal/general/price_precisionCheckout';

const XML_PATH_GENERAL_ENABLE
= 'catalog_price_decimal/general/enable';

/**
* @var \Magento\Framework\App\Config\ScopeConfigInterface
*/
private $scopeConfig;

/**
* @var \Magento\Framework\App\RequestInterface
*/
private $request;

/**
* @param ScopeConfigInterface $scopeConfig
*/
public function __construct(
ScopeConfigInterface $scopeConfig
) {

public function __construct( ScopeConfigInterface $scopeConfig, RequestInterface $request )
{
$this->scopeConfig = $scopeConfig;
}
$this->request = $request;
}

/**
* @return \Magento\Framework\App\Config\ScopeConfigInterface
Expand All @@ -62,7 +68,8 @@ public function getValueByPath($path, $scopeType = 'website')
*/
public function isEnable()
{
return $this->getValueByPath(self::XML_PATH_GENERAL_ENABLE, 'website');
//return $this->getValueByPath(self::XML_PATH_GENERAL_ENABLE, 'website');
return $this->getValueByPath(self::XML_PATH_GENERAL_ENABLE, 'website') && !$this->isDisabledForAction();
}

/**
Expand All @@ -82,4 +89,23 @@ public function getPricePrecision()
{
return $this->getValueByPath(self::XML_PATH_PRICE_PRECISION, 'website');
}
public function getPricePrecisionCart()
{
return $this->getValueByPath(self::XML_PATH_PRICE_PRECISIONCART, 'website');
}
public function getPricePrecisionCheckout()
{
return $this->getValueByPath(self::XML_PATH_PRICE_PRECISIONCHECKOUT, 'website');
}

private function isDisabledForAction()
{
$currentAction = $this->request->getModuleName() . '_' . $this->request->getControllerName() . '_' . $this->request->getActionName();
foreach (explode(',', $this->getValueByPath(self::XML_PATH_DISABLE_FOR_ACTIONS, 'website')) as $action) {
if (trim($action) == $currentAction) {
return true;
}
}
return false;
}
}
Empty file modified Model/ConfigInterface.php
100644 → 100755
Empty file.
Empty file modified Model/Currency.php
100644 → 100755
Empty file.
Empty file modified Model/Plugin/Currency.php
100644 → 100755
Empty file.
6 changes: 4 additions & 2 deletions Model/Plugin/Local/Format.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ public function afterGetPriceFormat($subject, $result)
$precision = $this->getPricePrecision();

if ($this->getConfig()->isEnable()) {
$result['precision'] = $precision;
$result['requiredPrecision'] = $precision;
$result['precision'] = $precision;
$result['precisionCart'] = $this->getConfig()->getPricePrecisionCart();
$result['precisionCheckout'] = $this->getConfig()->getPricePrecisionCheckout();
$result['requiredPrecision'] = $precision;
}

return $result;
Expand Down
Empty file modified Model/Plugin/OrderPlugin.php
100644 → 100755
Empty file.
Empty file modified Model/Plugin/PriceCurrency.php
100644 → 100755
Empty file.
Empty file modified Model/Plugin/PriceFormatPluginAbstract.php
100644 → 100755
Empty file.
6 changes: 5 additions & 1 deletion Model/PricePrecisionConfigTrait.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ public function getConfig()
public function getPricePrecision()
{
if ($this->getConfig()->canShowPriceDecimal()) {
return $this->getConfig()->getPricePrecision();
//return $context->getLayout();
$Val = $this->getConfig()->getPricePrecision();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, declare the local variables according to PSR.

$Val = (strstr($_SERVER['PHP_SELF'], 'checkout') > -1 ) ? $this->getConfig()->getPricePrecisionCheckout() : $Val;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't use global variables.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hope to find the proper variable or object to this. Meanwhile, I'm looking for the solution, please accept this way. I don't want to steal your project. I just want to contribute; that's why I don't want nor try to bring users to my fork, but give them as soon as possible a solution ( even if it's not perfect ). During that time, we'll find the proper way to achieve this code perfectly.

$Val = (strstr($_SERVER['PHP_SELF'], 'cart') > -1 ) ? $this->getConfig()->getPricePrecisionCart() : $Val;
return $Val;
}

return 0;
Expand Down
2 changes: 2 additions & 0 deletions README.md
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ The preferred way of installing lillik/magento2-price-decimal is through Compose
php composer.phar require lillik/magento2-price-decimal
```

Other way to install: copy all files from here to ./app/code/Lillik/PriceDecimal/ subdirectory ( you'll need to create the directory first).
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, define and describe the steps on this way of installation, to be clear for all other developers that choose this way.


### Configuration

```
Expand Down
Empty file modified Test/Unit/Plugin/Model/PriceCurrencyTest.php
100644 → 100755
Empty file.
Empty file modified Ui/DataProvider/Product/Modifier/Price.php
100644 → 100755
Empty file.
Empty file modified composer.json
100644 → 100755
Empty file.
Empty file modified etc/acl.xml
100644 → 100755
Empty file.
8 changes: 8 additions & 0 deletions etc/adminhtml/di.xml
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@
<item name="class" xsi:type="string">Lillik\PriceDecimal\Ui\DataProvider\Product\Modifier\Price</item>
<item name="sortOrder" xsi:type="number">200</item>
</item>
<item name="pricetabcart" xsi:type="array">
<item name="class" xsi:type="string">Lillik\PriceDecimal\Ui\DataProvider\Product\Modifier\Price</item>
<item name="sortOrder" xsi:type="number">200</item>
</item>
<item name="pricetabcheckout" xsi:type="array">
<item name="class" xsi:type="string">Lillik\PriceDecimal\Ui\DataProvider\Product\Modifier\Price</item>
<item name="sortOrder" xsi:type="number">200</item>
</item>
</argument>
</arguments>
</virtualType>
Expand Down
26 changes: 23 additions & 3 deletions etc/adminhtml/system.xml
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,37 @@
<depends>
<field id="*/*/enable">1</field>
</depends>
<label>Show the decimal?</label>
<label>Show decimal ?</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
<field id="price_precision" translate="label" type="select" sortOrder="3" showInDefault="1" showInWebsite="1" showInStore="0">
<label>Decimal Length</label>
<label>Decimal length -- general purpose</label>
<depends>
<field id="*/*/enable">1</field>
</depends>
<source_model>Lillik\PriceDecimal\Block\System\Config\Form\Field\Precision</source_model>
</field>
</group>
<field id="price_precisionCart" translate="label" type="select" sortOrder="3" showInDefault="1" showInWebsite="1" showInStore="0">
<label>Decimal lenght -- Cart</label>
<depends>
<field id="*/*/enable">1</field>
</depends>
<source_model>Lillik\PriceDecimal\Block\System\Config\Form\Field\Precision</source_model>
</field>
<field id="price_precisionCheckout" translate="label" type="select" sortOrder="3" showInDefault="1" showInWebsite="1" showInStore="0">
<label>Decimal lenght -- Checkout</label>
<depends>
<field id="*/*/enable">1</field>
</depends>
<source_model>Lillik\PriceDecimal\Block\System\Config\Form\Field\Precision</source_model>
</field>
<field id="disable_for_actions" translate="label" type="text" sortOrder="4" showInDefault="1" showInWebsite="1" showInStore="0">
<label>Disable for actions</label>
<depends>
<field id="*/*/enable">1</field>
</depends>
</field>
</group>
</section>
</system>
</config>
21 changes: 21 additions & 0 deletions etc/config.xml
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@
<price_precision>2</price_precision>
</general>
</catalog_price_decimal>
<catalog_price_decimalCart>
<general>
<price_precisionCart>2</price_precisionCart>
<disable_for_actions>checkout_klarna_index</disable_for_actions>
</general>
</catalog_price_decimalCart>
<catalog_price_decimalCheckout>
<general>
<price_precisionCheckout>2</price_precisionCheckout>
</general>
</catalog_price_decimalCheckout>
</default>
<websites>
<admin>
Expand All @@ -14,6 +25,16 @@
<enable>0</enable>
</general>
</catalog_price_decimal>
<catalog_price_decimalCart>
<general>
<enable>0</enable>
</general>
</catalog_price_decimalCart>
<catalog_price_decimalCheckout>
<general>
<enable>0</enable>
</general>
</catalog_price_decimalCheckout>
</admin>
</websites>
</config>
Empty file modified etc/di.xml
100644 → 100755
Empty file.
Empty file modified etc/module.xml
100644 → 100755
Empty file.
Empty file modified registration.php
100644 → 100755
Empty file.
Empty file modified view/frontend/requirejs-config.js
100644 → 100755
Empty file.
Empty file modified view/frontend/web/js/price-utils.js
100644 → 100755
Empty file.