Customizing product prices make cart items price showing 0 - php

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;
}

Related

Out of stock uses normal price woocommerce

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);

Custom field value to replace product price everywhere in Woocommerce 3

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

Custom Price for a specific product in Woocommerce

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;
}

Change product price with AJAX, Woocommerce Wordpress

I'm working with a custom product where customers can type their custom text that will be added to cart.
Depending on the size of the text, a different price will be set, the logic is done and I can see the new price in the product object.
It seems that I can change the product object with the new price but I get status 500 back from admin-ajax.php when I do $woocommerce->setup_product_data($product_id).
I have found several topics but none of them seems to work in my case.
I'm not able to update the cart with the new price.
Here is my ajax function in functions.php:
// Adjust new price
function applyNewPrice() {
global $woocommerce;
// From JS
$product_id = (int) $_POST['id'];
// From JS
$price = (float) $_POST['price'];
$product_data = get_post($product_id);
// Code returning status 500 here...
$product = $woocommerce->setup_product_data($product_data);
$product->set_price($price);
update_post_meta($product_id,'_price',$price);
update_post_meta($product_id,'_regular_price',$price);
$woocommerce->clear_product_transients( $product_id );
}
add_action('wp_ajax_applyNewPrice', 'applyNewPrice');
add_action('wp_ajax_nopriv_applyNewPrice', 'applyNewPrice');
The function you are trying to call does not exist. This is the function you need.
$product = wc_setup_product_data( $product_id );
No sure where you are coming up with those functions. I could not find this function either?
$woocommerce->clear_product_transients( $product_id );

How to dynamically change the price of a product in Woocommerce - WordPress?

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' ] );
}

Categories