致命错误:调用function.php中的非对象的成员函数add_to_cart()
问题描述:
我正在自定义woo-commerce插件以将产品从前端添加到购物车中。我已经在functions.php中写了这个函数,但是我得到了一个致命错误。任何机构有任何想法如何解决它?致命错误:调用function.php中的非对象的成员函数add_to_cart()
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();
}
得到这个错误 - >致命错误:调用一个成员函数add_to_cart()在C语言的非对象:\瓦帕\ WWW \切割edge_server \ wordpress_theme \可湿性粉剂内容\主题\ cutting_age \ responsive \ functions.php on line 56
答
为什么你评论此行 // global $ woocommerce; ?我认为这可能是问题