Woocommerce: custom price based on user input

2019-03-15 08:31发布

问题:

I did not want to post here but I could not find the answer I was looking for and I do not have enough reputation to comment on other VERY SIMILAR questions to get my exact answer.

I found an almost perfect answer from this post: WooCommerce: Add product to cart with price override?

using the code:

add_action( 'woocommerce_before_calculate_totals', 'add_custom_price');

function add_custom_price( $cart_object ) {
      $custom_price = 10; // This will be your custome price  
      foreach ( $cart_object->cart_contents as $key => $value ) {
          $value['data']->price = $custom_price;
      }
 }

and it works great...if you set a hard coded custom price.

My question is: How can I set a custom price based on user input?

I have tried every way I can think of to pass information (I even tried using cookies, globals, sessions) and none of them worked how I wanted and all of them were, at BEST, hacks.

The specific product in question is a donation and the customer wants the user to be able to set the donation price (rather than just creating a variable product with set price points).

On the donation page when the user submits the donation form I am adding the donation product to the cart like so:

$donation_success = $woocommerce->cart->add_to_cart($donation_id); 

My donation product has a set price of 0.00 so when it is added to the cart it has a price of 0.00 (I don't know if the price is set at this point or later)

I have tried passing in information at this point using the last variable in the add_to_cart method which accepts an array of arguments but I couldn't seem to get that to work either.

I am sure the answer is simple but I have been trying for hours to do this right and I cannot get it to work. I am out of ideas.

The actual code I am using at the moment is slightly different than was suggested by the answerer of the above post but works basically the same:

add_action( 'woocommerce_before_calculate_totals', 'woo_add_donation');

function woo_add_donation() {
    global $woocommerce;

    $donation = 10;
    foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
        if($cart_item['data']->id == 358 || $cart_item['data']->id == 360){
            $cart_item['data']->set_price($donation);
        }
    }
}

Thanks in advance for any helpful advice!

回答1:

I found a solution which is not elegant but works for my purposes.

I was trying to use cookies before but I didn't set the cookie path so it was only available to the page it was set on.

I am now setting a cookie with the donation price:

setcookie("donation", $_GET['donation'], 0, "/");

Then I am setting the price like so:

add_action( 'woocommerce_before_calculate_totals', 'woo_add_donation');

function woo_add_donation() {
    global $woocommerce;

    foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
        if($cart_item['data']->id == 358 && ! empty($_COOKIE['donation'])){
            $cart_item['data']->set_price($_COOKIE['donation']);
        }
    }
}


回答2:

I have been looking for exactly the same thing. I found this WooCommerce plugin (not free) for this

name your price plugin

Initially I wasn't sure what search terms to use to find plugins like this but it looks like "WooCommerce name your price" brings up links to other sources of similar plugins.



回答3:

[this is a comment] Where do you set the cookie? My first guess is that the refreshes in the same page, using the GET method, and provides a PHP code-block with the $_GET['donation'] to set the cookie with. And then, once the cookie is set, you redirect the page to Woocommerce Cart page to continue the shopping process. If you're doing it easier way, please let me know. Thanks.

Sorry, I couldn't post this as a comment to the selected answer due to the lack of points.