[SOLVED] Geting the stock from external ERP server and save it after ordering

  • Posts: 224
  • Thank you received: 26
  • Hikashop Business
5 years 9 months ago #294767

-- HikaShop version -- : 3.4.1
-- Joomla version -- : 3.8.10

Hi,
I'm when i create an order on my shop, i'm getting the REAL stock from another server, from an ERP.
So, let's say that in ERP i have stock 200 for Product A, i add Product A in cart, quantity 1, on checkout page when i press Finish, what trigger can i use to save the remaining stock 199 in hikashop ?
A) onBeforeOrderUpdate(&$order,&$do)
B) onBeforeProductUpdate(&$element,&$do)? (I think this is triggered only when i add or edit the product, not when stock is updated, right?)
C) onBeforeOrderCreate

And i have to use this function to get each product from the order to change the stock?

function getProductInfo($product_id) {
        $productClass = hikashop_get('class.product');
        $productinfo = $productClass->get($product_id);
        return $productinfo;
    }

And if so, how do i save the changes, with: $product->save(); ?

Last edit: 5 years 9 months ago by oxido.

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

  • Posts: 12953
  • Thank you received: 1778
5 years 9 months ago #294770

Hello,

Note that if you want to update Hikashop product's stock you'll just have to use the "Update the product stock on confirmed status" option through the page "Hikashop->System->Configuration->Main".

If you want to execute special code when order are confirmed then using the "onAfterOrderUpdate(&$order,&$send_email)" trigger will do the job, for more information :
www.hikashop.com/support/documentation/6...l#onAfterOrderUpdate

onAfterOrderUpdate(&$order,&$send_email)
This function will be triggered by HikaShop after an order is updated. You will get in the $order object the information which is updated for the order. The $send_email variable can be set to true/false in case you want to allow/disallow the email notification to the customer.


You'll have to check the the order_status is set to a "confirmed" order status before executing your code.

Best regards,
Mohamed Thelji.

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

  • Posts: 224
  • Thank you received: 26
  • Hikashop Business
5 years 9 months ago #294815

What is wrong with this function, why the product quantity is not saved?

function onAfterOrderUpdate(&$order,&$send_email){
        foreach ($order->products AS $product){
            $this->setNewStock($product->product_id);
        }
    }

function setNewStock($product_id) {
        $productClass = hikashop_get('class.product');
        $product = $productClass->get(3);
        $product->product_quantity = "10";
        $productClass->save();
    }

Last edit: 5 years 9 months ago by oxido.

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

  • Posts: 81540
  • Thank you received: 13071
  • MODERATOR
5 years 9 months ago #294816

Hi,

You need to pass the product to the save function.
So the line:
$productClass->save();
should be:
$productClass->save($product);

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

  • Posts: 224
  • Thank you received: 26
  • Hikashop Business
5 years 9 months ago #294845

Thank you for the replay nicolas,
Using this line $productClass->save($product);
iget this error:
1054 Unknown column 'alias' in 'field list'
And the URL become: /index.php/hikashop-menu-for-products-listing/checkout/confirm/cart_id-24
with this message in page: The requested page can't be found.

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

  • Posts: 12953
  • Thank you received: 1778
5 years 9 months ago #294856

Hello,

Before debugging your code, can you use the "Check database" button through the page "Hikashop->System->Configuration" and tes it again ? Thank you.

Best regards,
Mohamed Thelji.

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

  • Posts: 224
  • Thank you received: 26
  • Hikashop Business
5 years 9 months ago #294890

Hi Mohamed, i have Hikashop 3.5.0 now.
I did what you said, and i have the same problem.
This is the code in my plugin:

<?php
defined('_JEXEC') or die;

jimport('joomla.plugin.plugin');

class plgHikashopSeniorerp extends JPlugin {

    public function __construct(&$subject, $config = array()) {
        parent::__construct($subject, $config);
    }

    function onAfterOrderUpdate(&$order,&$send_email){
        dump($order, 'COM - onAfterOrderUpdate: ');
        $send_email = FALSE;
        foreach ($order->products AS $product){
            $this->setNewStock($product->product_id);
        }
    }
    
    function setNewStock($product_id) {
        $productClass = hikashop_get('class.product');
        $product = $productClass->get($product_id);
        $product->product_quantity = "10";
        $productClass->save($product);       
    }
}

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

  • Posts: 26007
  • Thank you received: 4004
  • MODERATOR
5 years 9 months ago #294899

Hello,

You might not have access to the order products during the trigger "onAfterOrderUpdate".
You have that informaton during the order creation (and it's in "order->order->products" but for an update ; you don't and specially if the order modification is just regarding the "order status".
You should perform a lot of check in that trigger to just perform your action when it is really necessary and you should load the order products (or the full order) when it's the right time.

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: 224
  • Thank you received: 26
  • Hikashop Business
5 years 9 months ago #294933

All right, let me simplify my requirement.
I want to create a function that get the product with all of his details, change the stock and save the product, how can I do it?
It does not matter how the function will be called.

This should work, but is not working because i don't have access to the product durring the trigger?

function setNewStock($product_id) {
        $productClass = hikashop_get('class.product');
        $product = $productClass->get($product_id);
        $product->product_quantity = "10";
        $productClass->save($product);
    }
Because if this is the problem, i will try to find another way to call the function.

Last edit: 5 years 9 months ago by oxido.

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

  • Posts: 12953
  • Thank you received: 1778
5 years 9 months ago #294939

Hello,

As we said :

You should perform a lot of check in that trigger to just perform your action when it is really necessary and you should load the order products (or the full order) when it's the right time.


Note that you can load the full order by using that kind of code :
	$orderClass = hikashop_get('class.order');
	$full_order = $orderClass->loadFullOrder($order->order_id,true);

Best regards,
Mohamed Thelji.

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

  • Posts: 224
  • Thank you received: 26
  • Hikashop Business
5 years 9 months ago #294977

Hi Mohamed, tnx for the replay.

To load an order by ID, it is assumed that the order already exists in the database, right?
What i'm trying to do is to perform a check of stock for all the products from cart before creating the order.
So i thought to perform the check / update, using "onBeforeOrderCreate" and to set the real stock by taking it from my ERP server and saving it to the site's database, doing this before creating the order, i don't need to change something else in HS code, I just let HikaShop follow the course to creating the order or not.

You should perform a lot of check in that trigger to just perform your action when it is really necessary and you should load the order products (or the full order) when it's the right time.

Can you help me with this, what check's should i perfom?
And when it's the right time? How can I figure this out?

Thank you!

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

  • Posts: 81540
  • Thank you received: 13071
  • MODERATOR
5 years 9 months ago #294983

Hi,

When you do:
$product = $productClass->get($product_id);
$product contains all the data from the product in the hikashop_product table + additional data, like the alias.
If you want to then save that $product, you need to first unset that extra data.
However, if you just want to update the stock of the product, there is no need to load the data of the product and thus you can avoid that issue altogether.
For that, just write your code like that:

function setNewStock($product_id) {
        $productClass = hikashop_get('class.product');
        $product = new stdClass();
        $product->product_id = $product_id;
        $product->product_quantity = "10";
        $productClass->save($product);
    }

The following user(s) said Thank You: oxido

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

  • Posts: 224
  • Thank you received: 26
  • Hikashop Business
5 years 9 months ago #295002

:woohoo: That was it nicolas!
Thank you!

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

Time to create page: 0.087 seconds
Powered by Kunena Forum