简体   繁体   English

向Woocommerce购物篮脚本添加多个产品

[英]Adding multiple products to Woocommerce basket script

I have a little HTML product selector that passes Woocommerce sku's via post data in the url. 我有一个小的HTML产品选择器,它通过url中的发布数据传递Woocommerce sku。 I want to use these sku's to add the matching products to woocommerce shopping cart. 我想使用这些sku将匹配的产品添加到woocommerce购物车中。

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". 我已经写了一个PHP脚本,它位于我的Wordpress主题目录中,我认为该目录几乎已经存在,但是可以帮助我解决为什么出现“ 500服务器错误”的问题。 If I remove the add_to_cart function and echo out the sku's instead that works so I see the problem is there. 如果我删除了add_to_cart函数并回显了sku,那可以正常工作,所以我发现问题出在这里。 My thought is because I am using the add_to_cart function outside of the loop? 我的想法是因为我在循环之外使用add_to_cart函数? 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);

}

?>

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.. 在functions.php文件中。

    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。我在主题目录中创建了一个文件,并使用了以下文件。 SKU's a posted to this file with post data. SKU已过帐到具有过帐数据的文件。 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);

This is what solved it for me. 这就是为我解决的问题。

I put this HTML in my page: 我将此HTML放在页面中:

<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 并将其添加到我的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;
        }

    });

?>

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? 我正在寻找可以做相同事情的代码-允许我使用SKU将多个产品添加到购物车中-因此用户可以在文本字段中输入SKU(假设有三个文本输入,他们可以在他们提交商品时,在每个商品中添加一个SKU,然后将这些商品添加到购物车中。 Would you mind providing your full code? 您愿意提供完整的代码吗? Thank yoU! 谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM