I'm trying to append text to WooCommerce product title in the order meta - if products has a specific tag. I'm working from
"Append text to product title if product has product-tag on cart in WooCommerce"
"Display custom payment field in Woocommerce Admin, Orders and emails"
This is what I have so fare:
add_filter( 'woocommerce_get_order_item_totals', 'add_udstilling_below_cart_item_name', 10, 3 );
function add_udstilling_below_cart_item_name( $total_rows, $order, $tax_display ) {;
$new_total_rows = [];
foreach($total_rows as $key => $total ){
$new_total_rows[$key] = $total;
}
}
return $new_total_rows;
}
Based on your previous question and this new question, I assume that you would like to see the product title adjusted 'everywhere'.
IMPORTANT!! You should definitely read the following post to
understand that this is a general adjustment, so that you ultimately
no longer need the code from your previous post / question
Changing WooCommerce cart item names
In other words, you can start using the code below to see the adjustment in the following places
Order-receive (Thank you) page,
email notifications
My Account Orders> Single order details
Cart & Checkout and Backend Order edit pages.
Now you have change the names everywhere except on Shop archives and product pages…
function add_udstilling_order_item_name( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
// Loop through cart items
foreach ( $cart->get_cart() as $cart_item ) {
// Get an instance of the WC_Product object
$product = $cart_item['data'];
// Get product id
$product_id = $cart_item['product_id'];
if( method_exists( $product, 'set_name' ) && has_term( 'udstillingsmodel', 'product_tag', $product_id ) ) {
$product->set_name( $product->get_name() . '(test)' );
}
}
}
add_action( 'woocommerce_before_calculate_totals', 'add_udstilling_order_item_name', 10, 1 );
Related
I am developing a wordpress and woocommerce-based website where information on cooking-related training is provided and various kitchen materials are sold.
Those who want to participate in the trainings apply by filling out a form. Kitchen supplies are also sold through woocommerce.
Trainings are added to the website with a type of content called training.
Some trainings are requested to be sold over the woocommerce structure. However, these "Trainings" that want to be sold are wanted to remain in the form of educational content. In addition, it is requested not to be added or moved as a product.
First of all, I created a virtual product called Education. I hid the product in the store.
Then I added a custom field for Tutorials called price. The price of each training to be sold will be entered here.
I have a button "Register for Training" on the training detail page, I changed it to "Buy" for the trainings wanted to sell and the link
?add-to-cart=340&custom_price=600&quantity=1
I gave in the form.
Here 340 is the id of the virtual product I created.
When the Buy button is clicked, the virtual product called Education is added to the basket. But I want to update the name and price of this training according to which training detail page is printed.
The codes I added to functions.php.
add_action( 'woocommerce_before_calculate_totals', 'before_calculate_totals' );
function before_calculate_totals( $_cart ){
// loop through the cart_contents
foreach ( $_cart->cart_contents as $cart_item_key => &$item ) {
// you will need to determine the product id you want to modify, only when the "donation_amount" is passed
if ( $item['product_id'] == 340 && isset( $_GET['custom_price'] ) ){
// custom price from POST
$custom_price = $_GET['custom_price'] > 0 ? $_GET['custom_price'] : 0;
// save to the cart data
//$item['data']->price = $custom_price;
// new versions of WooCommerce may require (instead of line above)...
$item['data']->set_price($custom_price);
}
}
}
function ipe_product_custom_price( $cart_item_data, $product_id ) {
if( isset( $_POST['custom_price'] ) && !empty($_POST['custom_price'])) {
$cart_item_data[ "custom_price" ] = $_POST['custom_price'];
}
return $cart_item_data;
}
add_filter( 'woocommerce_add_cart_item_data', 'ipe_product_custom_price', 99, 2 );
I wanted to update the price with these codes, but it didn't work.
How do I dynamically update the information of the virtual product? Or what different method would you suggest?
There are some mistakes in your code and some missing things to make it work. Try the following:
// Set custom data as custom cart data in the cart item
add_filter( 'woocommerce_add_cart_item_data', 'add_custom_price_as_custom_cart_item_data', 30, 3 );
function add_custom_price_as_custom_cart_item_data( $cart_item_data, $product_id, $variation_id ) {
if( ! isset($_GET['custom_price']) ) {
$cart_item_data['custom_price'] = (float) esc_attr( $_GET['custom_price'] );
$cart_item_data['unique_key'] = md5( microtime().rand() ); // Make each item unique
}
return $cart_item_data;
}
// Change cart item price
add_action( 'woocommerce_before_calculate_totals', 'change_cart_item_price' );
function change_cart_item_price( $cart ){
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// loop through the cart_contents
foreach ( $cart->get_cart() as $cart_item ) {
// you will need to determine the product id you want to modify, only when the "donation_amount" is passed
if ( isset($cart_item['custom_price']) ) {.
$item['data']->set_price($cart_item['custom_price']);
}
}
}
// Display the custom price on minicart too
add_filter( 'woocommerce_cart_item_price', 'change_minicart_item_price', 10, 3 );
function change_minicart_item_price( $price_html, $cart_item, $cart_item_key ) {
if( ! is_cart() && isset( $cart_item['custom_price'] ) ) {
$args = array( 'price' => floatval($cart_item['custom_price']) );
if ( WC()->cart->display_prices_including_tax() ) {
$product_price = wc_get_price_including_tax( $cart_item['data'], $args );
} else {
$product_price = wc_get_price_excluding_tax( $cart_item['data'], $args );
}
return wc_price( $product_price );
}
return $price_html;
}
Code goes in functions.php file of your active child theme (or active theme). It should works.
I'm building a woocommerce shop to sell antique maps, both originals and digital prints. The maps therefore are set as two variations of the same product: "Original" and "Print". "Original" is just the original antique map but "Print" has a load of options such as framing, mounting and print size and these are set by the user in the single-product page using jquery.
I've succeeded in allowing users to choose from a series of options in the single product page and thereby dynamically generate a price for the product they're buying based on what size, quantity, framing and mounting options they select.
This is done using jquery and the options (including the price) are passed to the cart using hidden fields and custom data as follows, where isset($_REQUEST['price']) refers to the hidden price field set by jquery (thwre are a load of these for framing, mounting, size etc but this gives an idea:
if(isset($_REQUEST['price']) && ! empty( 'price' )) {
$order_item_price = sanitize_text_field($_REQUEST['price']);
$cart_item_data['custom_data']['order_price'] = array(
'label' => 'Total this item',
'value' => '£' . $order_item_price
);
}
...this works fine. The price and total price (along with other custom information about the product) are displayed in the cart and the total at the bottom of the cart tallies with the product prices. I use this function to achieve this:
function update_price_in_cart( $cart_obj ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
return;
}
if( ! is_cart() ) {
return;
}
// Iterate through each cart item
foreach( $cart_obj->get_cart() as $item_key=>$cart_item ) {
if( isset( $cart_item['total_price'] ) || isset( $cart_item['total_price_original'] ) ) {
$price = $cart_item['total_price'] + $cart_item['total_price_original'];
$cart_item['data']->set_price( ( $price ) );
}
}
}
add_action( 'woocommerce_before_calculate_totals', 'update_price_in_cart', 10, 1 );
The problem is when the user proceeds to the checkout. All the data created using hidden fields (frame type, mount type, size, price) displays on the left side of the page under where it says product but the totals on the right next to each product ordered are set to the original price set for the variation 'Print' set in the woocommerce product in wordpress.
I've tried up dating using something like this (the below is a test using static values):
function update_price_in_checkout($cart_obj) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
var_dump($cart_item_data['custom_data']['order_price']);
if( isset( $cart_item['total_price'] ) || isset( $cart_item['total_price_original'] ) ) {
WC()->cart->total = 597;
} else {
WC()->cart->total = 1;
}
}
add_action( 'woocommerce_review_order_before_order_total', 'update_price_in_checkout' );
...but a. this only accepts a static number as it doesn't pick up the hidden fields and b. it doesn't touch the individual product prices on the right of the checkout.
In summary - I need to find a way to replace the base variation product price on which is appearing on the right with the calculated totals based on user input. It all works fine in the cart page but doesn't transfer to the checkout page.
Delving through stackoverflow I think setting it in woocommerce_checkout_create_order_line_item might be an idea but I can't work out how to do this.
Any ideas?
The following code will add as custom to cart item data the price of your product select options (from the hidden product field) and will update the cart item price:
// Get custom field value, calculate new item price, save it as custom cart item data
add_filter( 'woocommerce_add_cart_item_data', 'add_custom_field_data', 20, 3 );
function add_custom_field_data( $cart_item_data, $product_id, $variation_id ) {
if (isset($_REQUEST['price']) && !empty($_REQUEST['price'])) {
$cart_item_data['custom_price'] = sanitize_text_field($_REQUEST['price']);
$cart_item_data['unique_key'] = md5(microtime().rand());
}
return $cart_item_data;
}
// Update price
add_action( 'woocommerce_before_calculate_totals', 'update_price_in_cart', 10, 1 );
function update_price_in_cart( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
// Loop through cart items
foreach( $cart->get_cart() as $cart_item ) {
if( isset( $cart_item['custom_price'] ) ) {
// Add options to the product price
$cart_item['data']->set_price( $cart_item['data']->get_price() + $cart_item['custom_price'] );
}
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and work.
I would like to alter a specific product in Woocommerce, adding programmatically to its original an amount of $10, EXCEPT in those cases:
On specific product pages that are bookable products for hire (Woocommerce Bookings) that belongs to a certain Category "C".
If any cart item belongs to that category "C".
I was using this answer code to one of my questions until now. But I should need to display this specific product altered price everywhere except on that product pages that are bookable products, in front end.
I have also this code that displays the Force Sells price beside the titles. This should still show the specific product original price on that product pages that are bookable products in the Force Sells section:
function wc_forcesells_product_price( $name, $product ) {
// Only in single product pages
if( ! is_product() ) return $name;
// The product name + the product formatted price
return $name . ' (' . wc_price( wc_get_price_to_display( $product ) ) . ')';
}
add_filter( 'woocommerce_product_title', 'wc_forcesells_product_price', 20, 2 );
Reference for above: Add price beside Woocommerce Force Sells.
Updated to handle multiple product Ids if necessary
Try the following that will display for a specific product ID a custom price everywhere except:
On booking products to hire pages
When a product category is in cart
The code will also change this custom product price in cart items except when a specific product category is in cart items.
You will have to set in those functions:
This specific product ID
The booking products IDs to hire
The Additional price amount
The product category (Id slug or name)
The code:
// Change product ID 87 price everywhere on front end EXCEPT:
// - On booking products to hire pages
// - When a product category is in cart
add_filter( 'woocommerce_get_price_html', 'product_id_87_displayed_price', 10, 2 );
function product_id_87_displayed_price( $price_html, $product ) {
// Only for product ID 87
if( in_array( $product->get_id(), array( 87, 2799 ) ) ){
return $price_html; // Exit
// HERE set booking products IDs to hire in the array
$product_ids_to_hire = array( 53, 738 );
// HERE set your product category term ID, slug or name
$product_category = 'hoodies';
// HERE set the price additional amount
$addp = 10;
// EXCEPT on booking products to hire pages
if( is_single($product_ids_to_hire) ) return $price_html; // Exit
// Checking for the product category in cart items loop
foreach ( WC()->cart->get_cart() as $cart_item ) {
if ( has_term( $product_category, 'product_cat', $cart_item['product_id'] ) )
return $price_html; // Product category found ==> Exit
}
if ( '' !== $product->get_price() && ! $product->is_on_sale() ) {
$price_html = wc_price( wc_get_price_to_display( $product ) + $addp ) . $product->get_price_suffix();
}
return $price_html;
}
// Add custom calculated price to cart item data for product ID 87
add_filter( 'woocommerce_add_cart_item_data', 'add_cart_simple_product_custom_price', 20, 2 );
function add_cart_simple_product_custom_price( $cart_item_data, $product_id ){
// Only for product ID 87
if( ! in_array( $product->get_id(), array( 87 ) ) )
return $cart_item_data; // Exit
// HERE set the price additional amount
$addp = 10;
$product = wc_get_product($product_id); // The WC_Product Object
$price = (float) $product->get_price(); // The product price
// Set the custom amount in cart object
$cart_item_data['new_price'] = $price + $addp;
return $cart_item_data;
}
// Set custom calculated price to cart for product ID 87
add_action( 'woocommerce_before_calculate_totals', 'set_cart_simple_product_custom_price', 20, 1 );
function set_cart_simple_product_custom_price( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
// HERE set your product category term ID, slug or name
$product_category = 'hoodies';
// 1st cart items Loop: checking for the product category
foreach ( $cart->get_cart() as $cart_item ) {
if ( has_term( $product_category, 'product_cat', $cart_item['product_id'] ) )
return; // Product category found ==> We EXIT from this function
}
// 2nd Loop: Changing cart item prices (Product category not found)
foreach ( $cart->get_cart() as $cart_item ) {
if( isset($cart_item['new_price']))
$cart_item['data']->set_price( $cart_item['new_price'] );
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
It should also work with your wc_forcesells_product_price() function.
I have a function on my Woocommerce website that enables customers to set a custom amount to pay for a specific product, based on a value I'm passing through the URL.
I'm using the woocommerce_before_calculate_totals hook, and up until I upgraded to WC 3.3.5, it was working fine. Now, when I run the code, the checkout initially shows the custom amount.
However, once the loader has finished updating, it resets the price to '0' (i.e. displaying £0.00 the checkout page's total fields).
Here's that code:
add_action( 'woocommerce_before_calculate_totals', 'pay_custom_amount', 99);
function pay_custom_amount() {
$payment_value = $_GET['amount'];
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
if($cart_item['data']->id == 21 ){
$cart_item['data']->set_price($payment_value);
}
}
}
Well, colour me baffled. I've trawled Stack Overflow for solutions but can't see any similar problems. I see the hook runs multiple times but gather this is normal.
If anyone know what might be happening here, it would be great if you could share.
You can't get a price from an URL and set it in woocommerce_before_calculate_totals action hook. This needs to be done differently.
In the below code:
the first hooked function will get that "amount" from the URl and will set it (register it) in cart item object as custom data.
the 2nd hooked function will read that amount from custom cart item data and will set it as the new price.
Now your the target product ID in your code need to be the same ID that the added to cart product.
The code:
// Get the custom "amount" from URL and save it as custom data to the cart item
add_filter( 'woocommerce_add_cart_item_data', 'add_pack_data_to_cart_item_data', 20, 2 );
function add_pack_data_to_cart_item_data( $cart_item_data, $product_id ){
if( ! isset($_GET['amount']) )
return $cart_item_data;
$amount = esc_attr( $_GET['amount'] );
if( empty($amount) )
return $cart_item_data;
// Set the custom amount in cart object
$cart_item_data['custom_price'] = (float) $amount;
$cart_item_data['unique_key'] = md5( microtime() . rand() ); // Make each item unique
return $cart_item_data;
}
// Alter conditionally cart item price based on product ID and custom registered "amount"
add_action( 'woocommerce_before_calculate_totals', 'change_conditionally_cart_item_price', 30, 1 );
function change_conditionally_cart_item_price( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
// HERE set your targeted product ID
$targeted_product_id = 21;
foreach ( $cart->get_cart() as $cart_item ) {
// Checking for the targeted product ID and the registered "amount" cart item custom data to set the new price
if($cart_item['data']->get_id() == $targeted_product_id && isset($cart_item['custom_price']) )
$cart_item['data']->set_price($cart_item['custom_price']);
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
This is the continuation of : Set product sale price programmatically in WooCommerce 3
The answer works, however once a user adds the product to cart, the old price still shows up on checkout.
How to get the correct sale price on cart and checkout pages for cart items?
Any help is appreciated.
The missing part to get it work for for cart and checkout pages (and also Orders and email notifications too) is a very simple trick:
add_action( 'woocommerce_before_calculate_totals', 'set_cart_item_sale_price', 20, 1 );
function set_cart_item_sale_price( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
// Iterate through each cart item
foreach( $cart->get_cart() as $cart_item ) {
$price = $cart_item['data']->get_sale_price(); // get sale price
$cart_item['data']->set_price( $price ); // Set the sale price
}
}
Code goes in function.php file of your active child theme (active theme).
Tested and works.
So the code just set the product sale price as the product price in cart items and it works.
Hope this code is helpful for you
add_filter( 'woocommerce_get_price_html', 'bbloomer_alter_price_display', 9999, 2 );
function bbloomer_alter_price_display( $price_html, $product ) {
// ONLY ON FRONTEND
if ( is_admin() ) return $price_html;
// ONLY IF PRICE NOT NULL
if ( '' === $product->get_price() ) return $price_html;
// IF CUSTOMER LOGGED IN, APPLY 20% DISCOUNT
if ( wc_current_user_has_role( 'customer' ) ) {
$orig_price = wc_get_price_to_display( $product );
$price_html = wc_price( $orig_price * 0.80 );
}
return $price_html;
}
add_action( 'woocommerce_before_calculate_totals', 'bbloomer_alter_price_cart', 9999 );
function bbloomer_alter_price_cart( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ) return;
// IF CUSTOMER NOT LOGGED IN, DONT APPLY DISCOUNT
if ( ! wc_current_user_has_role( 'customer' ) ) return;
// LOOP THROUGH CART ITEMS & APPLY 20% DISCOUNT
foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
$product = $cart_item['data'];
$price = $product->get_price();
$cart_item['data']->set_price( $price * 0.80 );
}
}
#LoicTheAztec answers works very nicely, but is not required.
You need to filter at least woocommerce_product_get_price and woocommerce_product_variation_get_price with the dynamic_sales_price_function.
To make it work really smoothly, you need some more filters as well.
The accepted answer did not work for me.
Here is what worked:
function get_active_price($price, $product) {
if ($product->is_on_sale()) {
return $product->get_sale_price();
}
return $product->get_regular_price();
}
add_filter('woocommerce_product_get_price', 'get_active_price'));
This worked with custom sale and regular prices.