I have a problem with calling a class method from inside a wp_ajax_function. My code looks like this:
class Cart {
public $cart_content;
public $cart_id;
public $db;
public function __construct() {
$this->db = $GLOBALS['wpdb'];
add_action( 'wp_ajax_update_cart', array($this, 'update_cart') );
add_action( 'wp_ajax_nopriv_update_cart', array($this, 'update_cart') );
}
public function get_cart_content($cart_id = false) {
if( !$cart_id ) $cart_id = $this->cart_id;
$content = $this->db->get_row( $this->db->prepare("SELECT content FROM {$this->db->prefix}$this->cart_table WHERE cart_id = '%s'", $cart_id) );
$this->cart_content = $content ? unserialize($content->content) : $content;
return $this->cart_content;
}
public function update_cart() {
$items = $_POST['items'];
wp_send_json( $this->get_cart_content()) );
}
The problem is that I cannot access class properties inside updat_cart() function. So when I call get_cart_contents() method it return null. Thanks in advance for any help. Rafal