Opencart meta title include store name

前端 未结 3 1359
半阙折子戏
半阙折子戏 2021-01-28 16:38

How to get the store name when in the Document class. This is what I am trying to do:

public function setTitle($title) {

    // Append store name if small title         


        
3条回答
  •  执笔经年
    2021-01-28 17:06

    Opencart uses some kind of dependency injection to access the registry from library classes. This technique is applied in many library classes, like the customer, affiliate, currency, tax, weight, length and cart class. Surprisingly, the document class is one of the few classes that don't get the registry object passed in.

    If you'd like to follow this convention, I'd suggest you modify index.php and library/document.php so that the Document constructor takes the registry as an argument:

    class Document {
    
            [...]
    
            // Add the constructor below
            public function __construct($registry) {
                    $this->config = $registry->get('config');
            }
    
            [...]
    
            public setTitle($title) {
                if(strlen($title) < 30){
                    $this->title = $title . ' - ' . $this->config->get("store_name");
                } else {
                    $this->title = $title;
                }
            }
    
    }
    

    Now you only need to inject the registry object into the Document class in index.php, as follows:

    // Registry
    $registry = new Registry();
    
    [...]
    
    // Document
    $registry->set('document', new Document($registry));
    

提交回复
热议问题