• Resolved muzaffarmahmood

    (@muzaffarmahmood)


    Hi,

    I am trying to add product via code in my plugin. In which i am getting data from some external server via rest call and add products like this.

    $objProduct = new WC_Product();
    $objProduct->set_name($product->ProductName);
    $objProduct->set_status(“publish”); // can be publish,draft or any wordpress post status
    $objProduct->set_catalog_visibility(‘visible’); // add the product visibility status
    $objProduct->set_description($product->Description);
    $objProduct->set_sku($product->ProductNumber);
    $objProduct->set_price(55); // set product price
    $objProduct->set_regular_price(77); // set product regular price
    $objProduct->set_manage_stock(true); // true or false
    $objProduct->set_stock_quantity($totalInStock);
    if($totalInStock > 0)
    $objProduct->set_stock_status(‘instock’); // in stock or out of stock value
    else
    $objProduct->set_stock_status(‘outstock’); // in stock or out of stock value
    $objProduct->set_backorders(‘no’);
    $objProduct->set_reviews_allowed(true);
    $objProduct->set_sold_individually(false);
    $objProduct->set_category_ids($cat_ids);
    $productImagesIDs = array(); // define an array to store the media ids.
    echo ‘Product_id:’;
    echo $product_id = $objProduct->save();

    First time it did add a product and return product_id 80. But never again it could add product. Now it always return 0 as product_id and no product add.

    I am following this: https://www.sbloggers.com/add-a-woocommerce-product-using-custom-php-code-programmatically

Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Problem on adding product via code.’ is closed to new replies.