hook (or other solution) to save the products_name in the orders_products table in "standard" language

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

hook (or other solution) to save the products_name in the orders_products table in "standard" language

Post by loop »

Hi All

My Problem is, i have 4 languages in my shop and if the user orders in french, thenn the products_name in the orders_products table is also in french

And for us it's difficult to manage this in the shop ...so my question. what is the easiesst, best way to save in the mysql table the default language of the products_name

my idea is to make a hook to update it with the right language as i have done with manufacturers_id

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']));
      }
}
ist there a better way? if not, what is the cleanest query to update it to the products_name with languages_id = 2 ?
thanks in advance!


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 (or other solution) to save the products_name in the orders_products table in "standard" language

Post by ecartz »

Don't store it in the database. Just read the correct name in admin.

Remember that the database name is what is shown to the customer. You don't want to change that. Just use the product ID to fetch the correct name in admin and display that to your users. The hook to do that would be hook_admin_orders_rewrite_name and the listener would be listen_editAction

Beyond that, what you're describing is what the model or GTIN should do. If your employees used either or both of those, they are language independent.
loop
Contributor
Posts: 253
Joined: Thu Mar 25, 2021 12:26 pm
Phoenix Version:
Has thanked: 7 times
Been thanked: 3 times

Re: hook (or other solution) to save the products_name in the orders_products table in "standard" language

Post by loop »

Hi ecartz, i know that ean is worldwide, the point is the invoice is printed out in our warehouse and the employe looks on the invoice (paper) and search the product. And it's more difficult to read and understand which product the customer like than in the own language....that's why i wanted
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 (or other solution) to save the products_name in the orders_products table in "standard" language

Post by ecartz »

loop wrote: Thu Nov 30, 2023 3:39 pm the invoice is printed out in our warehouse
Yes. That's what I'm saying. Either change that step and print the product name that you want or change the next step and have the employee look at something other than the product name. The product model should already be on the invoice.

If you want to change the invoice, it would be hook_admin_invoice_rewrite_name and listen_injectBodyStart -- iterate over the products and change the names as desired.
User avatar
burt
Core Team
Posts: 4550
Joined: Tue Oct 29, 2019 9:37 am
Phoenix Version: v1.1.0.8
Has thanked: 252 times
Been thanked: 412 times

Re: hook (or other solution) to save the products_name in the orders_products table in "standard" language

Post by burt »

If using the name is non-negotiable in your system...ie, you cannot change to GTIN or Model...

Rather than change the name of what the buyer ordered, which would change other things (update emails, account_history and so on)...add in a column in the orders_products table which is just for admin use;

admin_view_product_name varchar 255
have a hook which activates in the checkout process which updates this to the product in your needed language.

Then add this into the invoice or packing slip (or whatever you use for order picking).
loop
Contributor
Posts: 253
Joined: Thu Mar 25, 2021 12:26 pm
Phoenix Version:
Has thanked: 7 times
Been thanked: 3 times

Re: hook (or other solution) to save the products_name in the orders_products table in "standard" language

Post by loop »

Hi All
i have GTIN and everything but i like to have the german language in the db after customer bought it. it's easyer for us, for warehouse for admin ...

i already made a hook in checkout_process

Code: Select all

class hook_shop_checkout_process__15_update_orders_products {

    public function listen_startCheckout() {
        $GLOBALS['db']->query(sprintf(<<<'EOSQL'
UPDATE orders_products op 
INNER JOIN products p ON op.products_id = p.products_id
INNER JOIN products_description pd ON p.products_id = pd.products_id
SET op.manufacturers_id = p.manufacturers_id, op.products_name = pd.products_name
WHERE pd.language_id = '2' AND op.orders_id = %d
EOSQL
          , $GLOBALS['order_id']));
      }
}
the strange thing is the code works 100% (if i execute it in sql it works 100%, if i make a testorder it also executed) but i have a couple of orders where this is not triggered or at least not with the right order_id

does somebody has a idea under which circumstances this hook is not fired or has no orders_id? in 90% it works perfect, but not everytime :(
loop
Contributor
Posts: 253
Joined: Thu Mar 25, 2021 12:26 pm
Phoenix Version:
Has thanked: 7 times
Been thanked: 3 times

Re: hook (or other solution) to save the products_name in the orders_products table in "standard" language

Post by loop »

i changed (moved) the hook to checkout_success, now it works. i think i have no disadvantage here:

Code: Select all

<?php
/*
  $Id$

  CE Phoenix, E-Commerce made Easy
  https://phoenixcart.org

  Copyright (c) 2021 Phoenix Cart

  Released under the GNU General Public License
*/
class hook_shop_checkout_success_orders_products_update {
    public function listen_injectSiteStart() {
        if(!empty($GLOBALS['order_id']) && $GLOBALS['order_id'] > 0){
            $GLOBALS['db']->query(sprintf(<<<'EOSQL'
UPDATE orders_products op 
INNER JOIN products p ON op.products_id = p.products_id
INNER JOIN products_description pd ON p.products_id = pd.products_id
SET op.manufacturers_id = p.manufacturers_id, op.products_name = pd.products_name
WHERE pd.language_id = '2' AND op.orders_id = %d
EOSQL
          , $GLOBALS['order_id']));
        }
    }
}
let me know if it's not a good idea here
User avatar
burt
Core Team
Posts: 4550
Joined: Tue Oct 29, 2019 9:37 am
Phoenix Version: v1.1.0.8
Has thanked: 252 times
Been thanked: 412 times

Re: hook (or other solution) to save the products_name in the orders_products table in "standard" language

Post by burt »

There are probably many ways to achieve this. Here is my thought;

A new checkout segment;
https://github.com/CE-PhoenixCart/Phoen ... s/checkout

which loops through your order_products and updates new DB column of each with the "main language" name, using;
https://github.com/CE-PhoenixCart/Phoen ... er.php#L92

$name = \Product::fetch_name($x, $y);

Where x is product ID and $y is the main language ID.

Admin Side (for viewing), eg invoice.php, you'll need to make the order get the new DB column.
This hook might do it;
https://github.com/CE-PhoenixCart/Phoen ... er.php#L47

And then some hardcoded change in invoice.php (I do have invoice/packingslip on the "needs to be looked" list to make no core code changes possible, but that is off in the future somewhere).
loop
Contributor
Posts: 253
Joined: Thu Mar 25, 2021 12:26 pm
Phoenix Version:
Has thanked: 7 times
Been thanked: 3 times

Re: hook (or other solution) to save the products_name in the orders_products table in "standard" language

Post by loop »

hi burt, thank you

but how is my solution? with a hook (no hardcoding) in checkout_success.php? i mean it works perfect and at the moment i don't see something bad about it...
User avatar
burt
Core Team
Posts: 4550
Joined: Tue Oct 29, 2019 9:37 am
Phoenix Version: v1.1.0.8
Has thanked: 252 times
Been thanked: 412 times

Re: hook (or other solution) to save the products_name in the orders_products table in "standard" language

Post by burt »

My view is always this:
If it works for your needs, it is good.


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