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.
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"
"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.
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.
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.
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
....
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.