Skip Navigation

[Resolved] Woocommerce different Prodzct Layout

This thread is resolved. Here is a description of the problem and solution.

Problem:

There are some PHP errors in user's custom codes.

https://toolset.com/forums/topic/woocommerce-different-prodzct-layout/#post-1137997

Solution:

It is fixed by custom codes:

https://toolset.com/forums/topic/woocommerce-different-prodzct-layout/#post-1141985

Relevant Documentation:

This support ticket is created 6 years, 3 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 9 replies, has 4 voices.

Last updated by Nashaat 6 years, 2 months ago.

Assisted by: Luo Yang.

Author
Posts
#1137997

Hi i am using a hook for displaying a product layout depending on which product category. This code work just fine and showing me the right layout for the right category.

https://toolset.com/forums/topic/different-single-layout-templates-of-product-woocommerce-depending-on-category/#post-913876

The thing is. When i turn debugging mode on i see following warning on all my pages even if its not a product page..

"Notice: Trying to get property of non-object in /home3/tatly/tat.ly/c/wp-content/toolset-customizations/woocommerce-product-different-layout.php on line 9"

I am not sure what is the issue with line 9!

function assign_layout_by_post_status( $id, $layout ){
        global $product;
				
		$product_cats = get_the_terms( $product->ID, 'product_cat');
		
		if(is_array($product_cats)){
				foreach($product_cats as $k=>$v):
					if($v->slug == 'rentalcarcat'){
						$id = 8670;
					}else if($v->slug == 'usedcarcat'){
						$id = 8674;
					}
				endforeach;
		}
			
        return $id;
}
 add_filter('get_layout_id_for_render', 'assign_layout_by_post_status', 10, 2);

#1138143

"Trying to get property of non-object" means, the code tries to get a property of something that is not having a property.
For example, sometimes one needs to get posts objects (the entire post) and then extract from it the id.
If we do not receive the expected object we get that error later (as well if we do not check if the $variable is really an object first).

When I read that code, I assume $v->slug is throwing the error, as it is the only property you try to get from a supposed object in this code.
It could also be "$product->ID", however, since you point to line 9 (but that might be different on your local) I assume it's rather the $v->slug.

Since you say you get that error everywhere even on products, I think there are 2 issues:
- you should not run the code on non-products
- you should check what $v is exactly.
That can be done by var_dump($v);
It will probably tell you "null", but should hold a whole lot of info about the product instead.

If that is the case, you need to check what $product_cats holds (again with var_dump).
There you probably will get some data, then you need to adjust your code so to fetch the correct properties of that data.

We can partially assist with this, I would start with above tests on a staging site, we can then help to adjust it if possible.

#1139565

Thanks Beda,

I have tried to add var_dump($v); to my function i am not sure i get it but i think the issue is following.

I am using 3 Layouts for products depending on the category
1- one for New Cars "0kmcarcat"
2- second for used Cars "usedcarcat"
3- third for Rental Cars "rentalcarcat"

this layouts should show up depending on which product category this Ad has. for example if this car has the product category slug "0kmcarcat" which means New Cars Category, then it will show up the main layout which is new car.

in the above mentioned code we have defined which layout should show up for each of two following categories.

- Used car has "usedcarcat"
- Rental Cars has "rentalcarcat"

but we didn't defined "0kmcarcat" because we have assigned this layout to product as main. i am not sure this is the issue!

this is the debugging showing after adding var_dump

Notice: ID was called incorrectly. Product properties should not be accessed directly. Backtrace: require('wp-blog-header.php'), require_once('wp-includes/template-loader.php'), do_action('template_redirect'), WP_Hook->do_action, WP_Hook->apply_filters, Class_WooCommerce_Views->woocommerce_views_activate_template_redirect, include('/plugins/woocommerce-views/templates/single-product.php'), the_ddlayout, get_the_ddlayout, WPDD_Layouts->get_layout_content_for_render, WPDD_Layouts_RenderManager->get_layout_content_for_render, WPDD_Layouts_RenderManager->get_layout_id_for_render, apply_filters('get_layout_id_for_render'), WP_Hook->apply_filters, assign_layout_by_post_status, WC_Abstract_Legacy_Product->__get, wc_doing_it_wrong Please see Debugging in WordPress for more information. (This message was added in version 3.0.) in /home3/folder/mydomain.com/c/wp-includes/functions.php on line 4161
object(WP_Term)#6787 (11) { ["term_id"]=> int(27) ["name"]=> string(16) "רכב 0 ק"מ" ["slug"]=> string(9) "0kmcarcat" ["term_group"]=> int(0) ["term_taxonomy_id"]=> int(27) ["taxonomy"]=> string(11) "product_cat" ["description"]=> string(0) "" ["parent"]=> int(0) ["count"]=> int(29) ["filter"]=> string(3) "raw" ["meta_value"]=> string(1) "0" }
You need to select a layout for this page. The layout selection is available in the page editor.
#1140182

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - could you please share problem URL and access details.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I would additionally need your permission to de- and re-activate Plugins and the Theme, and to change configurations on the site. This is also a reason the backup is really important. If you agree to this, please use the form fields I have enabled below to provide temporary access details (wp-admin and FTP).

I have set the next reply to private which means only you and I have access to it.

#1141251

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - what if you remove the assignment to Product for the layout "Template for New Cars":
=> hidden link

And with your code - add one more condition:

function assign_layout_by_post_status( $id, $layout ){
        global $product;
				
		$product_cats = get_the_terms( $product->ID, 'product_cat');
		
		if(is_array($product_cats)){
				foreach($product_cats as $k=>$v):
					if($v->slug == 'rentalcarcat'){
						$id = 8670;
					}else if($v->slug == 'usedcarcat'){
						$id = 8674;
					}else if($v->slug == '0kmcarcat'){
                                              $id = 8682;  // for new car category
                                       }
				endforeach;
		}
			
        return $id;
}
 add_filter('get_layout_id_for_render', 'assign_layout_by_post_status', 10, 2);

Does that makes any difference?

#1141374

unfortunately no. i have tried your suggestion but the the same debugging issue showing up. i Kept debugging true so you can see it : hidden link

#1141378
Layouts for new car Weird.png

i have unassigned the layout for product post type and the layout moved to

Template for single posts or pages and looks weird.

#1141985

Hello,

Minesh isn't available, I will take care of this thread.

Yes, you are right, there is an issue in the custom codes you mentioned above, if you need it works only in single product post, I suggest you add one line, like this:

function assign_layout_by_post_status( $id, $layout ){
if(!is_singular('product')) return $id;  // add this line
....

More help:
https://developer.wordpress.org/reference/functions/is_singular/

#1142283

No this didn't work unfortunately!

#1142284

My issue is resolved now. The right way to do this, is to change $product->ID to $product->get_id(). accessing the product data directly was the issue.