onAfterOrderCreate iterating through products

  • Posts: 32
  • Thank you received: 2
12 years 1 month ago #66736

I'm not proficient in PHP or Hikashop, and simply need a few lines of code to link Hikashop to some existing code. I can get my callback to fire after an order has been processed in the shop but I'm at a loss how to actually get at the data in the order. I seem to be going round in circles with the doco and searching.

So can somebody fill in the blanks please? Given the $order parameter, I want my function to be called once for each product ID in the order.

function onAfterOrderCreate(&$order,&$send_email){
    if(!empty($order->order_id)){
        <foreach product in $order>
            my_function(productID, userID);
        </foreach>
    }
    return true;
}

The User ID has to be the Joomla one, not anything Hikashop creates. There might be some configuration item I need to set to force Hikashop to only allow checkout with a logged-in user. Thanks.

Last edit: 12 years 1 month ago by GeoffM.

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

  • Posts: 13201
  • Thank you received: 2322
12 years 1 month ago #66941

Hi,

Try this code:

function onAfterOrderCreate(&$order,&$send_email){
    if(!empty($order->order_id)){
        $class = hikashop_get('class.user');
        $user = $class->get($order->order_user_id);
        foreach($order->cart->products as $product){
            my_function($product->product_id, $user->user_cms_id);
        }
    }

The following user(s) said Thank You: GeoffM

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

  • Posts: 32
  • Thank you received: 2
12 years 1 month ago #68115

Thank you, that works perfectly. Sorry for the delay in replying. Now, how do I get the order quantity per product? I've tried $product->quantity and $product->count and $product->cart_product_quantity (as per DB table) but none work.

Many thanks.

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

  • Posts: 82818
  • Thank you received: 13362
  • MODERATOR
12 years 1 month ago #68116

In the order_product table of the DB the quantity column is called order_product_quantity not cart_product_quantity.
So you should do $product->order_product_quantity

The following user(s) said Thank You: GeoffM

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

  • Posts: 32
  • Thank you received: 2
12 years 1 month ago #68117

I was looking at the wrong table then! Makes sense now - many thanks.

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

Time to create page: 0.070 seconds
Powered by Kunena Forum