I'm creating a plugin, I use onHikashopAfterDisplayView trigger to add javascript to needed page. At the checkout view all is working fine but at the address view it doesn't work. I'm a beginner Help me, please, to find the reason. Here is my code:
<?php
defined('_JEXEC') or die('Restricted access');
?><?php
class plgHikashopDadata_address_suggestions extends JPlugin {
// protected $autoloadLanguage = true;
function plgHikashopDadata_address_suggestions(&$subject, $config){
parent::__construct($subject, $config);
}
function onHikashopAfterDisplayView(&$view) {
$plugin = JPluginHelper::getPlugin('hikashop', 'dadata_address_suggestions');
$plg_params = new JRegistry();
$plg_params->loadString($plugin->params);
$document = JFactory::getDocument();
if($view->getName() === 'checkout') {
$document->addStyleSheet('https://cdn.jsdelivr.net/npm/suggestions-jquery@17.12.0/dist/css/suggestions.min.css','text/css');
$document->addScript('https://cdnjs.cloudflare.com/ajax/libs/jquery/1.10.2/jquery.min.js','text/javascript');
?><!--[if lt IE 10]><?php
$document->addScript('https://cdnjs.cloudflare.com/ajax/libs/jquery-ajaxtransport-xdomainrequest/1.0.1/jquery.xdomainrequest.min.js','text/javascript');
?><![endif]--><?php
$document->addScript('https://cdn.jsdelivr.net/npm/suggestions-jquery@17.12.0/dist/js/jquery.suggestions.min.js','text/javascript');
$document->addScriptDeclaration('
function suggest() {
function showPostalCode(suggestion) {
$("#' . $plg_params->get('post_code_field_id') . '").val(suggestion.data.postal_code);
}
function clearPostalCode(suggestion) {
$("#' . $plg_params->get('post_code_field_id') . '").val("");
}
$("#' . $plg_params->get('full_address_field_id') . '").suggestions({
token: "' . $plg_params->get('token') . '",
type: "ADDRESS",
onSelect: showPostalCode,
onSelectNothing: clearPostalCode
});
}
Oby.registerAjax("checkoutBlockRefresh", suggest);
');
}
if($view->getName() === 'address') {
$document->addStyleSheet('https://cdn.jsdelivr.net/npm/suggestions-jquery@17.12.0/dist/css/suggestions.min.css','text/css');
$document->addScript('https://cdnjs.cloudflare.com/ajax/libs/jquery/1.10.2/jquery.min.js','text/javascript');
?><!--[if lt IE 10]><?php
$document->addScript('https://cdnjs.cloudflare.com/ajax/libs/jquery-ajaxtransport-xdomainrequest/1.0.1/jquery.xdomainrequest.min.js','text/javascript');
?><![endif]--><?php
$document->addScript('https://cdn.jsdelivr.net/npm/suggestions-jquery@17.12.0/dist/js/jquery.suggestions.min.js','text/javascript');
$document->addScriptDeclaration('
$(document).ready(function () {
function showPostalCode(suggestion) {
$("#' . $plg_params->get('post_code_field_id') . '").val(suggestion.data.postal_code);
}
function clearPostalCode(suggestion) {
$("#' . $plg_params->get('post_code_field_id') . '").val("");
}
$("#' . $plg_params->get('full_address_field_id') . '").suggestions({
token: "' . $plg_params->get('token') . '",
type: "ADDRESS",
onSelect: showPostalCode,
onSelectNothing: clearPostalCode
});
});
');
}
}
}
P.S. I've attached the plugin. Its main purpose is to add javascript code to the checkout and address views for integration with a third-party service.