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;
}
Related
I want to change the default quantity from the products, from 1 to 0,1 but I can't seem to figure it out.
I tried the following:
function custom_quantity_input_args($args, $product) {
$args['input_value'] = 0.10;
$args['min_value'] = 0.10;
$args['step'] = 0.10;
$args['pattern'] = '[0-9.]*';
$args['inputmode'] = 'numeric';
return $args;
}
The problem with this is that modifies the quantity input from cart as well, which isn't what I want.
To be more specific I want the following:
when I go to the product page I want to show 0,1;
when I go to the cart page I want to show the current quantity;
The solution I mention above shows 0,1 both in product page and in cart page.
I found another solution, but it shows the current quantity both in product and in cart which, again, it's not what I want.
Any ideas?
Based on Decimal quantity step for specific product categories in WooCommerce answer code, try the following revisited code:
// Defined quantity arguments
add_filter( 'woocommerce_quantity_input_args', 'custom_quantity_input_args', 9000, 2 );
function custom_quantity_input_args( $args, $product ) {
if( ! is_cart() ) {
$args['input_value'] = 0.1; // Starting value
}
$args['min_value'] = 0.1; // Minimum value
$args['step'] = 0.1; // Quantity steps
return $args;
}
// For Ajax add to cart button (define the min value)
add_filter( 'woocommerce_loop_add_to_cart_args', 'custom_loop_add_to_cart_quantity_arg', 10, 2 );
function custom_loop_add_to_cart_quantity_arg( $args, $product ) {
$args['quantity'] = 0.1; // Min value
return $args;
}
// For product variations (define the min value)
add_filter( 'woocommerce_available_variation', 'filter_wc_available_variation_price_html', 10, 3);
function filter_wc_available_variation_price_html( $data, $product, $variation ) {
$data['min_qty'] = 0.1;
return $data;
}
// Enable decimal quantities (in frontend and backend)
remove_filter('woocommerce_stock_amount', 'intval');
add_filter('woocommerce_stock_amount', 'floatval');
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
I'm trying to add a default product shipping weight to WooCommerce in my functions.php file but is not working, I'm not sure if I'm using the right snippets or missing parts of the snippet.
Here's that I'm using,
add_filter('woocommerce_product_get_weight', 'woocommerce_product_get_weight_filter');
function woocommerce_product_get_weight_filter($weight) {
$default_weight = 0.1;
if ((!is_numeric($weight)) || ($weight <= 0.001)) {
return $default_weight;
}
return $weight;
}
Try with empty() and the hook for product variations :
add_filter('woocommerce_product_variation_get_weight', 'woocommerce_product_get_weight_filter', 10, 2 );
add_filter('woocommerce_product_get_weight', 'woocommerce_product_get_weight_filter', 10, 2 );
function woocommerce_product_get_weight_filter( $weight, $product ) {
if ( empty($weight) || $weight <= 0.001 ) {
return 0.1;
}
return $weight;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
This will not work in admin product edit pages in the weight setting field. The only available hooks are working for "view" context and not in "edit" context (as in backend)…
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 need to double the price for every product in Woocommerce frontend. For this I used the following code:
add_filter( 'woocommerce_product_get_price', 'double_price', 10, 2 );
function double_price( $price, $product ){
return $price*2;
}
But there is an error using this code. checkout page price is not correct.
for example the product orginal price is 10. We double the price by this code . so the product price is 20 now. When i added this product to the cart then cart and checkout page price is 40. That means this multiplication is take place in two times.
Please help to solve this.
Updated To double the price:
1) First you will restrict your code to single product and archives pages only:
add_filter( 'woocommerce_product_get_price', 'double_price', 10, 2 );
function double_price( $price, $product ){
if( is_shop() || is_product_category() || is_product_tag() || is_product() )
return $price*2;
return $price;
}
2) Then for cart and checkout pages, you will change the cart item price this way:
add_filter( 'woocommerce_add_cart_item', 'set_custom_cart_item_prices', 20, 2 );
function set_custom_cart_item_prices( $cart_data, $cart_item_key ) {
// Price calculation
$new_price = $cart_data['data']->get_price() * 2;
// Set and register the new calculated price
$cart_data['data']->set_price( $new_price );
$cart_data['new_price'] = $new_price;
return $cart_data;
}
add_filter( 'woocommerce_get_cart_item_from_session', 'set_custom_cart_item_prices_from_session', 20, 3 );
function set_custom_cart_item_prices_from_session( $session_data, $values, $key ) {
if ( ! isset( $session_data['new_price'] ) || empty ( $session_data['new_price'] ) )
return $session_data;
// Get the new calculated price and update cart session item price
$session_data['data']->set_price( $session_data['new_price'] );
return $session_data;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Tested and working. It will change all prices as you expect in cart, checkout and order pages…
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;
}