Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Method for changing price from extension #1231

Closed
helgatheviking opened this issue Jul 9, 2012 · 15 comments
Closed

Method for changing price from extension #1231

helgatheviking opened this issue Jul 9, 2012 · 15 comments
Milestone

Comments

@helgatheviking
Copy link
Contributor

I started working on an add-on that will let the user choose the price... (more like a donation really), but I've run into a wall with the cart and cart processing, b/c there isn't a way to adjust the price for the $product object. For the initial load I can use the woocommerce_get_price filter, but there's no place to hook into the woocommerce_update_cart_action() function and pass my updated price into the cart item.

apparently diigo won't let me embed, but here's a screen shot of what I'm doing

http://www.diigo.com/item/t/3250429_127605958_7444841

a couple of solutions that i need to investigate when i am fresher:
*a method to update the $price of an product object
*putting $price in the cart object

i've found adjust_price() and it is close, but its parameter is how much you'd like to change the price by, and not how much you' d like the final price to be. so this function has some potential, maybe a second parameter, or a similar function, but I also can't figure out how to access the correct $product object from my child class.

sidenote, should all the variables being declared at the beginning of WC_Product, not have var in front of them? i was under the impression they should be static?

@mikejolley
Copy link
Member

Hi,

I'll add set_price just to make it clearer but in reality you can change ->price directly - the var is not private.

@helgatheviking
Copy link
Contributor Author

nice. i have to come back to this at some point... i thought i was pretty close, but got stuck and then distracted by other more pressing jobs.

@joeblackwaslike
Copy link

@helgatheviking email me at joe@spektrumtheory.com when you finish that extension, it's a feature that's been asked for by a client but im too busy as well to really work on something. We'll be your first customer :D

@evandavey
Copy link

I am using set_price() to dynamically set a product's price but how do I then commit this change to the database? Is there a product save method? I've had a look at the product admin code and don't really want to have to rewrite the individual meta data update logic.

@mikejolley
Copy link
Member

set_price is for dynamic pricing, its not for storing prices in the DB

@evandavey
Copy link

Ok - so there is no save product method?

Specifically, I am trying to manage products via xml-rpc. I can create a new product object but need to fire the save logic as the product is missing from the shop listing etc. until I go in and manually click save from the admin.

@mikejolley
Copy link
Member

Use wp_insert_post and wp_update_post, and then update_post_meta for meta data.

@evandavey
Copy link

That's what I've tried - updating _price and _regular_price (the post gets created using the regular xml-rpc calls). The price gets changed but it won't show in the shop loop. Are there other meta fields I need to set - at the point this is called it is a new post of type product with only content and excerpt set?

function cd_add_price_to_product( $args ) {
    global $woocommerce;
    global $wp_xmlrpc_server;

    $wp_xmlrpc_server->escape( $args );

    $blog_id  = $args[0];
    $username = $args[1];
    $password = $args[2];
    $product_id = (int) $args[3];
    $price = (float) $args[4];

    if ( ! $user = $wp_xmlrpc_server->login( $username, $password ) )
           return $wp_xmlrpc_server->error;


    $product_data=get_post( $product_id );
    $product = $woocommerce->setup_product_data( $product_data );

    $product->set_price($price);

    /* must be a better way? */
    update_post_meta($product->id,'_price',$price);
    update_post_meta($product->id,'_regular_price',$price);

    return True;
}

@mikejolley
Copy link
Member

Might be cache. Use $woocommerce->clear_product_transients( $product_id );

@evandavey
Copy link

That seems to have done the trick, thanks!

I'm not too familiar with the wordpress design pattern but is there a specific reason this isn't implemented as a save function on the object itself? Seems to me this would be a better approach.

@avenarie
Copy link

hello, can anyone please explain me the difference between the meta keys:
_sale_price (well, this is obvious)
_price
_regular_price (whats the difference to price ????)

especially i dont know why regular price and price?
thanks in advance

@mikejolley
Copy link
Member

@avenarie _price is set to either the _sale_price or _regular_price depending on which is set and lower. Its used for sorting etc (not all products have a sale price).

@avenarie
Copy link

thanks a lot @mikejolley , this makes it really clearer!
so _price is basically a placeholder for the lowest price (sales price or regular price)
👍

@jamieatreyu1
Copy link

Was this add-on ever completed? I'd love to have it.

Jamie

@helgatheviking
Copy link
Contributor Author

helgatheviking commented Mar 12, 2013

@jamieatreyu1 I did finish this extension. It lets the customer input any price she is willing to pay for a given product (currently only for single and subscription products) It is for sale at: http://www.woocommerce.com/products/name-your-price/ Hope it helps you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants