Hi, hope you are doing well.
Please note that you are using wrong PHP syntax:
$stock["stock-quantity"] = '$stock()';
and if you need to hook into each item of order, you should use “puiw_printinvoice_create_html_item_row_metas” which syntax could be found on https://github.com/peprodev/ultimate-invoice/blob/master/include/admin/class-print.php#L693 .
add_filter( "puiw_get_default_dynamic_params", function ($stock, $optm){
$stock["stock-quantity"] = '';
return $stock;
}, 10, 2);
it should be so ?
I’m not programming expert, I’m trying to be able to import the remaining stock quantity of an order into the order template of my site, could you help me to write the right code please?
Hi,
You should first make a custom template using the guidelines given here:
https://github.com/peprodev/ultimate-invoice/wiki/Add-Customized-External-Template-to-Ultimate-Invoice
Then you need to add a custom macro to use in each order line item, something like the below snippet, and place it in the template you’ve created. (files with the “.row” suffix in the name).
add_filter( "puiw_printinvoice_create_html_item_row_metas", "my_custom_lineitems_macros", 10, 5);
// add the macro: { { { remaining-qty } } }
function my_custom_lineitems_macros ($macros, $item_id, $item, $product_id, $order){
$product = $item->get_product();
$current_qty = $product->get_stock_quantity();
$bought_qty = $item->get_quantity();
$remaining_qty = (int) $current_qty - (int) $bought_qty;
$macros["remaining-qty"] = $remaining_qty;
return $macros;
}
Important note:
This snippet calculated the remaining quantity based on the current quantity of purchased product and NOT the stock amount of product at the time user placed the order. If you wish to not calculate based on the live stock amount value, you should save quantity amount of each line item when user places order and then retrieve it to calculate and show on invoice.
I followed the procedure, and I inserted in the default folder both a prova.row and a prova.row.tpl file containing the code you provided.
But when I went to modify the template instead of going out a number, the words “remaining-qty” came out
*I added the template, and it displays correctly
So, the problem is solved?
No this didn’t work, as no number is displayed, but the code { { {remaining-qty} } }
Dear @arndofiore80, you should use the macro like other macros, with three opening and three closing curly braces, without any space between. The reason we put it like { { {remaining-qty} } } is because the WordPress forum does not allow us to use more than one curly brace attached to a word.
Please ignore slashes in the following line and use a macro like:
{{\{remaining-qty\}}}
Sure, here it is.
I also divided the brackets for the wordpress forum.
But when entering the code this is entered in the correct way.
But the display remains that of the code, and not of the possible number that should be displayed.
ok, perfect I managed to get the result. Thanks millet
You are welcome.
I’m closing the topic.
If there’s anything else please let me know.