0
votes

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);

}

?>
3

3 Answers

0
votes

Ok I have cobbled together a solution that works for me. It may not be the best however so I'm still interested to see what others can come up with. But posting this here in case it helps anyone.

In the functions.php file..

    function add_multiple_sku($design_space_sample_skus) {

  global $woocommerce;

  //put the skus in an array
  $design_space_sample_sku = explode(";", $design_space_sample_skus);

  //loop through array of skus
  foreach ($design_space_sample_sku as $sample_sku) {

    //convert skus to product ids

      $sample_id = wc_get_product_id_by_sku( $sample_sku );

      //add each product id to the cart

    WC()->cart->add_to_cart( $sample_id );

  }

  //redirect to the cart
  $cart_url = $woocommerce->cart->get_cart_url();
  wp_safe_redirect( $cart_url );

}

Then the function needs calling somewhere and the SKU's pulling in. I have created a file in my theme directory and used the following. SKU's a posted to this file with post data. Again, may not be the best solution.

 $parse_uri = explode( 'wp-content', $_SERVER['SCRIPT_FILENAME'] );
require_once( $parse_uri[0] . 'wp-load.php' );

//add multiple skus

$design_space_sample_id = $_GET["samples"];

add_multiple_sku($design_space_sample_id);
0
votes

This is what solved it for me.

I put this HTML in my page:

<form method="post" action="/">

    <input type="hidden" name="E24MT1260" value="23">
    <input type="hidden" name="ACFIT60060015" value="14">

    <input type="hidden" name="programatic_add_to_cart" value="true">
    <input type="submit" value="Add to cart">

</form>

And added this to my functions.php

<?php

    add_action('wp_loaded', function() {
        global $woocommerce;

        if (!empty($_POST) && !empty($_POST['programatic_add_to_cart'])){ 
            global $woocommerce;

            foreach ($_POST as $sku => $quantity) {

                $product_id = wc_get_product_id_by_sku($sku);
                $woocommerce->cart->add_to_cart($product_id, $quantity);
            }

            wp_redirect( '/cart' );
            exit;
        }

    });

?>
-1
votes

I'm looking for code that will do the same thing -- Allow me to add multiple products to the cart using the SKU -- So the user can input a SKU into a text field (let's say there are three text inputs, they can add a SKU into each) when they submit, it adds those products to the cart.... Is this what yours does? Would you mind providing your full code? Thank yoU!