我在从wp\\u ajax\\u函数内部调用类方法时遇到问题。我的代码如下所示:
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()) );
}
问题是我无法访问update\\u cart()函数中的类属性。因此,当我调用get\\u cart\\u contents()方法时,它返回null。提前感谢您的帮助。拉法尔