📜 ⬆️ ⬇️

How “crooked” to remove the display of cents from the price in Magento, or a couple of words about the redefinition of standard classes

I want to share a working (although, of course, somewhat crooked) solution, if necessary, to remove the display of kopecks in prices. Once again, it changes only the display format, real prices do not change, and this is not rounding - tenths are simply discarded, so it is quite possible that the apparent amount of goods in the basket will differ from the real amount if you add up all the bars. This can be avoided by controlling the absence of kopecks in the prices of goods when importing or manually changing the price.

I must say right away that the actual decision is not mine; I peeped it here and decided to issue it correctly.

When developing my online store interface, I was faced with the task of not only making the right design and logic, but also ensuring a smooth upgrade of the engine to the next versions, so I excluded editing the system files right away. However, Magento provides excellent tools for developing your own extensions, including the ability to replace the standard functionality with your own. This is what we will do.

So, we need to do the following:
- override the class responsible for outputting a formatted price;
- create a module that will contain this class;
- configure the module so that the overridden class is called in cases in which the original class was previously activated;
- activate a new module in the system.
')
All paths listed are relative to the directory in which Magento is installed.

As I already said, Magento represents good opportunities for developing your own modules and extending the basic functionality. The main part of the working system code (with the exception of the libraries and frameworks on which it is written) is located in the app directory. If we look inside, we will see the following contents there:

app
| ----- Mage.php
| ----- code
| ----- design
| ----- etc
| ----- locale

Mage.php - a module describing the main class hub of the system - Mage
code - all code
design - as the name implies, design descriptions are located here - it is the logic and output patterns of the blocks; descriptions of css-styles directly, scripts and images are moved to a separate place
etc - configuration files
locale - basic language files, or, in other words, localization of output; for a specific interface, localized output can also be partially or completely redefined in the descriptions of its own interface in the design subdirectories.

For this task, we are interested in the code directory. In it, we see three standard folders: core - contains the code for the main modules of the system, as well as community and local , which are initially empty and are designed to install third-party modules (community) or for those developed independently (local). In fact, there are no differences between folders, but for convenience, we will add our own modules to the local folder.

So, first we need to create a folder that will contain our modules. The name of this folder will be the name of the package (package) of modules that we will develop. Because I developed modules for my project, my folder is named after the project - Cifrum.

%pwd
//app/code
%ll -a
total 10
drwxrwxr-x 5 vlad www 512 18 09:37 .
drwxrwxr-x 6 vlad www 512 29 19:30 ..
drwxrwxr-x 3 vlad www 512 29 19:30 community
drwxrwxr-x 4 vlad www 512 18 09:37 core
drwxrwxr-x 3 vlad www 512 27 02:37 local
%ll -a local
total 6
drwxrwxr-x 3 vlad www 512 27 02:37 .
drwxrwxr-x 5 vlad www 512 18 09:37 ..
drwxrwxr-x 6 vlad www 512 29 23:48 Cifrum


, , . , , Core , CoreC .

:

%ll -1aR //app/code/local/
.
..
Cifrum

//app/code/local/Cifrum:
.
..
CoreC

//app/code/local/Cifrum/CoreC:
.
..
Block
Helper
Model
controllers
etc
sql


- . , , php|architect's Guide to Programming with Magento.

- etc, config.xml, Model, .

Model/Store.php :

<?php

/*****

Trying to rewrite Core_Model_Store

*/

// , ,
// app/code/core/Mage/Core/Model/Store.php

class Cifrum_CoreC_Model_Store extends Mage_Core_Model_Store
{


/**
*
* formatPrice without decimals, for rubles only for right now
*
*/

// ,

public function formatPrice($price, $includeContainer = true )
{
if ($ this ->getCurrentCurrency()) {
$priceReturn = $ this ->getCurrentCurrency()->format($price, array(), $includeContainer);

//Not the cleanest method but the fastest for now…
if (preg_match( '//i' , $priceReturn)) {
return $ this ->getCurrentCurrency()->format($price, array( 'precision' => 0), $includeContainer);
} else {
return $priceReturn;
}
}

return $price;
}

}

* This source code was highlighted with Source Code Highlighter .


?>


, Mage_Core_Model_Store::formatPrice() "". , ( , - ""), .

, . etc/config.xml :

<? xml version ="1.0" ? >
< config >
< modules >


< Cifrum_CoreC >
< version > 0.0.1 </ version >
< depends >

</ depends >
</ Cifrum_CoreC >


</ modules >
< global >
< models >



< core >
< rewrite >
< store > Cifrum_CoreC_Model_Store </ store >
</ rewrite >
</ core >


</ models >
< resources ></ resources >
< blocks ></ blocks >
< corec >

</ corec >
</ global >
< adminhtml >
< menu ></ menu >
< acl ></ acl >
< events ></ events >
< translate ></ translate >
</ adminhtml >
< frontend >
< routers ></ routers >
< events ></ events >
< translate ></ translate >
< layout ></ layout >
</ frontend >
< default >
< config_vars >

</ config_vars >
</ default >
</ config >


* This source code was highlighted with Source Code Highlighter .



-, , . . , store core .

. , , .
, app/code/etc . app/etc/modules/Cifrum_All.xml, Cifrum.

<? xml version ="1.0" ? >
< config >
< modules >
< Cifrum_CoreC >
< active > true </ active >
< codePool > local </ codePool >
</ Cifrum_CoreC >
</ modules >
</ config >

* This source code was highlighted with Source Code Highlighter .



, , . , , .

PS - , .
PPS - , .
%pwd
//app/code
%ll -a
total 10
drwxrwxr-x 5 vlad www 512 18 09:37 .
drwxrwxr-x 6 vlad www 512 29 19:30 ..
drwxrwxr-x 3 vlad www 512 29 19:30 community
drwxrwxr-x 4 vlad www 512 18 09:37 core
drwxrwxr-x 3 vlad www 512 27 02:37 local
%ll -a local
total 6
drwxrwxr-x 3 vlad www 512 27 02:37 .
drwxrwxr-x 5 vlad www 512 18 09:37 ..
drwxrwxr-x 6 vlad www 512 29 23:48 Cifrum


, , . , , Core , CoreC .

:

%ll -1aR //app/code/local/
.
..
Cifrum

//app/code/local/Cifrum:
.
..
CoreC

//app/code/local/Cifrum/CoreC:
.
..
Block
Helper
Model
controllers
etc
sql


- . , , php|architect's Guide to Programming with Magento.

- etc, config.xml, Model, .

Model/Store.php :

<?php

/*****

Trying to rewrite Core_Model_Store

*/

// , ,
// app/code/core/Mage/Core/Model/Store.php

class Cifrum_CoreC_Model_Store extends Mage_Core_Model_Store
{


/**
*
* formatPrice without decimals, for rubles only for right now
*
*/

// ,

public function formatPrice($price, $includeContainer = true )
{
if ($ this ->getCurrentCurrency()) {
$priceReturn = $ this ->getCurrentCurrency()->format($price, array(), $includeContainer);

//Not the cleanest method but the fastest for now…
if (preg_match( '//i' , $priceReturn)) {
return $ this ->getCurrentCurrency()->format($price, array( 'precision' => 0), $includeContainer);
} else {
return $priceReturn;
}
}

return $price;
}

}

* This source code was highlighted with Source Code Highlighter .

?>


, Mage_Core_Model_Store::formatPrice() "". , ( , - ""), .

, . etc/config.xml :

<? xml version ="1.0" ? >
< config >
< modules >


< Cifrum_CoreC >
< version > 0.0.1 </ version >
< depends >

</ depends >
</ Cifrum_CoreC >


</ modules >
< global >
< models >



< core >
< rewrite >
< store > Cifrum_CoreC_Model_Store </ store >
</ rewrite >
</ core >


</ models >
< resources ></ resources >
< blocks ></ blocks >
< corec >

</ corec >
</ global >
< adminhtml >
< menu ></ menu >
< acl ></ acl >
< events ></ events >
< translate ></ translate >
</ adminhtml >
< frontend >
< routers ></ routers >
< events ></ events >
< translate ></ translate >
< layout ></ layout >
</ frontend >
< default >
< config_vars >

</ config_vars >
</ default >
</ config >


* This source code was highlighted with Source Code Highlighter .



-, , . . , store core .

. , , .
, app/code/etc . app/etc/modules/Cifrum_All.xml, Cifrum.

<? xml version ="1.0" ? >
< config >
< modules >
< Cifrum_CoreC >
< active > true </ active >
< codePool > local </ codePool >
</ Cifrum_CoreC >
</ modules >
</ config >

* This source code was highlighted with Source Code Highlighter .



, , . , , .

PS - , .
PPS - , .
%pwd
//app/code
%ll -a
total 10
drwxrwxr-x 5 vlad www 512 18 09:37 .
drwxrwxr-x 6 vlad www 512 29 19:30 ..
drwxrwxr-x 3 vlad www 512 29 19:30 community
drwxrwxr-x 4 vlad www 512 18 09:37 core
drwxrwxr-x 3 vlad www 512 27 02:37 local
%ll -a local
total 6
drwxrwxr-x 3 vlad www 512 27 02:37 .
drwxrwxr-x 5 vlad www 512 18 09:37 ..
drwxrwxr-x 6 vlad www 512 29 23:48 Cifrum


, , . , , Core , CoreC .

:

%ll -1aR //app/code/local/
.
..
Cifrum

//app/code/local/Cifrum:
.
..
CoreC

//app/code/local/Cifrum/CoreC:
.
..
Block
Helper
Model
controllers
etc
sql


- . , , php|architect's Guide to Programming with Magento.

- etc, config.xml, Model, .

Model/Store.php :

<?php

/*****

Trying to rewrite Core_Model_Store

*/

// , ,
// app/code/core/Mage/Core/Model/Store.php

class Cifrum_CoreC_Model_Store extends Mage_Core_Model_Store
{


/**
*
* formatPrice without decimals, for rubles only for right now
*
*/

// ,

public function formatPrice($price, $includeContainer = true )
{
if ($ this ->getCurrentCurrency()) {
$priceReturn = $ this ->getCurrentCurrency()->format($price, array(), $includeContainer);

//Not the cleanest method but the fastest for now…
if (preg_match( '//i' , $priceReturn)) {
return $ this ->getCurrentCurrency()->format($price, array( 'precision' => 0), $includeContainer);
} else {
return $priceReturn;
}
}

return $price;
}

}

* This source code was highlighted with Source Code Highlighter .


?>


, Mage_Core_Model_Store::formatPrice() "". , ( , - ""), .

, . etc/config.xml :

<? xml version ="1.0" ? >
< config >
< modules >


< Cifrum_CoreC >
< version > 0.0.1 </ version >
< depends >

</ depends >
</ Cifrum_CoreC >


</ modules >
< global >
< models >



< core >
< rewrite >
< store > Cifrum_CoreC_Model_Store </ store >
</ rewrite >
</ core >


</ models >
< resources ></ resources >
< blocks ></ blocks >
< corec >

</ corec >
</ global >
< adminhtml >
< menu ></ menu >
< acl ></ acl >
< events ></ events >
< translate ></ translate >
</ adminhtml >
< frontend >
< routers ></ routers >
< events ></ events >
< translate ></ translate >
< layout ></ layout >
</ frontend >
< default >
< config_vars >

</ config_vars >
</ default >
</ config >


* This source code was highlighted with Source Code Highlighter .



-, , . . , store core .

. , , .
, app/code/etc . app/etc/modules/Cifrum_All.xml, Cifrum.

<? xml version ="1.0" ? >
< config >
< modules >
< Cifrum_CoreC >
< active > true </ active >
< codePool > local </ codePool >
</ Cifrum_CoreC >
</ modules >
</ config >

* This source code was highlighted with Source Code Highlighter .



, , . , , .

PS - , .
PPS - , .
%pwd
//app/code
%ll -a
total 10
drwxrwxr-x 5 vlad www 512 18 09:37 .
drwxrwxr-x 6 vlad www 512 29 19:30 ..
drwxrwxr-x 3 vlad www 512 29 19:30 community
drwxrwxr-x 4 vlad www 512 18 09:37 core
drwxrwxr-x 3 vlad www 512 27 02:37 local
%ll -a local
total 6
drwxrwxr-x 3 vlad www 512 27 02:37 .
drwxrwxr-x 5 vlad www 512 18 09:37 ..
drwxrwxr-x 6 vlad www 512 29 23:48 Cifrum


, , . , , Core , CoreC .

:

%ll -1aR //app/code/local/
.
..
Cifrum

//app/code/local/Cifrum:
.
..
CoreC

//app/code/local/Cifrum/CoreC:
.
..
Block
Helper
Model
controllers
etc
sql


- . , , php|architect's Guide to Programming with Magento.

- etc, config.xml, Model, .

Model/Store.php :

<?php

/*****

Trying to rewrite Core_Model_Store

*/

// , ,
// app/code/core/Mage/Core/Model/Store.php

class Cifrum_CoreC_Model_Store extends Mage_Core_Model_Store
{


/**
*
* formatPrice without decimals, for rubles only for right now
*
*/

// ,

public function formatPrice($price, $includeContainer = true )
{
if ($ this ->getCurrentCurrency()) {
$priceReturn = $ this ->getCurrentCurrency()->format($price, array(), $includeContainer);

//Not the cleanest method but the fastest for now…
if (preg_match( '//i' , $priceReturn)) {
return $ this ->getCurrentCurrency()->format($price, array( 'precision' => 0), $includeContainer);
} else {
return $priceReturn;
}
}

return $price;
}

}

* This source code was highlighted with Source Code Highlighter .


?>


, Mage_Core_Model_Store::formatPrice() "". , ( , - ""), .

, . etc/config.xml :

<? xml version ="1.0" ? >
< config >
< modules >


< Cifrum_CoreC >
< version > 0.0.1 </ version >
< depends >

</ depends >
</ Cifrum_CoreC >


</ modules >
< global >
< models >



< core >
< rewrite >
< store > Cifrum_CoreC_Model_Store </ store >
</ rewrite >
</ core >


</ models >
< resources ></ resources >
< blocks ></ blocks >
< corec >

</ corec >
</ global >
< adminhtml >
< menu ></ menu >
< acl ></ acl >
< events ></ events >
< translate ></ translate >
</ adminhtml >
< frontend >
< routers ></ routers >
< events ></ events >
< translate ></ translate >
< layout ></ layout >
</ frontend >
< default >
< config_vars >

</ config_vars >
</ default >
</ config >


* This source code was highlighted with Source Code Highlighter .


-, , . . , store core .

. , , .
, app/code/etc . app/etc/modules/Cifrum_All.xml, Cifrum.

<? xml version ="1.0" ? >
< config >
< modules >
< Cifrum_CoreC >
< active > true </ active >
< codePool > local </ codePool >
</ Cifrum_CoreC >
</ modules >
</ config >

* This source code was highlighted with Source Code Highlighter .



, , . , , .

PS - , .
PPS - , .
%pwd
//app/code
%ll -a
total 10
drwxrwxr-x 5 vlad www 512 18 09:37 .
drwxrwxr-x 6 vlad www 512 29 19:30 ..
drwxrwxr-x 3 vlad www 512 29 19:30 community
drwxrwxr-x 4 vlad www 512 18 09:37 core
drwxrwxr-x 3 vlad www 512 27 02:37 local
%ll -a local
total 6
drwxrwxr-x 3 vlad www 512 27 02:37 .
drwxrwxr-x 5 vlad www 512 18 09:37 ..
drwxrwxr-x 6 vlad www 512 29 23:48 Cifrum


, , . , , Core , CoreC .

:

%ll -1aR //app/code/local/
.
..
Cifrum

//app/code/local/Cifrum:
.
..
CoreC

//app/code/local/Cifrum/CoreC:
.
..
Block
Helper
Model
controllers
etc
sql


- . , , php|architect's Guide to Programming with Magento.

- etc, config.xml, Model, .

Model/Store.php :

<?php

/*****

Trying to rewrite Core_Model_Store

*/

// , ,
// app/code/core/Mage/Core/Model/Store.php

class Cifrum_CoreC_Model_Store extends Mage_Core_Model_Store
{


/**
*
* formatPrice without decimals, for rubles only for right now
*
*/

// ,

public function formatPrice($price, $includeContainer = true )
{
if ($ this ->getCurrentCurrency()) {
$priceReturn = $ this ->getCurrentCurrency()->format($price, array(), $includeContainer);

//Not the cleanest method but the fastest for now…
if (preg_match( '//i' , $priceReturn)) {
return $ this ->getCurrentCurrency()->format($price, array( 'precision' => 0), $includeContainer);
} else {
return $priceReturn;
}
}

return $price;
}

}

* This source code was highlighted with Source Code Highlighter .


?>


, Mage_Core_Model_Store::formatPrice() "". , ( , - ""), .

, . etc/config.xml :

<? xml version ="1.0" ? >
< config >
< modules >


< Cifrum_CoreC >
< version > 0.0.1 </ version >
< depends >

</ depends >
</ Cifrum_CoreC >


</ modules >
< global >
< models >



< core >
< rewrite >
< store > Cifrum_CoreC_Model_Store </ store >
</ rewrite >
</ core >


</ models >
< resources ></ resources >
< blocks ></ blocks >
< corec >

</ corec >
</ global >
< adminhtml >
< menu ></ menu >
< acl ></ acl >
< events ></ events >
< translate ></ translate >
</ adminhtml >
< frontend >
< routers ></ routers >
< events ></ events >
< translate ></ translate >
< layout ></ layout >
</ frontend >
< default >
< config_vars >

</ config_vars >
</ default >
</ config >


* This source code was highlighted with Source Code Highlighter .



-, , . . , store core .

. , , .
, app/code/etc . app/etc/modules/Cifrum_All.xml, Cifrum.

<? xml version ="1.0" ? >
< config >
< modules >
< Cifrum_CoreC >
< active > true </ active >
< codePool > local </ codePool >
</ Cifrum_CoreC >
</ modules >
</ config >

* This source code was highlighted with Source Code Highlighter .



, , . , , .

PS - , .
PPS - , .
%pwd
//app/code
%ll -a
total 10
drwxrwxr-x 5 vlad www 512 18 09:37 .
drwxrwxr-x 6 vlad www 512 29 19:30 ..
drwxrwxr-x 3 vlad www 512 29 19:30 community
drwxrwxr-x 4 vlad www 512 18 09:37 core
drwxrwxr-x 3 vlad www 512 27 02:37 local
%ll -a local
total 6
drwxrwxr-x 3 vlad www 512 27 02:37 .
drwxrwxr-x 5 vlad www 512 18 09:37 ..
drwxrwxr-x 6 vlad www 512 29 23:48 Cifrum


, , . , , Core , CoreC .

:

%ll -1aR //app/code/local/
.
..
Cifrum

//app/code/local/Cifrum:
.
..
CoreC

//app/code/local/Cifrum/CoreC:
.
..
Block
Helper
Model
controllers
etc
sql


- . , , php|architect's Guide to Programming with Magento.

- etc, config.xml, Model, .

Model/Store.php :

<?php

/*****

Trying to rewrite Core_Model_Store

*/

// , ,
// app/code/core/Mage/Core/Model/Store.php

class Cifrum_CoreC_Model_Store extends Mage_Core_Model_Store
{


/**
*
* formatPrice without decimals, for rubles only for right now
*
*/

// ,

public function formatPrice($price, $includeContainer = true )
{
if ($ this ->getCurrentCurrency()) {
$priceReturn = $ this ->getCurrentCurrency()->format($price, array(), $includeContainer);

//Not the cleanest method but the fastest for now…
if (preg_match( '//i' , $priceReturn)) {
return $ this ->getCurrentCurrency()->format($price, array( 'precision' => 0), $includeContainer);
} else {
return $priceReturn;
}
}

return $price;
}

}

* This source code was highlighted with Source Code Highlighter .


?>


, Mage_Core_Model_Store::formatPrice() "". , ( , - ""), .

, . etc/config.xml :

<? xml version ="1.0" ? >
< config >
< modules >


< Cifrum_CoreC >
< version > 0.0.1 </ version >
< depends >

</ depends >
</ Cifrum_CoreC >


</ modules >
< global >
< models >



< core >
< rewrite >
< store > Cifrum_CoreC_Model_Store </ store >
</ rewrite >
</ core >


</ models >
< resources ></ resources >
< blocks ></ blocks >
< corec >

</ corec >
</ global >
< adminhtml >
< menu ></ menu >
< acl ></ acl >
< events ></ events >
< translate ></ translate >
</ adminhtml >
< frontend >
< routers ></ routers >
< events ></ events >
< translate ></ translate >
< layout ></ layout >
</ frontend >
< default >
< config_vars >

</ config_vars >
</ default >
</ config >


* This source code was highlighted with Source Code Highlighter .



-, , . . , store core .

. , , .
, app/code/etc . app/etc/modules/Cifrum_All.xml, Cifrum.

<? xml version ="1.0" ? >
< config >
< modules >
< Cifrum_CoreC >
< active > true </ active >
< codePool > local </ codePool >
</ Cifrum_CoreC >
</ modules >
</ config >

* This source code was highlighted with Source Code Highlighter .


, , . , , .

PS - , .
PPS - , .
%pwd
//app/code
%ll -a
total 10
drwxrwxr-x 5 vlad www 512 18 09:37 .
drwxrwxr-x 6 vlad www 512 29 19:30 ..
drwxrwxr-x 3 vlad www 512 29 19:30 community
drwxrwxr-x 4 vlad www 512 18 09:37 core
drwxrwxr-x 3 vlad www 512 27 02:37 local
%ll -a local
total 6
drwxrwxr-x 3 vlad www 512 27 02:37 .
drwxrwxr-x 5 vlad www 512 18 09:37 ..
drwxrwxr-x 6 vlad www 512 29 23:48 Cifrum


, , . , , Core , CoreC .

:

%ll -1aR //app/code/local/
.
..
Cifrum

//app/code/local/Cifrum:
.
..
CoreC

//app/code/local/Cifrum/CoreC:
.
..
Block
Helper
Model
controllers
etc
sql


- . , , php|architect's Guide to Programming with Magento.

- etc, config.xml, Model, .

Model/Store.php :

<?php

/*****

Trying to rewrite Core_Model_Store

*/

// , ,
// app/code/core/Mage/Core/Model/Store.php

class Cifrum_CoreC_Model_Store extends Mage_Core_Model_Store
{


/**
*
* formatPrice without decimals, for rubles only for right now
*
*/

// ,

public function formatPrice($price, $includeContainer = true )
{
if ($ this ->getCurrentCurrency()) {
$priceReturn = $ this ->getCurrentCurrency()->format($price, array(), $includeContainer);

//Not the cleanest method but the fastest for now…
if (preg_match( '//i' , $priceReturn)) {
return $ this ->getCurrentCurrency()->format($price, array( 'precision' => 0), $includeContainer);
} else {
return $priceReturn;
}
}

return $price;
}

}

* This source code was highlighted with Source Code Highlighter .


?>


, Mage_Core_Model_Store::formatPrice() "". , ( , - ""), .

, . etc/config.xml :

<? xml version ="1.0" ? >
< config >
< modules >


< Cifrum_CoreC >
< version > 0.0.1 </ version >
< depends >

</ depends >
</ Cifrum_CoreC >


</ modules >
< global >
< models >



< core >
< rewrite >
< store > Cifrum_CoreC_Model_Store </ store >
</ rewrite >
</ core >


</ models >
< resources ></ resources >
< blocks ></ blocks >
< corec >

</ corec >
</ global >
< adminhtml >
< menu ></ menu >
< acl ></ acl >
< events ></ events >
< translate ></ translate >
</ adminhtml >
< frontend >
< routers ></ routers >
< events ></ events >
< translate ></ translate >
< layout ></ layout >
</ frontend >
< default >
< config_vars >

</ config_vars >
</ default >
</ config >


* This source code was highlighted with Source Code Highlighter .



-, , . . , store core .

. , , .
, app/code/etc . app/etc/modules/Cifrum_All.xml, Cifrum.

<? xml version ="1.0" ? >
< config >
< modules >
< Cifrum_CoreC >
< active > true </ active >
< codePool > local </ codePool >
</ Cifrum_CoreC >
</ modules >
</ config >

* This source code was highlighted with Source Code Highlighter .



, , . , , .

PS - , .
PPS - , .

Source: https://habr.com/ru/post/144026/


All Articles