WooCommerce add to cart
New here? Learn about Bountify and follow @bountify to get notified of new bounties! x

Hi, I would like to send a product to the cart using WooCommerce. I have this half working :)

At the moment my code is adding a product but it's not adding the price or the options.

In the pic below (link) my code adds the bottom product but I want it to add like the top product.

I'm using the product ID so I'm not sure why the price is not showing. I need to be able to add the price.

The below is not needed and I'm not sure if it's possible but...

I will give a tip to anybody who can get this to work without using the ID.

My code:


global $woocommerce;

$myID = $_REQUEST['myID'];

$product_id = $myID; 
$quantity = 1; 
$variation_id = null; 
$variation = array(
    'Seating Panels:' => 'Black Alligator (baby)',
    'Side Panels:'  => 'Asphalt',
    'Welts:'  => 'Backrest to match seat',
    'Stitch Color:'  => 'Backrest to match seat',
$cart_item_data = array(
    'Seating Panels:' => 'Black Alligator (baby)',
    'Side Panels:'  => 'Asphalt',
    'Welts:'  => 'Backrest to match seat',
    'Stitch Color:'  => 'Backrest to match seat',

WC()->cart->add_to_cart( $product_id,$quantity,$variation_id,$variation,$cart_item_data ); //  ID 12623



<!DOCTYPE html>

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>




    <div class="container" style="margin-top: 100px;">
        <div class="row">
            <div class="col s12">
                <a id="cart" class="waves-effect waves-light btn">Send to cart</a>



        // Get parameters from URL
        let searchParams = new URLSearchParams(window.location.search)
        var myID = searchParams.get('id');

            type: "POST",
            url: "cart.php?myID="+myID,
            success: function(data) {
            error: function(jqxhr) {
                console.log("error" + jqxhr.responseText);


Hi, if what I'm doing is not possible please let me know. Also if this can be done in JS then I will be okay with that option. If there is anything else please leave a comment and I will reply. I just don't know if what I'm asking is very easy or more difficult. Thanks, Dave.

Hello David. Here we don't have the whole app (context), but looking at your JS code, it could be possible that you are passing an empty ID so WooCommerce defaults to a certain product. Since in your case link does not point anywhere, should be used instead and could resolve your issue.
VladimirMikulic 1 year ago
Thanks Vladimir! Can you add this as a solution as I have it working now.
Jacknumpty 1 year ago
I would like to leave this for charity :)
VladimirMikulic 1 year ago
Hey, This can easily be achieved in python and if you want only php/js then you can convert it into your required format through a script i have and frequently use to convert my scripts from one language to another(supported :- php,python,NodeJS,Julia,Html,css,perl,curl(REST APIs) etc.) Let me know if you need this, will provide for 5$ np.
Hot_Mogambo 1 year ago
Thanks Hot_Mogambo, I have this working now, I appreciate your comment. Curious that you have a solution in Python when I thought WooCommerce was PHP based.
Jacknumpty 1 year ago

Crowdsource coding tasks.

0 Solutions