Trigger for Product Quantity for Product Variants

  • Posts: 8
  • Thank you received: 0
10 years 10 months ago #165858

-- HikaShop version -- : 2.3.1
-- Joomla version -- : 3.3.1

Hi,

Can´t even find a Topic about this.
I have a problem with the product quantity so i´m thinking about a trigger or somethin.

Let me explain:
I have a product:
Product_a->Variant 1->Variant 1-a
->Variant 1-b
->Variant 2->Variant 2-a
->Variant 2-b
->Variant 2-c
This product have 2 Options and 2 Variants.
if i klick on edit variant then i see 6 entries with product_code, Product_price....
so the variants are in the database.

but i also have the variants in the shop as a standalone product.

is it possible to make a trigger for having always the same product quantity for the variant and the original product.
I know it is possible with options. but i can´t make it with options cause an option is not the same.

i just cant find the right file in the source that changes the product_quantity in the Database.

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

  • Posts: 12953
  • Thank you received: 1778
10 years 10 months ago #165863

Hello,

is it possible to make a trigger for having always the same product quantity for the variant and the original product.

The solution will just be to let the quantity fields of your product variants to empty, and then set a quantity for the main product, so your product variants and your product will share the same quantity.

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

  • Posts: 8
  • Thank you received: 0
10 years 10 months ago #165962

Thx for your reply,

i think you´ve misunderstood.
i dont mean the main product of the variant. i mean a totaly other product.
i have a built a new table in DB where i have the variant ids listed to the original product.

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

  • Posts: 13201
  • Thank you received: 2322
10 years 10 months ago #165964

Hi,

To update stock of the product when the variant stock is changed, you can create a plugin based on:
hikashop.com/support/support/documentati...onAfterProductUpdate

This way when one of the stock is updated, you can do a request to update the other stock.
To make it more dynamic, you can use the custom fields to put the related product id, then to update a product you can get it thanks to:

// custom field name is in this example "relatedprod"
if(!empty($element->relatedprod)){
$productClass = hikashop_get('class.product');
$assocProd = $productClass->get($element->relatedprod);
$assocProd->product_quantity = $element->product_quantity;
$productClass->save($assocProd);
}

The following user(s) said Thank You: idh-webdesign

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

  • Posts: 8
  • Thank you received: 0
10 years 10 months ago #166129

Wow, thx...thats a big step.

So i have a plugin looking like this:

defined('_JEXEC') or die('Restricted access');

class ProTrigger extends JPlugin {
function plgHikashopName(&$subject, $config){
parent::__construct($subject, $config);
}
function onAfterProductUpdate(&$element) {
if(!empty($element->assoc_id)){
$productClass = hikashop_get('class.product');
$assocProd = $productClass->get($element->assoc_id);
$assocProd->product_quantity = $element->product_quantity;
$productClass->save($assocProd);
}
}
}
onAfterProductUpdate("86");


but it doesn´t trigger at the time i think i am missing something.
everything is installed and the custom field "assoc_id" is implemented and filled with some data.

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

  • Posts: 83993
  • Thank you received: 13605
  • MODERATOR
10 years 10 months ago #166130

Hi,

The onAfterProductUpdate function will be called automatically by Joomla when you change the quantity of a product/variant in the backend interface. There is no need to (and you can't) call the function yourself like you did at the end of your code.

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

  • Posts: 8
  • Thank you received: 0
10 years 10 months ago #166359

Okay, i have tried now many different things and start reading the documentary about the Triggers but i cant get them running.

So i´ve made a test code looking like that:

class ProTrigger extends JPlugin {
}
function onAfterProductUpdate(&$element) {
$dbhost = 'HOST';
$dbuser = 'USER';
$dbpass = 'PASS';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db('DB');
$SQL = "INSERT INTO jos_test_table (pro_id, art_nr, off, art_nr_ver) VALUES ('999', 'test accomplished', '1', 'test accomplished')";
mysql_query($SQL);
}


so i wanted to see if the trigger is working correctly. but there is absolutely no data in the DB.
so i think i am missing something in the code.
thx for your help.

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

  • Posts: 83993
  • Thank you received: 13605
  • MODERATOR
10 years 10 months ago #166362

I can see that you put your onAfterProductUpdate function outside of the ProTrigger class. So it's normal that it doesn't work as Joomla won't find it.
Also, the class of the plugin needs to respect a certain format, which is not the case in your code.
Also, the plugin needs a XML file with the correct information in it.
And the plugins needs to be installed and enabled in the plugins manager of Joomla, or the code won't be called.
It's really a Joomla plugin, like any other Joomla plugin.
I invite you to take a look at the plugins in the folder plugins/hikashop for a reference of how to do it.

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

  • Posts: 8
  • Thank you received: 0
10 years 10 months ago #166476

SOLVED!

Thanks for your help. The Plugin is working now correctly.

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

Time to create page: 0.060 seconds
Powered by Kunena Forum