Problem with custom plugin in product show view

  • Posts: 128
  • Thank you received: 3
8 years 8 months ago #233683

-- HikaShop version -- : 2.5.0
-- Joomla version -- : 3.4.1
-- PHP version -- : 5.3.24

I have created a custom component to enter information for our products. I'm kinda new to working with creating plugins. I've created a plugin to work with this information in the product show_default view. I have attached the component and plugin.

File Attachment:

File Name: com_ballistics.zip
File Size:19 KB


File Attachment:

File Name: plg_produc..._1_0.zip
File Size:2 KB


The code I have at the beginning of my show_default view to work with the plugin is:
// Get plugin 'productballistics' of plugin type 'ballistics'
$plugin = JPluginHelper::getPlugin('ballistics', 'productballistics');
// Check if plugin is enabled
if($plugin){
	//fetch variables to run through productballistics plugin
	if($this->row->unit_type == "pallets"){
		if($this->row->package_count == "50000"){
			$ballistic_sku = rtrim($this->element->product_code,"50KP");
		}elseif($this->row->package_count == "100000"){
			$ballistic_sku = rtrim($this->element->product_code,"100KP");
		}
	}elseif($this->row->unit_type == "cases"){
		$ballistic_sku = rtrim($this->element->product_code,"L");
	}else{
		$ballistic_sku = $this->element->product_code;
	}
	$dispatcher = JDispatcher::getInstance();
	$ballistics = $dispatcher->trigger('onProductballistics', array($ballistic_sku, $this->element->grains, $this->element->gun_type, $this->element->bullet_type));
}

$this->element->grains, $this->element->gun_type, and $this->element->bullet_type are all custom fields I've created for the products and they are not empty. The code I have further down in the view to display the results of working with the plugin is:
foreach($ballistics as $ballistic){
			echo $ballistic;
		}

I'm not getting anything to display and I know the data is there for the products I've tried this with. I'm sure it's something simple that is going on with this, but I'm just not sure what it is. Thanks for any help.

Attachments:
Last edit: 8 years 8 months ago by deuce4884.

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

  • Posts: 82867
  • Thank you received: 13373
  • MODERATOR
8 years 8 months ago #233711

Hi,

I've had a quick look at the code but I didn't find it to be wrong.
But then, I would recommend a simpler version of your solution.
Instead of customizing the show_default view to add a trigger to call a custom plugin, it's better to either directly put your custom code in the show_default view, or to use a trigger we already have in HikaShop (onHikashopBeforeDisplayView) in your custom plugin and fill in your HTML in the extraData array that is displayed automatically by the view and so you avoid having a view override.
In either case, you reduce the number of locations where you have your code, so you reduce the possibilities of making a mistake, and it's also easier to maintain in the future.

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

  • Posts: 128
  • Thank you received: 3
8 years 8 months ago #234102

Well I first had the code directly in the view, but I wasn't sure if it would be wise to have database connections and queries directly in the view.

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

  • Posts: 82867
  • Thank you received: 13373
  • MODERATOR
8 years 8 months ago #234104

Well, it's indeed bad practice when you're developing a whole application in MVC. But for a custom development I don't see a problem. Having your code easy to maintain by reducing the locations where you add your custom code, and simplifying it in the process is more important from my point of view in that case.

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

Time to create page: 0.061 seconds
Powered by Kunena Forum