WooCommerce add to cart products with customized price

Pramod picture Pramod · May 30, 2014 · Viewed 7.5k times · Source

I have added number of products using add_to_cart($product_id) function through a template using following code in WooCommerce Wordpress.

global $woocommerce;
$id_arr = $_POST['up_product_chk'];
$i = 0;
for($i; $i<=count($id_arr); $i++){
   $ids = $id_arr[$i];
   $woocommerce->cart->add_to_cart($ids);  
}
wp_redirect(site_url().'/cart/');

Now I wish to add custom price for each product through this template. As now price in cart is same as in database but I want to add my custom price through this point. Can someone help me to do the same. Thanks

Answer

Renish Khunt picture Renish Khunt · May 30, 2014

using this hook you can set your custom price. write this code in functions.php file.

add_filter('woocommerce_get_price','change_price', 10, 2);
add_filter('woocommerce_get_regular_price','change_price', 10, 2);
add_filter('woocommerce_get_sale_price','change_price', 10, 2);


function change_price($price, $productd){
     if($productd->id == '1'){
        $price = "150";
     }
     return $price;
}

This way you can set a custom price in woocommerce without effecting the database.

fore more detail please read this article.

I hope this is useful for you.