0

我正在自定义 woo-commerce 插件以将产品从前端添加到购物车中。我已经在functions.php 中编写了该函数,但出现了致命错误。任何人都知道如何解决它?

  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_server\wordpress_theme\wp-content\themes\cutting_age\responsive\functions.php 中的非对象上调用成员函数 add_to_cart()第 56 行

4

1 回答 1

0

你为什么评论这一行 //global $woocommerce; ? 我认为这可能是问题

于 2013-10-28T09:40:30.370 回答