致命错误:对function.php中的非对象调用成员函数add_to_cart()
我正在定制woo commerce插件,将产品从前端添加到购物车中。我已经在functions.php中编写了这个函数,但是我遇到了一个致命错误。有人知道怎么解决吗致命错误:对function.php中的非对象调用成员函数add_to_cart(),function,customization,woocommerce,Function,Customization,Woocommerce,我正在定制woo commerce插件,将产品从前端添加到购物车中。我已经在functions.php中编写了这个函数,但是我遇到了一个致命错误。有人知道怎么解决吗 if (isset($_POST["addcustomcarts"])) { echo $_SERVER[QUERY_STRING]; // echo $_SERVER[REQUEST_URI]; echo "i am in if";
if (isset($_POST["addcustomcarts"]))
{
echo $_SERVER[QUERY_STRING];
// echo $_SERVER[REQUEST_URI];
echo "i am in if";
//exit();
add_filter('woocommerce_before_cart', 'customcart');
function customcart() {
echo "i am in function";
//global $woocommerce;
$my_post = array(
'post_title' => 'My post',
'post_content' => 'This is my post.',
'post_status' => 'publish',
'post_author' => 1,
'post_type' =>'product'
);
// Insert the post into the database
$product_ID=wp_insert_post( $my_post );
add_post_meta($product_ID, '_regular_price', 100, $unique);
add_post_meta($product_ID, '_price', 100, $unique);
add_post_meta($product_ID, '_stock_status', 'instock', $unique);
//Getting error on this line.
$woocommerce->cart->add_to_cart( $product_ID, $quantity=1 );
exit( wp_redirect( home_url( "cart" ) ) );
}
customcart();
}
获取此错误-->致命错误:在C:\wamp\www\cutting-edge\u server\wordpress\u theme\wp content\themes\cutting\age\responsive\functions.php的第56行对非对象调用成员函数add\u to\u cart(),为什么要注释这一行 //全球商业?我认为这可能是问题所在