• Resolved jeffb502

    (@jeffb502)


    I need to create a woocommerce product with code, I am using this code to create the product.

    function createNewProductTest() {

    $post_id = wp_insert_post( array(
    ‘post_title’ => ‘Custom Suscription’,
    ‘post_content’ => ‘Text’,
    ‘post_status’ => ‘publish’,
    ‘post_type’ => “product”,
    ) );

    wp_set_object_terms( $post_id, ‘subscription’, ‘product_type’ );
    update_post_meta( $post_id, ‘_price’, ‘600’ );
    update_post_meta( $post_id, ‘_subscription_price’, ‘600’ );
    update_post_meta( $post_id, ‘_subscription_period’, ‘year’ );
    update_post_meta( $post_id, ‘_subscription_length’, ‘1’ );

    $woocommerce->cart->add_to_cart( $post_id );

    }

    The code works correctly, the product is created and saved in woocommerce, the problem is that once the product is finished being created I must add it to the cart, for that I am using this code.

    $woocommerce->cart->add_to_cart( $post_id );

    However even though the product that you successfully add to woocommerce fails to add to cart, it returns this error to me.

    Product has been removed from your cart because it has since been modified.

    As I can add the product to the cart correctly, I think the problem is that the meta fields are still being updated, but I don’t know how to make the function wait for the product to finish updating to be added to the cart.

    I would appreciate any help, Thank you

Viewing 4 replies - 1 through 4 (of 4 total)
  • Zworthkey

    (@zworthkey)

    Hi,
    Did you try this WC()->cart->add_to_cart( $product_id); because that works for me.
    Thank you

    Bubblechaz

    (@bubblechaz)

    Hello

    I am having a very similar issue

    Mine looks like this

     $my_post = array(
        'post_title'    => $_POST['title'],
       'post_status'   => 'publish',
        'post_author'   => 1,
        'post_type'      => 'product'	
        );
    
     $product_ID = wp_insert_post( $my_post );
    $numberAsString = number_format($price, 2);
    wp_set_object_terms( $product_ID, 'subscription', 'product_type' ); 
    wp_set_object_terms( $product_ID, 'Guides', 'product_cat' ); 
    update_post_meta($product_ID , '_subscription_period', 'year');
    update_post_meta($product_ID , '_subscription_period_interval', '1');
    update_post_meta($product_ID, '_regular_price', $numberAsString );
    update_post_meta($product_ID , '_subscription_price', $numberAsString );
    update_post_meta($product_ID, '_price', $numberAsString );
    update_post_meta($product_ID, '_stock_status', 'instock' );
    update_post_meta($product_ID, '_manage_stock', 'yes');
    update_post_meta ($product_ID, '_stock', 1);
    WC()->cart->add_to_cart( $product_ID );
    wp_redirect( home_url('/cart/') ); exit;

    If I removed wp_set_object_terms( $product_ID, 'subscription', 'product_type' ); It will add to cart fine.

    Its very unlikely this will help you, but maybe it will help others who are trying to help you.

    Plugin Support Tseten a11n

    (@tibetanitech)

    We haven’t heard back from you in a while, so I’m going to mark this as resolved – we’ll be here if and/or when you are ready to continue.

    Thread Starter jeffb502

    (@jeffb502)

    I solved it by sending the product by ajax, if I do it with that method the product is added correctly

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘How to add a product to the cart after it is created’ is closed to new replies.