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.
https://www.dropbox.com/s/bliyqajeaggpzy1/cart.gif?dl=0

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:
https://www.dropbox.com/s/ojy3nupdiwj1fnl/cart.php?dl=0
https://www.dropbox.com/s/4mdanbmdqz7m1x0/index.php?dl=0

PHP

<?php
require('../../wp-load.php');
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

echo($myID);
?>

HTML JS

<!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>

</head>

<body>

<main>

    <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>
            </div>
        </div>
    </div>
</main>

</body>

<script>

    $("#cart").click(function(){
        // Get parameters from URL
        let searchParams = new URLSearchParams(window.location.search)
        var myID = searchParams.get('id');

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

</script>

UPDATE
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 month ago
Thanks Vladimir! Can you add this as a solution as I have it working now.
Jacknumpty 1 month ago
I would like to leave this for charity :)
VladimirMikulic 1 month 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 month 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 month ago

Crowdsource coding tasks.

0 Solutions