I have a little HTML product selector that passes Woocommerce sku's via post data in the url. I want to use these sku's to add the matching products to woocommerce shopping cart.
I have written a php script that is sitting in my Wordpress theme directory that I think is almost there but could do with a little help as to why I am getting "500 server error". If I remove the add_to_cart function and echo out the sku's instead that works so I see the problem is there. My thought is because I am using the add_to_cart function outside of the loop? But I am not sure. Below is what I have so far if anyone could help out it would be much appreciated.
<?php
//get skus
$design_sample_ids = $_GET["samples"];
//put skus in array
$design_sample_id = explode(";", $design_sample_ids);
//loop through skus
foreach ($design_sample_id as $sample_id) {
//add each sku to cart
WC()->cart->add_to_cart($sample_id);
}
?>