How I changed Magento, or Changing the basic functionality with a simple example

    When developing the store’s interface, I was faced with the task of not only bringing everything to the right look and logic, but also of updating the engine versions, so I immediately excluded editing the main software modules. Magento was chosen as the platform, which, as it turned out, provides excellent tools for developing its own extensions, including the ability to replace the standard functionality with its own. This is what we will do.

    Let's look at a specific example. In most stores, now nobody uses a penny, but by default - thanks to the locale - they are displayed. There was a task to remove them. As it turned out, you can do this by simply editing the component of the Zend library, but, firstly, it contradicts the first sentence of this post, and secondly, we are not looking for easy ways. :)



    So, we need to do the following:
    - redefine the class that is responsible for displaying the formatted price;
    - create a module that will contain this class;
    - configure the module so that the redefined class is called in cases in which the original class was previously activated;
    - activate a new module in the system.

    All paths given are relative to the directory in which Magento is installed.

    As I said, Magento presents good opportunities for developing your own modules and expanding the basic functionality. The main part of the working code of the system (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 that describes the main class-hub of the system - Mage
    code - all
    design code - as the name implies, design descriptions are located here: it is the logic and templates for block output; descriptions of css styles directly, scripts and pictures taken out in a separate place
    etc - locale configuration files
    - 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 of the main modules of the system, as well as community and local , which are initially empty and designed to install third-party modules (community) or for ones 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, for starters, we need to create a folder that will contain our modules. The name of this folder will be the name of the package of the modules that we will develop. Because I developed modules for my project, the folder I have is the name of the project - Cifrum . Next, you need to create a directory in which the module will be located, one of the classes of which we will implement the necessary functionality. The standard module responsible for formatting prices is called Core, so I named my CoreC. Create the following directory structure: The list of folders at the end is the standard structure of the module. I will not dwell on the description here in detail, if necessary, you can read the book

    # 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






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

    In this case, we will be interested in two directories - this is etc , in which we will place the config.xml file , and Model , where the description of our class will be located.

    The Core module is engaged in the output of the formatted price, and the Store class, the formatPrice () method, are engaged in the concrete. We need to create a new class - the inheritor of Mage_Core_Model_Store and override its method.

    Let's create a Model / Store.php file with the following contents:


    /*****

     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.



    As you can see, we take the code of the standard function Mage_Core_Model_Store :: formatPrice () and append the check for the occurrence of the substring “rub” in the string. I’m not sure what will work on all locales (maybe just “p” appears somewhere), but it works for me.

    Now we need to specify what exactly needs to be done with the class we created. To do this, create etc / config. xml and fill it with the following: Again, the file structure is standard, we don’t need everything. I highlighted the important points with comments. First we describe the name and version of our module, and then redefine it using the tag



     

      
      
       0.0.1
       
        
       

      

      

     

     
      

       
        -->
       
      
       Cifrum_CoreC_Model_Store
      

       

       

      

      
       
      
       
      

     

     
      
      
      
      
     

     
      
      
      
      
     

     
      
       
      

     



    * This source code was highlighted with Source Code Highlighter.



    system call of the store class of the core module .

    However, this is not all. We need to tell the system that we have a new module, activate it.
    As you may recall, the system configs are in the app / below code / etc . We create and open the file app / etc / modules / Cifrum_All.xml , which in my case contains a description of all the modules of the Cifrum package. That, in fact, is all. Having updated the store page, we will see that tenths of rubles have disappeared. If you have questions or clarifications - write, I will communicate with pleasure. PS I apologize for the wild mixture of Russian and English comments - the Russians added during the writing of the article, wrote English for themselves.



     
      
       true
       local
      

     



    * This source code was highlighted with Source Code Highlighter.









    Also popular now: