php - Adding multiple products to Woocommerce basket script -
i have little html product selector passes woocommerce sku's via post data in url. want use these sku's add matching products woocommerce shopping cart.
i have written php script sitting in wordpress theme directory think there little why getting "500 server error". if remove add_to_cart function , echo out sku's instead works see problem there. thought because using add_to_cart function outside of loop? not sure. below have far if out 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 $sample_id) { //add each sku cart wc()->cart->add_to_cart($sample_id); } ?>
ok have cobbled solution works me. may not best i'm still interested see others can come with. posting here in case helps anyone.
in functions.php file..
function add_multiple_sku($design_space_sample_skus) { global $woocommerce; //put skus in array $design_space_sample_sku = explode(";", $design_space_sample_skus); //loop through array of skus foreach ($design_space_sample_sku $sample_sku) { //convert skus product ids $sample_id = wc_get_product_id_by_sku( $sample_sku ); //add each product id cart wc()->cart->add_to_cart( $sample_id ); } //redirect cart $cart_url = $woocommerce->cart->get_cart_url(); wp_safe_redirect( $cart_url ); }
then function needs calling somewhere , sku's pulling in. have created file in theme directory , used following. sku's posted file post data. again, may not 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);
Comments
Post a Comment