0

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
    if(strlen($title) < 30){
        $this->title = $title . ' - ' . $this->config->get("store_name");
    } else {
        $this->title = $title;
    }
}

Although the $this is referring to the document class. How to I get the config?

Using the latest version of opencart 1.5.2.1

When you check the index.php file to see how config is loaded

// Registry
$registry = new Registry();

// Loader
$loader = new Loader($registry);
$registry->set('load', $loader);

// Config
$config = new Config();
$registry->set('config', $config);
2
  • Check in the file ... somewhere you can find this : $this->config = New ... you'll see the class used. Apr 16, 2012 at 15:30
  • I have added this part to show what I am trying to achieve. $this->config isn't in the class. Apr 16, 2012 at 17:25

3 Answers 3

4
+50

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));
1
  • Perfect I was waiting for a answer which provided a way to do this by only editing a couple of files. Apr 19, 2012 at 12:05
1

You cannot use $this->cofig inside document class, because it has not config property, also it has not magic __get method, like controller class.

You can try to change your header controller.

public function index() {

   $title = $this->document->getTitle();
   if(strlen($title) < 30){
      $this->data['title'] = $title . ' - ' . $this->config->get("store_name");
   } else {
      $this->data['title'] = $title;
   }

   // ....
}

-------- UPDATED --------

If you want use $config inside Document class, you may use global variable:

public function setTitle($title) {

    global $config;
    // Append store name if small title
    if(strlen($title) < 30){
        $this->title = $title . ' - ' . $config->get("store_name");
    } else {
        $this->title = $title;
    }
}

But I recommend you don't do this.

1

On Opencart 1.5.1.3 that worked changing $this->config->get("store_name") to $this->config->get("config_name")

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.