1

I want to add a product to a cart problematically without using the admin panel. Add to cart and then checkout using php code. So how can i achieve it and where do I place the code?

I am writing a php script in plugin file that accepting the post data from plugin. my code is

    <?php 
    var_dump($_POST);

    $post_id = wp_insert_post( array(
    'post_title' => 'Great new product',
    'post_content' => 'Here is content of the post, so this is our great new products 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', '' );
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', '' );

?>

I am using wordpress 4.9 and woocommerce plugin. Please help me to solve this i am new to wordpress and searched a lot for this but nothing is worked for me. Where i can write this code or what i am missing?

  • Do you want to create a product, or add one to the cart? Your question and title are different questions. – Jacob Peattie Dec 26 '17 at 2:43
  • Actually first i want to create product with dynamic data and add same product to the empty cart. So i can go to checkout and purchase that product. Is it possible? – Nitin Pawar Dec 26 '17 at 5:31
1

This will give you a base to start:

function generate_simple_product() {
    $name              = 'My Product Name';
    $will_manage_stock = true;
    $is_virtual        = false;
    $price             = 1000.00;
    $is_on_sale        = true;
    $sale_price        = 999.00;
    $product           = new \WC_Product();
    $image_id = 0; // Attachment ID
    $gallery  = self::maybe_get_gallery_image_ids();
    $product->set_props( array(
        'name'               => $name,
        'featured'           => false,
        'catalog_visibility' => 'visible',
        'description'        => 'My awesome product description',
        'short_description'  => 'My short description',
        'sku'                => sanitize_title( $name ) . '-' . rand(0, 100), // Just an example
        'regular_price'      => $price,
        'sale_price'         => $sale_price,
        'date_on_sale_from'  => '',
        'date_on_sale_to'    => '',
        'total_sales'        => 0,
        'tax_status'         => 'taxable',
        'tax_class'          => '',
        'manage_stock'       => $will_manage_stock,
        'stock_quantity'     => $will_manage_stock ? 100 : null, // Stock quantity or null
        'stock_status'       => 'instock',
        'backorders'         => 'no',
        'sold_individually'  => true,
        'weight'             => $is_virtual ? '' : 15,
        'length'             => $is_virtual ? '' : 15,
        'width'              => $is_virtual ? '' : 15,
        'height'             => $is_virtual ? '' : 15,
        'upsell_ids'         => '',
        'cross_sell_ids'     => '',
        'parent_id'          => 0,
        'reviews_allowed'    => true,
        'purchase_note'      => '',
        'menu_order'         => 10,
        'virtual'            => $is_virtual,
        'downloadable'       => false,
        'category_ids'       => '',
        'tag_ids'            => '',
        'shipping_class_id'  => 0,
        'image_id'           => $image_id,
        'gallery_image_ids'  => $gallery,
    ) );

    $product->save();

    return $product;
}

The code above is taken from WooCommerce Smooth Generator, made by WooCommerce itself. It's mostly used for testing.

https://github.com/woocommerce/wc-smooth-generator

Example:

// Generate WC_Product object and save it to database
// 70% change generated product is simple
// 30% chance generated product is variable
$product = \WC\SmoothGenerator\Generator\Product::generate();

// Returns WC_Product object of Simple product and don't save it  to database
$product = \WC\SmoothGenerator\Generator\Product::generate_simple_product();

// Returns WC_Product object of Variable Product and saves it to database
$variable_product = \WC\SmoothGenerator\Generator\Product::generate_variable_product();

Src: https://github.com/woocommerce/wc-smooth-generator/blob/master/includes/Generator/Product.php

If you want to create products programatically, you can edit the Product generator class above with your needs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.