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 all 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
Empty file modified LICENSE.txt
100644 → 100755
Empty file.
83 changes: 68 additions & 15 deletions Model/Config.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,50 @@
/**
*
* @package Lillik\PriceDecimal\Model
*
* @author Lilian Codreanu <[email protected]>
* includes Roy Nilsson's modifications ( Aug 2020 )
* Includes: differentCurrencies from quintenbuis (2021-03-14)
* Includes: diffenteDecimales from Patriboom (2021-03-01)
*/

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_precision';
const XML_PATH_PRICE_PRECISIONCHECKOUT = 'catalog_price_decimal/general/price_precision';

const XML_PATH_DIFFERENT_CURRENCY = 'catalog_price_decimal/general/different';
const XML_PATH_PRICE_PRECISION_CURRENCY= 'catalog_price_decimal/general/price_precision_currency';

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 +72,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 @@ -76,10 +87,52 @@ public function canShowPriceDecimal()
/**
* Return Price precision from store config
*
* @return mixed
* @return int
*/
public function getPricePrecision(): int
{
return (int) $this->getValueByPath(self::XML_PATH_PRICE_PRECISION, 'website');
}

/**
* Returns if the currency decimal is different
*
* @return bool
*/
public function getDifferentForCurrency(): bool
{
return (bool) $this->getValueByPath(self::XML_PATH_DIFFERENT_CURRENCY, 'website');
}

/**
* Return Price precision for currency from store config
*
* @return int
*/
public function getPricePrecision()
public function getPricePrecisionCurrency(): int
{
return $this->getValueByPath(self::XML_PATH_PRICE_PRECISION, 'website');
return (int) $this->getValueByPath(self::XML_PATH_PRICE_PRECISION_CURRENCY, '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.
6 changes: 5 additions & 1 deletion Model/Currency.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* @package package Lillik\PriceDecimal
*
* @author Lilian Codreanu <[email protected]>
* Includes: differentCurrencies from quintenbuis (2021-03-14)
*/

namespace Lillik\PriceDecimal\Model;
Expand All @@ -12,7 +13,10 @@
use Magento\Framework\Currency as MagentoCurrency;
use Lillik\PriceDecimal\Model\ConfigInterface;

/** @method getPricePrecision */
/**
* @method getPricePrecision
* @method getPricePrecisionCurrency
*/
class Currency extends MagentoCurrency implements CurrencyInterface
{

Expand Down
3 changes: 2 additions & 1 deletion Model/Plugin/Currency.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* @package package Lillik\PriceDecimal\Model\Plugin\Local
*
* @author Lilian Codreanu <[email protected]>
* Includes: differentCurrencies from quintenbuis (2021-03-14)
*/

namespace Lillik\PriceDecimal\Model\Plugin;
Expand All @@ -24,7 +25,7 @@ public function beforeToCurrency(
...$arguments
) {
if ($this->getConfig()->isEnable()) {
$arguments[1]['precision'] = $subject->getPricePrecision();
$arguments[1]['precision'] = $subject->getPricePrecisionCurrency();
}
return $arguments;
}
Expand Down
40 changes: 21 additions & 19 deletions Model/Plugin/Local/Format.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
* @package package Lillik\PriceDecimal\Model\Plugin\Local
*
* @author Lilian Codreanu <[email protected]>
*/
* Includes: differentCurrencies from quintenbuis (2021-03-14)
*/

namespace Lillik\PriceDecimal\Model\Plugin\Local;

Expand All @@ -13,23 +14,24 @@
class Format extends PriceFormatPluginAbstract
{

/**
* {@inheritdoc}
*
* @param $subject
* @param $result
*
* @return mixed
*/
public function afterGetPriceFormat($subject, $result)
{
$precision = $this->getPricePrecision();
/**
* {@inheritdoc}
*
* @param $subject
* @param $result
*
* @return mixed
*/
public function afterGetPriceFormat($subject, $result)
{
$precision = $this->getPricePrecisionCurrency();
if ($this->getConfig()->isEnable()) {
$result['precision'] = $precision;
$result['precisionCart'] = $this->getConfig()->getPricePrecisionCart();
$result['precisionCheckout'] = $this->getConfig()->getPricePrecisionCheckout();
$result['requiredPrecision'] = $precision;
}

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

return $result;
}
return $result;
}
}
Empty file modified Model/Plugin/OrderPlugin.php
100644 → 100755
Empty file.
9 changes: 4 additions & 5 deletions Model/Plugin/PriceCurrency.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function beforeFormat(
$args[1] = true;
}
// Precision argument
$args[2] = $this->getPricePrecision();
$args[2] = intval($this->getPricePrecision());
}

return $args;
Expand All @@ -39,12 +39,11 @@ public function beforeFormat(
*/
public function aroundRound(
\Magento\Directory\Model\PriceCurrency $subject,
callable $proceed,
$price,
callable $proceed, $price,
...$args
) {
if ($this->getConfig()->isEnable()) {
return round($price, $this->getPricePrecision());
return round($price, intval($this->getPricePrecision()));

} else {
return $proceed($price);
Expand Down Expand Up @@ -82,7 +81,7 @@ public function beforeConvertAndRound(
//add optional args
$args[1] = isset($args[1])? $args[1] : null;
$args[2] = isset($args[2])? $args[2] : null;
$args[3] = $this->getPricePrecision();
$args[3] = intval($this->getPricePrecision());
}

return $args;
Expand Down
Empty file modified Model/Plugin/PriceFormatPluginAbstract.php
100644 → 100755
Empty file.
70 changes: 69 additions & 1 deletion Model/PricePrecisionConfigTrait.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,99 @@
* @package Lillik\PriceDecimal
*
* @author Lilian Codreanu <[email protected]>
* Includes: differentCurrencies from quintenbuis (2021-03-14)
* Includes: diffenteDecimales from Patriboom (2021-03-01)
*/

namespace Lillik\PriceDecimal\Model;

trait PricePrecisionConfigTrait
{

/**
* @$Val used to keep track of the way to count decimals
*/
public $Val;

/**
* @$requestUri used to know which page is calling function so, which value return to
*/
public $requestUri;

/**

* @return \Lillik\PriceDecimal\Model\ConfigInterface
*/
public function getConfig()
{
return $this->moduleConfig;
}

/**
* Set the PAGE INFO string
*
* @param string|null $pathInfo
* @return $this
*/
public function getPageInfo($pathInfo = null)
{
if ($pathInfo === null) {
$requestUri = $this->getRequestUri();
if ($requestUri == '/') {
return $this;
}

// Remove the query string from REQUEST_URI
$pos = strpos($requestUri, '?');
if ($pos) {
$requestUri = substr($requestUri, 0, $pos);
}

$baseUrl = $this->getBaseUrl();
$pathInfo = substr($requestUri, strlen($baseUrl));
if (!empty($baseUrl) && '/' === $pathInfo) {
$pathInfo = '';
} elseif ($baseUrl === null) {
$pathInfo = $requestUri;
}
$pathInfo = ($pos !== false) ? substr($requestUri, $pos) : '';
$this->requestString = $pathInfo;
}
$this->pathInfo = ($pathInfo == '') ? array() : explode('/', $pathInfo);
return $this;
}

/**
* @return int|mixed
*/
public function getPricePrecision()
{
if ($this->getConfig()->canShowPriceDecimal()) {
//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 = (in_array('checkout', getPageInfo())) ? $this->getConfig()->getPricePrecisionCheckout() : $Val;
$Val = (in_array('cart', getPageInfo())) ? $this->getConfig()->getPricePrecisionCart() : $Val;
// $Val = (strstr($_SERVER['PHP_SELF'], 'checkout') > -1 ) ? $this->getConfig()->getPricePrecisionCheckout() : $Val;
// $Val = (strstr($_SERVER['PHP_SELF'], 'cart') > -1 ) ? $this->getConfig()->getPricePrecisionCart() : $Val;
return $Val;
}
return 0;
}

/**
* @return int|mixed
*/
public function getPricePrecisionCurrency()
{
if (!$this->getConfig()->canShowPriceDecimal()) {
return 0;
}

if (!$this->getConfig()->getDifferentForCurrency()) {
return $this->getConfig()->getPricePrecision();
}

return 0;
return $this->getConfig()->getPricePrecisionCurrency();
}

}
8 changes: 8 additions & 0 deletions README.md
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ 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 ( green button "Code" )
- use the way you want ( clone or Zip )
- find you Magento directory ( look like /var/www/html/MyRepositories/Magento/ or c:\MyFiles\MyWebSite\Magento )
- create in the previous directory the following sub-dir: app/code/Lillik/PricDecimal
- Copy the actual repository ( clone or unzip ) in /var/www/html/MyRepositories/Magento/app/code/Lillik/PriceDecimal


### 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
Loading