TheGoof
TheGoof

Reputation: 25

Woo Commerce hook for updating a price only for the current user. PHP

I hope that someone may know the answer or guide me in the right direction on this.

I have been looking unsuccessfully at how to create dynamic pricing on woo commerce plugin for wordpress. (example shown is a simple form to update the pricing by user input, but will eventually be a price created from an uploaded file) Being Specific to user/session

$getcost= $_POST['cost'];
echo "<form action=\"#\" method=\"POST\"><input type=\"text\" name=\"cost\"><button type=\"submit\">Price me</button>";
global $post, $woocommerce;
    $post_id = $post->ID;
    if($post_id == '34'){
$customPrice= $getcost;

Using the update post meta i can change the price, but this changes it for everyone.

update_post_meta($post_id,'_price', $customPrice);

I am just looking to change so it only for the person using it, perhaps based on the session, or to users basket(shopping cart) Would be great to hear from anyone who has a way round this. Thank you in advance for being you (AKA "Great")

Thanks to the wonderful 'Pelmered' for his help on this. I thought I would add the edited code I have been playing with for others looking.

The Current code works great, but only allows one custom price at the moment, I hope to expand this in the near future to allow for many custom prices within the basket. I will edit this when I have investigated this further

Although the code below is just for practice on a wider project of mine, I hope that this is of benefit to others. Would be great as a Donation page.

(Although I have added comments and code, please feel free anyone to correct me if anything is unclear, incorrect, misleading or if you have any cleaner instruction, all credit will be given)

Apologies to all for my poor formatting, I am generally a messy writer :/

//You all know what a form is, but just incase you want a clearer Idea of what is happening, here it is :)
<form action="#" method="POST">
<input type="text" name="cost">
<button type="submit">Please take my money</button>

//I store my dynamic price in a cookie, as my test price is just coming from a simple
//form that posts to self, but If you don't want this, skip this function&action

      //loads the function
add_action( 'init', 'SetQuoteInfo');             
                                                           
     //gets the posted data(if available) and sets cookie value
function SetQuoteInfo(){                                            
 if(!empty($_POST['cost'])){                                                    
     setcookie("QuoteMe", $_POST['cost']);                                      
 }                                                                              
   }                                                                                                                                                            
                                                                                
    //Price is filtered to be the new values instructed by my_custom_price()                                                                                  
add_filter('woocommerce_get_sale_price', 'my_custom_price', 99);                   
add_filter('woocommerce_get_price', 'my_custom_price', 99);                        
                                            
    //New price is created                            
function my_custom_price( $orginal_price )                                         
             {                                                                  
                     global $post, $woocommerce;
    // Checking the posted data first ensures that price data is not taken from previous cookie data so it is always new/overridden posted data(if posted by user)
                     If(!empty($_POST['cost']))                                 
                    $new_price = $_POST['cost'];                                
else {                                                                             
   $new_price =  $_COOKIE['QuoteMe']; //Gets data from cookie if no new posted value                      
     }                                                                                  
return $new_price;                                                                 
             }     

Upvotes: 0

Views: 4803

Answers (1)

Pelmered
Pelmered

Reputation: 2882

update_post_meta() would update the price permanently for every user. A better way to dynamically update the price globally(for every user) is:

$product = new WC_Product( $id );
$product->set_price( 99 ); //set the price to 99 for the product

But in this case(update for individual user), this is what I think you should do:

add_filter('woocommerce_get_sale_price', 'my_custom_price', 99);
add_filter('woocommerce_get_price', 'my_custom_price', 99);

function my_custom_price( $orginal_price )
{
    global $post, $woocommerce;

    //your logic for calculating the new price here
    $new_price = $orginal_price / 2;

    //Return the new price (this is the price that will be used everywhere in the store)
    return $new_price;
}

But beware of cache when using this solution. That might cause some trouble.

Upvotes: 2

Related Questions