Page 1 of 3

format_raw

Posted: Tue Feb 27, 2024 3:07 pm
by heatherbell
https://github.com/CE-PhoenixCart/Phoen ... er.php#L41
$data['data-product-price'] = $product->format_raw();
which outputs the price with tax as applicable.
Want to add, e.g.
$data['data-product-fullPrice] = $product->get('products_price');
but with tax as applicable similar to above but have hit the wall - any pointers please?

Re: s01e10 - Sale Sticker

Posted: Tue Feb 27, 2024 6:33 pm
by burt
heatherbell wrote: Tue Feb 27, 2024 3:07 pm $data['data-product-price'] = $product->format_raw();
which outputs the price with tax as applicable.
$data['data-product-fullPrice] = $product->get('products_price');
but with tax as applicable similar to above but have hit the wall - any pointers please?
Could you rephrase the question as I don't quite understand what you are looking for.

It seems like you're looking for $product->format_raw(); as that would be the answer to what I have quoted second.

Maybe you want it without tax ?

Re: s01e10 - Sale Sticker

Posted: Wed Feb 28, 2024 7:18 am
by heatherbell
burt wrote: Tue Feb 27, 2024 6:33 pmI don't quite understand what you are looking for.
LOL - story of my life!
TY anyway, now gone a different route but after some hours browsing core, now beginning to understand format_raw()

Re: format_raw

Posted: Wed Feb 28, 2024 10:28 am
by heatherbell
Still experimenting.
Adding this code in product_card

Code: Select all

echo $product->format_raw('price') . '<br>';
echo $product->format_raw();
outputs this which is great
Screenshot 2024-02-28 101623.png
taking it a stage further, (naively) thought to hook in that content so replaced that code with

Code: Select all

echo $GLOBALS['hooks']->cat('example');
and made a hook

Code: Select all

class hook_shop_siteWide_productExample {
    function listen_example() {
    	echo $product->format_raw('price') . '<br>';
    	echo $product->format_raw();
    } 
}
but that fails with
Undefined variable $product
and
Call to a member function format_raw() on null
so tried, in the hook, with

Code: Select all

    echo $GLOBALS['product']->format_raw('price') . '<br>';
    echo $GLOBALS['product']->format_raw();
but that fails with
Undefined array key "product"
and
Call to a member function format_raw() on null
Just doing it wrong or trying to do impossible?

Re: format_raw

Posted: Wed Feb 28, 2024 11:26 am
by burt
Hook runs before product object?

Re: format_raw

Posted: Wed Feb 28, 2024 11:32 am
by heatherbell
burt wrote: Wed Feb 28, 2024 11:26 am Hook runs before product object?
Naively thought that if the code runs OK in that position in the file, then a hook in that same position calling the same code should run. Still stumped as to what's wrong.

Re: format_raw

Posted: Wed Feb 28, 2024 11:55 am
by heatherbell
So changing the code in hook to

Code: Select all

    global $product;
    echo $product->format_raw('price') . '<br>';
    echo $product->format_raw();
gives just the one error now
Uncaught Error: Call to a member function format_raw() on null
so still stumped :(

Re: format_raw

Posted: Wed Feb 28, 2024 12:09 pm
by burt
I think you'll find that there is still no available product object.

In other words: $product = NULL

print_r($product) and you will ascertain.

Re: format_raw

Posted: Wed Feb 28, 2024 12:23 pm
by heatherbell
burt wrote: Wed Feb 28, 2024 12:09 pm print_r($product)
Inserting that into the file at exactly the same position as the hook call (commented out) outputs all the product data so guessing $product is doing as expected so still stumped.
As per previous post, the code works at that position in the file but the hook containing the same code called at the same position does not - that's what is not understood.

Re: format_raw

Posted: Wed Feb 28, 2024 12:28 pm
by Kofod95
heatherbell wrote: Wed Feb 28, 2024 12:23 pm Inserting that into the file at exactly the same position as the hook call
Yes, your problem is, that hooks are processed before the product-object. Meaning, that even if the position in the file has the data, the hooks are processed before that and just injected at the place of the hook-call - makes sense?

//Daniel