Hook Needed to insert in orders_products Table the manufacturers_id

Open to all! Ask other shopowners for help.
Post Reply
loop
Contributor
Posts: 253
Joined: Thu Mar 25, 2021 12:26 pm
Phoenix Version:
Has thanked: 7 times
Been thanked: 3 times

Hook Needed to insert in orders_products Table the manufacturers_id

Post by loop »

Hi All
I need to save on every order the manufacturers_id from the product (i already have the cell in the table) but i don't know how to make a hook that it's inserted on every product, can somebody help me here? thanks in advance!

Code: Select all

class hook_shop_siteWide_ordersProductsManufacturersId {

    //Save in DB
    function listen_insertOrder($parameters) {
      $ordersProducts = &$parameters['sql_data']['orders_products'];
      foreach ($ordersProducts as $i => $product) {
        $manufacturers_id = Query in the products_table for the ID
        $ordersProducts[$i]['manufacturers_id'] = $manufactures_id ?? '';
      }
    }

}
my idea is something like this, but i'm not sure if this is the way to go,


Join The Code Co-op to get access to your library in the Code Co-op Forum
ecartz
Core Team
Posts: 3084
Joined: Tue Nov 05, 2019 6:02 pm
Phoenix Version:
Has thanked: 4 times
Been thanked: 208 times

Re: Hook Needed to insert in orders_products Table the manufacturers_id

Post by ecartz »

That should be fine from the order insertion standpoint. You might be better off doing the assignment after the order insertion though. Once in orders_products, that could be as simple as

Code: Select all

class hook_shop_checkout_process__15_update_manufacturers_id {

  public function listen_startCheckout() {
    $GLOBALS['db']->query(sprintf(<<<'EOSQL'
UPDATE orders_products op INNER JOIN products p ON op.products_id = p.products_id
SET op.manufacturers_id = p.manufacturers_id
WHERE op.orders_id = %d
EOSQL
      , $GLOBALS['order_id']));
  }

}
That's a single query, where your solution requires a select for each product.
loop
Contributor
Posts: 253
Joined: Thu Mar 25, 2021 12:26 pm
Phoenix Version:
Has thanked: 7 times
Been thanked: 3 times

Re: Hook Needed to insert in orders_products Table the manufacturers_id

Post by loop »

Hi eCartz

Thank you for your help

i did that, i creatad a directory hooks/shop/checkout_process and created the hook file.

Code: Select all

class hook_shop_checkout_process_15_update_manufacturers_id {

    public function listen_startCheckout() {
      $GLOBALS['db']->query(<<<'EOSQL'
UPDATE orders_products op INNER JOIN products p ON op.products_id = p.products_id
SET op.manufacturers_id = p.manufacturers_id
EOSQL
        );
    }
}
the code is executed and it seems it works, but am i right, that this code makes everytime a checkout starts a update over ALL orders_products and update the manufacturers_id of all orders we have (everytime a new customer orders something?

maybe i don't understant exactly the query ..thank you for clarifying..i mean, it works like this i have the manufcaturer_id of the orders, but it seems strange to update on every checkout 200k orders...thank you!
ecartz
Core Team
Posts: 3084
Joined: Tue Nov 05, 2019 6:02 pm
Phoenix Version:
Has thanked: 4 times
Been thanked: 208 times

Re: Hook Needed to insert in orders_products Table the manufacturers_id

Post by ecartz »

You're right. That's unnecessary and potentially counterproductive if you're changing manufacturers frequently.

I updated the query in the previous post to limit it to just the current order.
loop
Contributor
Posts: 253
Joined: Thu Mar 25, 2021 12:26 pm
Phoenix Version:
Has thanked: 7 times
Been thanked: 3 times

Re: Hook Needed to insert in orders_products Table the manufacturers_id

Post by loop »

Hi eCartz
it seems not working (as i already noticed before, somehow also with the old query, only ALL OTHER ORDERS (previus), without the actual order was processed) and now somehow as only the actual order is selected it makes no update at all.
it seems that the order_products is not there at the time of the update (or a other reason i don't see?) do you have a idea?
ecartz
Core Team
Posts: 3084
Joined: Tue Nov 05, 2019 6:02 pm
Phoenix Version:
Has thanked: 4 times
Been thanked: 208 times

Re: Hook Needed to insert in orders_products Table the manufacturers_id

Post by ecartz »

Code: Select all

hook_shop_checkout_process_15_update_manufacturers_id 
That's missing an underscore. It should be

Code: Select all

hook_shop_checkout_process__15_update_manufacturers_id 
Two underscores before the 15.

You could also try changing it to a database hook. It's possible that checkout hooks don't work properly with file hooks.

Or make it

Code: Select all

hook_shop_after__25_update_manufacturers_id 
and

Code: Select all

listen_afterStart
loop
Contributor
Posts: 253
Joined: Thu Mar 25, 2021 12:26 pm
Phoenix Version:
Has thanked: 7 times
Been thanked: 3 times

Re: Hook Needed to insert in orders_products Table the manufacturers_id

Post by loop »

Hi ecartz
Thank you!
the doubel "_" worked!
(can i ask why? why 2 x _ and why 15?)
ecartz
Core Team
Posts: 3084
Joined: Tue Nov 05, 2019 6:02 pm
Phoenix Version:
Has thanked: 4 times
Been thanked: 208 times

Re: Hook Needed to insert in orders_products Table the manufacturers_id

Post by ecartz »

hook_shop_checkout_process__15_update_manufacturers_id

That's made up of pieces, separated by underscores. The pieces are

hook
shop
checkout_process
_15_update_manufacturers_id

So it is a hook, on the shop side, registered to the checkout_process pipeline (which only runs on the checkout_process page). Now, there are a bunch of hooks that run on the checkout_process.php page and listen for startCheckout. One of those hooks is _14_insert_order. You want to run after _14_insert_order. They run in alphabetical order (ASCII). _15 is after _14 in alphabetical order (because 5 is after 4), but 15 is before both (because 1 is before _).

You need the second underscore because it is separated into the pieces by an underscore and the separator is an underscore. So the insert_order database hook is the equivalent of hook_shop_checkout_process__14_insert_order.

The list of hooks that run during checkout in 1.0.8.20 is at https://github.com/CE-PhoenixCart/Phoen ... 146..L1180


Join The Code Co-op to get access to your library in the Code Co-op Forum
Post Reply