- Posts: 117
- Thank you received: 6
Javascript isn't loaded by onHikashopAfterDisplayView at the address view
8 years 5 months ago - 8 years 5 months ago #293200
by alin
Sorry for my English, it's not my native language.
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:
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.
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.
Sorry for my English, it's not my native language.
Last edit: 8 years 5 months ago by alin.
Please Log in or Create an account to join the conversation.
8 years 5 months ago #293222
by Jerome
Jerome - Obsidev.com
HikaMarket & HikaSerial developer / HikaShop core dev team.
Replied by Jerome on topic Javascript isn't loaded by onHikashopAfterDisplayView at the address view
Hello,
You need to check if the view you're targeting do have the " displayView " variable enable.
Please note that if you want to add some javascript in the header ; you can also use regular Joomla triggers (like the one which is called before the head processing)
Regards,
You need to check if the view you're targeting do have the " displayView " variable enable.
Please note that if you want to add some javascript in the header ; you can also use regular Joomla triggers (like the one which is called before the head processing)
Regards,
Jerome - Obsidev.com
HikaMarket & HikaSerial developer / HikaShop core dev team.
Please Log in or Create an account to join the conversation.
8 years 5 months ago - 8 years 5 months ago #293229
by alin
what does this means? Where can I check this?
This code at the view "checkout" (first screenshot) works:
But at the view "address" (second screenshot) it doesn't work:
Sorry for my English, it's not my native language.
Replied by alin on topic Javascript isn't loaded by onHikashopAfterDisplayView at the address view
Hello,Jerome wrote: You need to check if the view you're targeting do have the " displayView " variable enable.
what does this means? Where can I check this?
This code at the view "checkout" (first screenshot) works:
Code:
if($view->getName() === 'checkout') {
some code works
}
Code:
if($view->getName() === 'address') {
some code doesn't work
}
I need to insert some js only at the pages where HikaShop address fields are present.Jerome wrote: Please note that if you want to add some javascript in the header ; you can also use regular Joomla triggers (like the one which is called before the head processing)
Sorry for my English, it's not my native language.
Last edit: 8 years 5 months ago by alin.
Please Log in or Create an account to join the conversation.
8 years 5 months ago #293253
by nicolas
Replied by nicolas on topic Javascript isn't loaded by onHikashopAfterDisplayView at the address view
Hi,
1. If you look in components/com_hikashop/views/checkout/view.html.php you have the line:
public $triggerView = array('hikashop','hikashoppayment','hikashopshipping');
This line allow plugins of that group to use the onHikashopAfterDisplayView and onHikashopBeforeDisplayView triggers.
If you look in components/com_hikashop/views/address/view.html.php, there is no $triggerView which means that these triggers are not available for the views from "address".
So you would have to add the line:
public $triggerView = true;
in that file and then these triggers would be called there.
2. As Jerome said, since you're only adding javascript to the header, it's better to use this trigger of Joomla in a plugin of the group "system":
docs.joomla.org/Plugin/Events/System#onBeforeCompileHead
You just need to change your check on $view->getName to use JInput:
docs.joomla.org/Retrieving_request_data_using_JInput
And check on $jinput->get("option") == "com_hikashop" and $jinput->get("ctrl") == "checkout"
1. If you look in components/com_hikashop/views/checkout/view.html.php you have the line:
public $triggerView = array('hikashop','hikashoppayment','hikashopshipping');
This line allow plugins of that group to use the onHikashopAfterDisplayView and onHikashopBeforeDisplayView triggers.
If you look in components/com_hikashop/views/address/view.html.php, there is no $triggerView which means that these triggers are not available for the views from "address".
So you would have to add the line:
public $triggerView = true;
in that file and then these triggers would be called there.
2. As Jerome said, since you're only adding javascript to the header, it's better to use this trigger of Joomla in a plugin of the group "system":
docs.joomla.org/Plugin/Events/System#onBeforeCompileHead
You just need to change your check on $view->getName to use JInput:
docs.joomla.org/Retrieving_request_data_using_JInput
And check on $jinput->get("option") == "com_hikashop" and $jinput->get("ctrl") == "checkout"
The following user(s) said Thank You: alin
Please Log in or Create an account to join the conversation.
8 years 5 months ago - 8 years 5 months ago #293316
by alin
Sorry for my English, it's not my native language.
Replied by alin on topic Javascript isn't loaded by onHikashopAfterDisplayView at the address view
Thank you very much for detailed explanation - now it's working like needed. I faced jQuery compatibility issues with Chosen library that HikaShop loads but I think it's other story... At now plugin does its job - puts js code to head.
Sorry for my English, it's not my native language.
Last edit: 8 years 5 months ago by alin.
Please Log in or Create an account to join the conversation.
Time to create page: 0.097 seconds