WooCommerce - Adding a custom price to each product in cart
Asked Answered
D

2

4

I would like to update the price of products adding a custom price in cart using this simple piece of code update_post_meta( $product->id, '_regular_price', $frame_price_added);.

Note: what I'm trying to achieve is to add this custom price to each product in cart.

I have try to get $frame_price_added this way:

$frame_price = $res['_number_field'][0];
$frame_price_added = $product->price + $frame_price;

Here $product->price is price coming from woocomerce product and $frame_price is coming from my newly added price.

I was wondering how do I associate this new price to cart, because it doesn't work.

I have tried using update_post_meta( $product->id, '_price', $frame_price_added); and when page is refreshed it adds and stores the custom price to the product, and saved it.

Any idea on how I can achieve this properly?

Thanks.


Edit: One more thing… I have searched a function that can being called on add to cart and i didn't find any thing, and also an action hook being called on woocommerce_template_single_add_to_cart which had woocommerce_single_product_summary but it didn't find any function.

Dampen answered 31/8, 2016 at 18:12 Comment(2)
WooCommerce compares the _regular_price to the sale_price and the sale price's sale dates to determine the _price key. The _price is then the price that users will pay. Best that I can tell, it sounds like you may want to consider Product Addons.Smarm
@Smarm ohh i have been reading all you comments on wordpress form -_- and i read that same as well :p sorry for asking that again but i was searching a way if we update actual price it's rightly served in cart. So what i am thinking is to add a hook and update those prices in that hook after checkout destroy themDampen
L
3

Update: For WooCommerce 3.0+ Change cart item prices in WooCommerce version 3.0

You can use woocommerce_before_calculate_totals hook to customize your cart items prices.

You can define $framed_price variables as global in your function, this way.

This is the code:

// getting your additional price outside the function (making any conditional calculations) 
$framed_price = 20;

add_action( 'woocommerce_before_calculate_totals', 'add_custom_total_price', 10 );
function add_custom_total_price( $cart_object ) {

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    global $framed_price;

    foreach ( $cart_object->get_cart() as $key => $value ) {
        $value['data']->price += $framed_price;
    }
}

Or get your custom price inside the hooked function (optionally, depending on how you get your custom price):

add_action( 'woocommerce_before_calculate_totals', 'add_custom_total_price', 10 );
function add_custom_total_price( $cart_object ) {

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    $framed_price = 20;

    foreach ( $cart_object->get_cart() as $key => $value ) {
        $value['data']->price += $framed_price;
    }
}

This code is tested and working.

Naturally this code goes on function.php file of your active child theme (or theme) or in any plugin file.

Reference: WooCommerce Cart - Dynamic Price variable pass into custom price hook

Lachus answered 31/8, 2016 at 18:29 Comment(2)
how i can get the $framed_price from my form ? i don't want it to be staticHydrogenous
@sona what do you exactly mean/want? what form, what field?Lachus
G
0

Use set_price() method to set it for the new version.

add_action( 'woocommerce_before_calculate_totals', 'add_custom_total_price', 10 );
function add_custom_total_price( $cart_object ) {

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    $framed_price = 20;

    foreach ( $cart_object->get_cart() as $key => $value ) {
        $value['data']->set_price( $framed_price );
    }
}
Ganny answered 31/5, 2024 at 11:10 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.