Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Woocommerce: custom price based on user input

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!

like image 516
Bullyen Avatar asked Feb 13 '14 00:02

Bullyen


People also ask

How to add custom pricing options to WooCommerce products?

WooCommerce custom user price plugin allows you to restrict order quantity on product and cart page based on customers price. You can also hide general and sale price from product and shop page. Add custom text to the field titles of your new pricing options. Set redirects to control where customers go after adding a product to their cart.

What is role based pricing in WooCommerce?

Add user roles and customer specific pricing. Add a fixed price, markup or discount existing price in percentage or fixed amount. WooCommerce Role Based Pricing extension empowers you to set your product prices based on user roles and individual customers. You can discount or markup prices by fixed or percentage amount.

How to install WooCommerce plugins in WordPress?

At the WordPress admin panel, go to the Plugins section, and click ‘ Add New ’ to upload and install the plugin you just downloaded from WooCommerce. Upload the .zip file to proceed with the installation.

How do I get my money back from WooCommerce?

Take them directly to the cart or checkout page, or back to the product page to keep shopping. We're so confident in our WooCommerce offering that we offer a 30 day money back guarantee if you are unhappy with our products or service.


1 Answers

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']);
        }
    }
}
like image 147
Bullyen Avatar answered Sep 16 '22 16:09

Bullyen