I want to change per product price in WooCommerce. I am looking for some hook to do it. Actually i am looking to discount product 10%. I want to do it pragmatically.
function woo_my_custom_message($price_html) {
$price = trim($price_html);
$price = (int) $price;
return ($price*10)/100;
}
add_filter( 'woocommerce_cart_item_price', 'woo_my_custom_poa_message' );
The filter you're looking for is woocommerce_get_price. So your code will be something like:
function my_custom_price($price, $product) {{
return $price * 0.9;
}
add_filter( 'woocommerce_get_price', 'my_custom_price', 10, 2);
Related
I want to add text before the price on the WooCommerce product catalogue only, which I have working using this code in functions.php:
// Add text before price
function bd_rrp_price_html( $price, $product ) {
$return_string = 'Rent from: ' . $price;
return $return_string;
}
add_filter( 'woocommerce_get_price_html', 'bd_rrp_price_html', 100, 2 );`
However, the above function is also adding the text before the price on the product detail page, which I do not want...
What would I need to change in the function to get it so it only displays on the product catalogue?
Thanks.
You have just to inverted the price and the text:
add_filter( 'woocommerce_get_price_html', 'cw_change_product_price_display' );
add_filter( 'woocommerce_cart_item_price', 'cw_change_product_price_display' );
function cw_change_product_price_display( $price ) {
// Your additional text in a translatable string
$text = __('TEXT');
// returning the text before the price
return $text . ' ' . $price;
}
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);
In Woocommerce, I would like to apply custom price for a specific product.
This is my actual code:
add_action('woocommerce_get_price','change_price_regular_member', 10, 2);
function change_price_regular_member($price, $productd){
return '1000';
}
Any help on this is appreciated.
The hook woocommerce_get_price is outdated and deprecated in Woocommerce 3 (and it was a filter hook but NOT an action hook). It has been replaced by woocommerce_product_get_price.
So instead try this (where you will define your targeted Product ID in this hooked function):
add_filter( 'woocommerce_product_get_price','change_price_regular_member', 10, 2 );
function change_price_regular_member( $price, $product ){
// HERE below define your product ID
$targeted_product_id = 37;
if( $product->get_id() == $targeted_product_id )
$price = '1000';
return $price;
}
This code goes on function.php file of your active child theme (or theme). Tested and works.
Try this code. just replace id of product 3030
add_action('woocommerce_get_price','change_price_regular_member', 10, 2);
function change_price_regular_member($price, $productd){
if($productd->id==3030){
$price= 1000;
}
return $price;
}
I have a Woocommerce store set up with various product categories.
I want to apply a 20% discount to all products, which belong to the product category Cuckoo
For now all I'm trying to achieve is set a sale price in my functions.php
It tried as follows:
/*
* For a specific date, 20% off all products with product category as cuckoo clock.
*/
function cuckoo_minus_twenty($sale_price, $product) {
$sale_price = $product->get_price() * 0.8;
return $sale_price;
};
// add the action
add_filter( 'woocommerce_get_sale_price', 'cuckoo_minus_twenty', 10, 2 );
If I var_dump the result of $sale_price after the calculation I get the correct answer, however the price display on the front-end strikes out the regular price and displays the sale price as the regular price.
Is there a hook/filter I can use to achieve this?
I've also tried setting the sale price by doing:
$product->set_sale_price($sale_price);
to no avail.
The hook woocommerce_get_sale_price is deprecated since WooCommerce 3 and replaced by woocommerce_product_get_sale_price.
Also Product displayed prices are cached. When sale price is active, regular price is also active.
Try this instead:
// Generating dynamically the product "regular price"
add_filter( 'woocommerce_product_get_regular_price', 'custom_dynamic_regular_price', 10, 2 );
add_filter( 'woocommerce_product_variation_get_regular_price', 'custom_dynamic_regular_price', 10, 2 );
function custom_dynamic_regular_price( $regular_price, $product ) {
if( empty($regular_price) || $regular_price == 0 )
return $product->get_price();
else
return $regular_price;
}
// Generating dynamically the product "sale price"
add_filter( 'woocommerce_product_get_sale_price', 'custom_dynamic_sale_price', 10, 2 );
add_filter( 'woocommerce_product_variation_get_sale_price', 'custom_dynamic_sale_price', 10, 2 );
function custom_dynamic_sale_price( $sale_price, $product ) {
$rate = 0.8;
if( empty($sale_price) || $sale_price == 0 )
return $product->get_regular_price() * $rate;
else
return $sale_price;
};
// Displayed formatted regular price + sale price
add_filter( 'woocommerce_get_price_html', 'custom_dynamic_sale_price_html', 20, 2 );
function custom_dynamic_sale_price_html( $price_html, $product ) {
if( $product->is_type('variable') ) return $price_html;
$price_html = wc_format_sale_price( wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ), wc_get_price_to_display( $product, array( 'price' => $product->get_sale_price() ) ) ) . $product->get_price_suffix();
return $price_html;
}
Code goes in function.php file of your active child theme (active theme).
Tested and works on single product, shop, product category and tag archive pages.
The continuation in:
Wrong Woocommerce cart item price after setting programmatically product sale price
I realiced, you more or less need all of the following filters to make the HTML work out of the box.
add_filter( 'woocommerce_product_get_price', 'custom_dynamic_sale_price', 10, 2 );
add_filter( 'woocommerce_product_variation_get_price', 'custom_dynamic_sale_price', 10, 2 );
add_filter( 'woocommerce_product_get_sale_price', 'custom_dynamic_sale_price', 10, 2 );
add_filter( 'woocommerce_product_variation_get_sale_price', 'custom_dynamic_sale_price', 10, 2 );
add_filter( 'woocommerce_variation_prices_price', 'custom_dynamic_sale_price', 10, 2 );
add_filter( 'woocommerce_variation_prices_sale_price', 'custom_dynamic_sale_price', 10, 2 );
And you need to make sure you've changed the woocommerce_get_variation_prices_hash becasue of the stored transients if you want to display it correctly for variable products.
You may find the gist i've created for a client useful
https://gist.github.com/xandl/743fb6af60827eb95ad42b20b478b020
use woocomerce_get_sale_price filter.
add_filter('woocommerce_get_sale_price', 'my_custom_price', 99, 2);
add_filter('woocommerce_get_price', 'my_custom_price', 99, 2);
function my_custom_price( $price, $product )
{
//your logic for calculating the new price here
$price = $product->get_regular_price() * 0.8;
//Return the new price (this is the price that will be used everywhere in the store)
return $price;
}
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' ] );
}