format_raw

Open to all! Ask other shopowners for help.
User avatar
burt
Core Team
Posts: 4560
Joined: Tue Oct 29, 2019 9:37 am
Phoenix Version: v1.1.0.8
Has thanked: 252 times
Been thanked: 413 times

Re: format_raw

Post by burt »

print_r($product) in the Hook.
You will, I think, see NULL $product as the Hook runs before the Product object

So you need to think of another way to access that data.
Maybe throw something from the card and catch it in the hook.

This is the life of a developer, fun no?
It gets duller and duller the more years you do it.
I am not here to build for you.
I am here to build with you. Let's help each other.


Join The Code Co-op to get access to your library in the Code Co-op Forum
heatherbell
Senior Contributor
Posts: 2540
Joined: Mon Oct 07, 2019 4:39 am
Phoenix Version:
Has thanked: 35 times
Been thanked: 243 times

Re: format_raw

Post by heatherbell »

Kofod95 wrote: Wed Feb 28, 2024 12:28 pmmakes sense?
TY. What you say does make sense, otherwise not so much. LOL!
burt wrote: Wed Feb 28, 2024 12:29 pm This is the life of a developer, fun no?
Yes, even banging head against wall is fun when stopped!
burt wrote: Wed Feb 28, 2024 12:29 pm It gets duller and duller the more years you do it.
Could be applied to life if counter-measures were not taken but feel sure, even you, still finds some stars in an otherwise dark sky.
Anyway, TY to you both for replies and patience and feck this hook idea! (though nothing tried, nothing learned)
ecartz
Core Team
Posts: 3084
Joined: Tue Nov 05, 2019 6:02 pm
Phoenix Version:
Has thanked: 4 times
Been thanked: 208 times

Re: format_raw

Post by ecartz »

heatherbell wrote: Wed Feb 28, 2024 10:28 am

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();
    } 
}

Code: Select all

$parameters = [
  'product' => $product,
];
echo $GLOBALS['hooks']->cat('example', $parameters);

Code: Select all

class hook_shop_siteWide_productExample {
    function listen_example($parameters) {
    	return $parameters['product']->format_raw('price') . '<br>'
    	  . $parameters['product']->format_raw();
    } 
}
Kofod95 wrote: Wed Feb 28, 2024 12:28 pm 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?
No. The problem is that the $product in the product_card is not global, so to access it via hook, you have to pass it into the hook publisher so that the listener can hear it. The order of processing is irrelevant here.
heatherbell
Senior Contributor
Posts: 2540
Joined: Mon Oct 07, 2019 4:39 am
Phoenix Version:
Has thanked: 35 times
Been thanked: 243 times

Re: format_raw

Post by heatherbell »

ecartz wrote: Wed Feb 28, 2024 10:44 pmThe problem is that the $product in the product_card is not global, so to access it via hook, you have to pass it into the hook publisher so that the listener can hear it. The order of processing is irrelevant here.
Sincere thanks for both the answer and, more important to me, the explainer.
This really has opened the gate for me :)
Delete the now superfluous ; from the code in the hook file and works as expected.

Code: Select all

class hook_shop_siteWide_productExample {
    function listen_example($parameters) {
    	return $parameters['product']->format_raw('price') . '<br>'
    	  . $parameters['product']->format_raw();
    } 
}
heatherbell
Senior Contributor
Posts: 2540
Joined: Mon Oct 07, 2019 4:39 am
Phoenix Version:
Has thanked: 35 times
Been thanked: 243 times

Re: format_raw

Post by heatherbell »

Playing with this today and found difficulty getting the products_date_added data.
Used $product->get('date_added') and $product->get('products_date_added') in product_card to output that data on cards, both produced some unexpected results in some areas of the site that do not correlate to the DB data. Guessing something to do with manufacturers in one of the core DB queries?
Can't find what to use instead of $product->get('date_added') to output that data.
ecartz
Core Team
Posts: 3084
Joined: Tue Nov 05, 2019 6:02 pm
Phoenix Version:
Has thanked: 4 times
Been thanked: 208 times

Re: format_raw

Post by ecartz »

heatherbell wrote: Thu Feb 29, 2024 4:12 pm produced some unexpected results
I'm not following. What change did you make exactly and what unexpected results did you get? I follow that $product->get('date_added') was added somewhere. Where? Did you make any other changes?
heatherbell
Senior Contributor
Posts: 2540
Joined: Mon Oct 07, 2019 4:39 am
Phoenix Version:
Has thanked: 35 times
Been thanked: 243 times

Re: format_raw

Post by heatherbell »

ecartz wrote: Thu Feb 29, 2024 10:17 pm I follow that $product->get('date_added') was added somewhere. Where?
heatherbell wrote: Thu Feb 29, 2024 4:12 pm Used $product->get('date_added') in product_card to output that data on cards
<?= $product->get('date_added'); ?> added to product_card component on 1.0.8.21
The data in products table is this:
Screenshot 2024-03-01 054031.png
Note that Lime (see bottom row) products_date_added is 2024-02-26 15:51:03
Output on index is 2024-02-26 15:51:03 - expected
Output on index nested is 2024-02-26 15:51:03 - expected
Output on index products is 2024-02-18 15:51:03 - unexpected, That data does not occur in products table.
Output on specials, products_new and advanced_search_result is 2024-02-18 15:51:03 - unexpected, That data does not occur in products table.
What to use instead of $product->get('date_added') to output products_date_added on all cards?
You do not have the required permissions to view the files attached to this post.
ecartz
Core Team
Posts: 3084
Joined: Tue Nov 05, 2019 6:02 pm
Phoenix Version:
Has thanked: 4 times
Been thanked: 208 times

Re: format_raw

Post by ecartz »

heatherbell wrote: Fri Mar 01, 2024 5:57 am That data does not occur in products table.
Sure it does. That's the date for Orange. This suggests to me that when you call $product->get('date_added'), $product refers to Orange. I.e. ->get('date_added') is correct. $product is incorrect. So the thing that you should be troubleshooting is why $product is incorrect.

Previously you had been attempting to use global $product. That's exactly the kind of thing that would cause $product to be set to the wrong value. Look for places where you set $product after using global.
heatherbell
Senior Contributor
Posts: 2540
Joined: Mon Oct 07, 2019 4:39 am
Phoenix Version:
Has thanked: 35 times
Been thanked: 243 times

Re: format_raw

Post by heatherbell »

ecartz wrote: Fri Mar 01, 2024 6:31 am Sure it does. That's the date for Orange.
Screenshot 2024-03-01 064021.png
Now changed that data in DB so that data is definitely not there with exactly the same result
Screenshot 2024-03-01 064230.png
Searching the DB shows that data occurring in manufacturers so maybe that's correlated.
ecartz wrote: Fri Mar 01, 2024 6:31 am Previously you had been attempting to use global $product.
This is on a clean install, the only addition to product_card component is <?= $product->get('date_added'); ?>
Still stumped by the unexpected outputs and do not know what to use instead of <?= $product->get('date_added'); ?> that would output products_date_added on all cards.
You do not have the required permissions to view the files attached to this post.
ecartz
Core Team
Posts: 3084
Joined: Tue Nov 05, 2019 6:02 pm
Phoenix Version:
Has thanked: 4 times
Been thanked: 208 times

Re: format_raw

Post by ecartz »

It looks like ->get('products_date_added') would be correct in any place where the manufacturer data is loaded.


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