• Resolved Hannan

    (@phpcoderhannan)


    Hello,

    I am trying to create simple product programmatically and when i going to apply below code then i got some warning. see this screenshot( https://prnt.sc/N3WcJaZr_DO- ). even after those warning product added more than one also.

    My code:

    $product = new WC_Product_Simple();

    $product->set_name(“Apple Watch”);
    $product->set_slug(“apple-watch”);

    $product->set_short_description(“Excellent Product”);

    $product->set_regular_price(2500);
    $product->set_sale_price(500);

    $product->set_image_id(14);
    $product->set_category_ids(array(20));

    $product->save();

    By the way, I am using WordPress version: 6.2.2 and WooCommerce version is 7.8.2

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hi
    try this way

    $item = array(
        'Name' => 'Product A',
        'Description' => 'This is my test product A',
        'SKU' => '10085WE',
    );
    $user_id = get_current_user(); // this has NO SENSE AT ALL, because wp_insert_post uses current user as default value
    // $user_id = $some_user_id_we_need_to_use; // So, user is selected..
    $post_id = wp_insert_post( array(
        'post_author' => $user_id,
        'post_title' => $item['Name'],
        'post_content' => $item['Description'],
        'post_status' => 'publish',
        'post_type' => "product",
    ) );
    wp_set_object_terms( $post_id, 'simple', 'product_type' );
    update_post_meta( $post_id, '_visibility', 'visible' );
    update_post_meta( $post_id, '_stock_status', 'instock');
    update_post_meta( $post_id, 'total_sales', '0' );
    update_post_meta( $post_id, '_downloadable', 'no' );
    update_post_meta( $post_id, '_virtual', 'yes' );
    update_post_meta( $post_id, '_regular_price', '' );
    update_post_meta( $post_id, '_sale_price', '' );
    update_post_meta( $post_id, '_purchase_note', '' );
    update_post_meta( $post_id, '_featured', 'no' );
    update_post_meta( $post_id, '_weight', '' );
    update_post_meta( $post_id, '_length', '' );
    update_post_meta( $post_id, '_width', '' );
    update_post_meta( $post_id, '_height', '' );
    update_post_meta( $post_id, '_sku', $item['SKU'] );
    update_post_meta( $post_id, '_product_attributes', array() );
    update_post_meta( $post_id, '_sale_price_dates_from', '' );
    update_post_meta( $post_id, '_sale_price_dates_to', '' );
    update_post_meta( $post_id, '_price', '' );
    update_post_meta( $post_id, '_sold_individually', '' );
    update_post_meta( $post_id, '_manage_stock', 'no' );
    update_post_meta( $post_id, '_backorders', 'no' );
    update_post_meta( $post_id, '_stock', '' );

    Thanks
    Ahir Hemant

    Thread Starter Hannan

    (@phpcoderhannan)

    Hello Ahir Hemant,

    You are welcome. by the way, right now I do not want to try this way. Actually i need to know why those warning are coming. So if you know that, please shear with me.

    Thank’s

    Hannan

    Hi @phpcoderhannan ,

    Thanks for reaching out!

    I understand that you need help with the errors you are seeing when applying your custom code which allows you to programmatically create a simple product, is this correct?

    Kindly note that custom code is outside?our scope of support. For help with custom code, you could?hire a developer?or ask your customization questions in one of the channels below.

    Hope this points you in the right direction!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Created Simple product programatically’ is closed to new replies.