Reputation: 320
I am trying to save a post meta using post->ID, But the $post seems to be null and showing notice - "Notice: Trying to get property of non-object" (while accessing $post->ID)
Hence the add_filter doesn't seems to be working at all.
add_filter('add_to_cart_redirect', array($this, 'custom_add_to_cart_redirect')); // Goes in class constructor.
function custom_add_to_cart_redirect() {
global $post;
var_dump($post).die(); // this $post var is always null.
return $this->woocommerce_custom_add_to_cart_get_cartURL(); // The global $post declaration within this method doesn’t works too.
}
I also tried adding wp_post_resetdata() call before the global declaration, But no luck.
Any idea what's going wrong here ?
Upvotes: 1
Views: 3626
Reputation: 223
I ran into a similar situation today, where the global $post
wasn't available when it should've been. On closer examination, I found that every page request was returning true for is_404()
, and the query for post 0 failed to return the data for the global $post
object because of that.
Flushing permalinks did the trick for me (nothing fancy, I just re-saved them).
Upvotes: 3
Reputation: 1192
Are you loading the above code in an external PHP script? You may need to include wp_load.php
Upvotes: 0
Reputation: 9476
You can use woocommerce global variable like
global $woocommerce;
var_dump($woocommerce).die();
Upvotes: 0