最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugins - WooCommerce create new product and add to cart on form submit

programmeradmin0浏览0评论

I am customizing woo-commerce plugin to add product from the front end into the cart. I have written the function in functions.php, but i am getting a Fatal error.

Getting this error-->

Fatal error: Call to a member function add_to_cart() on a non-object in
C:\wamp\www\cutting-edge_server\wordpress_theme\wp-content\themes\cutting_age\responsive\functions.php on line 56

Any body have any idea how to resolve it?

My function.php file

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();
      }

My html file

<form name="addpro" method="post" action="">
    <input type="submit" name="addcustomcarts" value="ADD TOO CART" />
  </form> 

I am customizing woo-commerce plugin to add product from the front end into the cart. I have written the function in functions.php, but i am getting a Fatal error.

Getting this error-->

Fatal error: Call to a member function add_to_cart() on a non-object in
C:\wamp\www\cutting-edge_server\wordpress_theme\wp-content\themes\cutting_age\responsive\functions.php on line 56

Any body have any idea how to resolve it?

My function.php file

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();
      }

My html file

<form name="addpro" method="post" action="">
    <input type="submit" name="addcustomcarts" value="ADD TOO CART" />
  </form> 
Share Improve this question edited Aug 7, 2017 at 17:01 Johansson 15.4k11 gold badges43 silver badges79 bronze badges asked Oct 25, 2013 at 11:25 Sanjay NakateSanjay Nakate 2115 gold badges11 silver badges25 bronze badges 4
  • What happens if you uncomment //global $woocommerce;? – fuxia Commented Oct 25, 2013 at 12:10
  • getting same error.. @toscho – Sanjay Nakate Commented Oct 25, 2013 at 12:12
  • you have any idea????@toscho – Sanjay Nakate Commented Oct 25, 2013 at 12:21
  • 2 1. I already did provide a suggestion on your original question in SO where you asked the exact same thing. 2. I voted to close your question as I already told you in SO that it would be off topic here. 3. You're hijacking someone else's question for your personal gain. – helgatheviking Commented Nov 17, 2015 at 12:36
Add a comment  | 

3 Answers 3

Reset to default 4

I'm not sure exactly what you are doing, but the following code did work for me in that it created a new product and added it to the cart. Note, I had to use $_GET to test on my setup since I don't have the rest of your code and didn't feel like creating a form.

EDIT: I've added a simple <form> element and switched to $_POST. EDIT 2: I've removed the form. Apparently the OP has the form on the front page.

add_action('init', 'customcart');

function customcart() {

  if (isset($_POST["addcustomcarts"])) {

    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 );

    if ( $product_ID ){
      add_post_meta($product_ID, '_regular_price', 100 );
      add_post_meta($product_ID, '_price', 100 );
      add_post_meta($product_ID, '_stock_status', 'instock' );

      //Getting error on this line.
      $woocommerce->cart->add_to_cart( $product_ID, $quantity=1 );

      exit( wp_redirect( get_permalink( woocommerce_get_page_id( 'cart' ) ) ) );

    }

  }

}

The $woocommerce->cart apparently is not an object during the call. Set it to get checked before the line that gives the error:

    if( $woocommerce->cart )
      $woocommerce->cart->add_to_cart( $product_ID, $quantity=1 );

This will make sure the cart is even there and won't execute the line if it's not.

add_action( 'template_redirect', 'add_to_cart_on_custom_page_and_redirect');
 
function add_to_cart_on_custom_page_and_redirect(){
 
    if( is_page( 'my-page' ) ) { // you can also pass a page ID instead of a slug
 
        WC()->cart->add_to_cart( 72 ); // add to cart product with ID 72
        wp_safe_redirect( wc_get_checkout_url() );
        exit();
 
    }
 
}
发布评论

评论列表(0)

  1. 暂无评论