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
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 TryVary.com
I hope this is useful for you.
Related
I'm trying to set things up so that woocommerce will change a sale product back to the original price when the stock runs out.
I've written this snippet:
if ($product->get_stock_quantity()<1){
$price = $product->regular_price;
}
The problem is I'm not sure where to put it and if it's entirely correct. I can't see price.php in woocommerce > single product.
Use this code in function.php
function return_custom_price($price, $product) {
if ($product->get_stock_quantity()<1){
$price = $product->regular_price;
}
return $price;
}
add_filter('woocommerce_get_price', 'return_custom_price', 10, 2);
I have a new custom field named super_sale_price, and I'm trying to use that value for every product, so if this custom field value is exists,then we will show this in every where for that product,
I used this to display that price,
function return_custom_price($price, $product) {
global $post, $blog_id;
$price = get_post_meta($post->ID, 'super_sale_price', true);
return $price;
}
add_filter('woocommerce_get_price', 'return_custom_price', 10, 2);
This changes value in single product page, but once I add this product to cart, there the price shows 0. Please someone tell me why this happening ? Is this is a wrong hook?
The hook that you are using is deprecated, Instead Try this instead:
add_filter('woocommerce_product_get_price', 'display_super_sale_price', 10, 2);
function display_super_sale_price( $price, $product ) {
if( $product->get_meta('super_sale_price') );
$price = $product->get_meta('super_sale_price');
return $price;
}
Code goes in function.php file of your active child theme (or active theme). It should work.
For variable products and product variations, see this answers threads:
Change product prices via a hook in WooCommerce 3
Conditional product prices cart issue in WooCommerce 3
I am trying to change the price of the product based on location.
For this i am using wc fields factory to create number of fields for locations and updating the price and based on IP i am finding the city(location) and i am fetching the custom field value.
Using
function return_custom_price($price, $product) {
global $post, $blog_id;
$price = get_post_meta($post->ID, '_regular_price');
$post_id = $post->ID;
$price = ($price[0]*2.5);
return $price;
}
add_filter('woocommerce_get_price', 'return_custom_price', 10, 2);
Its working fine, but when i go to view Cart there it showing product price as 0
like this:
Please help me out in this.
Thanks.
Update:
The product meta data '_regular_price' is not a custom field but the product regular price, that you can get directly using WC_Product methods and magic properties directly on the $product object.
If you look to your function you have 2 arguments: $price (the product price) and the $product (the product object)… So you don't need to use any global variables as you got already $product object, that you can use.
Here is the updated code:
add_filter('woocommerce_get_price', 'product_custom_price', 10, 2);
function product_custom_price($price, $product) {
$custom_price = $product->get_regular_price();
return $custom_price * 2.5;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Please see the cart screenshots:
1) The cart without using this code (before):
2) The cart using this the code (after):
As you can see, this code works perfectly and display the regular prices in cart items.
The OP is using this code with a custom field:
add_filter('woocommerce_get_price', 'product_custom_price', 10, 2);
function product_custom_price($price, $product) {
$custom_price = get_post_meta($product->id, 'custom_key', true);
return $custom_price;
}
I am trying to add some additional discounts to the cart total and I tried this code but it's not quite working for me.
function mysite_box_discount( ) {
global $woocommerce;
$total_disc = 10;
// Alter the cart discount total
$woocommerce->cart->discount_total = $total_disc;
}
add_action('woocommerce_calculate_totals', 'mysite_box_discount');
I also tried adding $cart as an argument to the function, but it didn't work.
I also tried $cart->discount_total but it is not working for me either.
Try this code
function custom_wc_add_discount() {
$total_disc = 10;
WC()->cart->add_fee( 'Discount note', -$total_disc );
}
add_action( 'woocommerce_cart_calculate_fees','custom_wc_add_discount' );
I need to change the price of my products in my store, with a 10% discount, if my customer is from some specific place, so, I wrote this code:
add_filter('woocommerce_price_html', 'my_price_edit');
function my_price_edit() {
$product = new WC_Product( get_the_ID() );
$price = $product->price;
echo $price * 0.9;
}
Ok, it works! But when in the checkout the price are normals without the 10% discount!
Does have some hook for change the price of the products in the checkout area or some different code to change correctly in the both (in the product page and checkout)?
New in Woocommerce too.
Your question looks really similiar to this one.
Adding Custom price with woocomerce product price in Cart & Checkout
I think you need to use the woocommerce_cart_item_subtotal hook to change the price in the cart directly (not exactly as a parameter)
I made a slightly modification to the code (changed price formula). I think that may help you.
add_filter( 'woocommerce_get_discounted_price', 'calculate_discounted_price', 10, 2 );
// Display the line total price
add_filter( 'woocommerce_cart_item_subtotal', 'display_discounted_price', 10, 2 );
function calculate_discounted_price( $price, $values ) {
// You have all your data on $values;
$price = $price*.09;
return $price;
}
// wc_price => format the price with your own currency
function display_discounted_price( $values, $item ) {
return wc_price( $item[ 'line_total' ] );
}