Post by radutc » Tue Sep 24, 2019 10:48 pm

Salut,

Vreau sa inserez un text inainte de tag-ul </head>, utilizand Opencart events.
Am creat un event in admin.
Trigger
catalog/view/common/header/before
Action
extension/module/tagmanager/insert_head_code_before

Cum pot afisa un text inainte de tagul </head>?
Mai jos e controller-ul din Catalog

Code: Select all

class ControllerExtensionModuleTagmanager extends Controller  {
	public function insert_head_code_before(&$route, &$data)  {
        	$head_code = 'bla bla bla';
		//var_dump($data);
		
		// Insert  before the data
        	$data = str_replace('</head>', $head_code . '</head>', $data);
	}
}
Nu functioneaza corect. Variabila $data nu contine niciun tag </head>, deci nu prea am ce inlocui.

Multumesc.

baby Opencart developer :bang:


Newbie

Posts

Joined
Fri Apr 05, 2019 3:00 pm

Post by radutc » Sat Sep 28, 2019 2:27 pm

Rezolvat!
Intre timp, mi-am dat seama de ce nu functioneaza.
Am utilizat un tip de event gresit.
Daca utilizezi un eveniment de tip "before" nu ai acces la template, adica la output-ul final, ci doar la datele din array-ul controller-ului.
Asadar solutia a fost sa schimb tipul eventului in event "after".

Solutia:
Eveniment:
Trigger
catalog/view/common/header/after
Action
extension/module/mymodule/insert_head_code_after

Metoda din controller

Code: Select all

public function insert_head_code_after(&$route,  &$args,  &$output) {
	// ...
	$head_code = 'un text oarecare';
	
	// Insert codes before the </head> tag
	$output = str_replace('</head>',  $head_code . '</head>' ,  $output);
}

baby Opencart developer :bang:


Newbie

Posts

Joined
Fri Apr 05, 2019 3:00 pm
Who is online

Users browsing this forum: No registered users and 3 guests