Javascript isn't loaded by onHikashopAfterDisplayView at the address view

  • Posts: 117
  • Thank you received: 6
3 years 11 months ago #293200

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.


Sorry for my English, it's not my native language.
Attachments:
Last edit: 3 years 11 months ago by alin.

Please Log in or Create an account to join the conversation.

  • Posts: 25249
  • Thank you received: 3908
  • MODERATOR
3 years 11 months ago #293222

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,


Jerome - Obsidev.com
HikaMarket & HikaSerial developer / HikaShop core dev team.

Also helping the HikaShop support team when having some time or couldn't sleep.
By the way, do not send me private message, use the "contact us" form instead.

Please Log in or Create an account to join the conversation.

  • Posts: 117
  • Thank you received: 6
3 years 11 months ago #293229

Jerome wrote: You need to check if the view you're targeting do have the " displayView " variable enable.

Hello,
what does this means? Where can I check this?

This code at the view "checkout" (first screenshot) works:
if($view->getName() === 'checkout') {
some code works
}
But at the view "address" (second screenshot) it doesn't work:
if($view->getName() === 'address') {
some code doesn't work
}

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)

I need to insert some js only at the pages where HikaShop address fields are present.


Sorry for my English, it's not my native language.
Attachments:
Last edit: 3 years 11 months ago by alin.

Please Log in or Create an account to join the conversation.

  • Posts: 75473
  • Thank you received: 11742
  • MODERATOR
3 years 11 months ago #293253

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"

The following user(s) said Thank You: alin

Please Log in or Create an account to join the conversation.

  • Posts: 117
  • Thank you received: 6
3 years 11 months ago #293316

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: 3 years 11 months ago by alin.

Please Log in or Create an account to join the conversation.

Time to create page: 0.065 seconds
Powered by Kunena Forum