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 に答える