I don't find how to customize my pricing in my WordPress WebShop with the WooCommerce plugin.
I would like to have:
Article A 12€/kg
Article B 3€/piece
Article C 12€/colli
How is this possible please?
greets
Wim
You can add this on your functions.php
// Filter on cart price
add_filter( 'woocommerce_cart_product_price' , 'custom_price', 10, 2);
// Filter on product page
add_filter( 'woocommerce_get_price_html' , 'custom_price', 10, 2);
function custom_price( $price, WC_Product $product ){
// You can store the unit as custom fields
$unit = get_post_meta( $product->id, 'unit', true );
// Then concatenate with the price
$price .= $unit;
return $price;
}
Then you have to add a custom field on each product :
Related
I am trying to change product price in cart using the following function:
add_action( 'woocommerce_before_shipping_calculator', 'add_custom_price'
);
function add_custom_price( $cart_object ) {
foreach ( $cart_object->cart_contents as $key => $value ) {
$value['data']->price = 400;
}
}
It was working correctly in WooCommerce version 2.6.x but not working anymore in version 3.0+
How can I make it work in WooCommerce Version 3.0+?
Thanks.
Update 2021 (Handling mini cart custom item price)
With WooCommerce version 3.0+ you need:
To use woocommerce_before_calculate_totals hook instead.
To use WC_Cart get_cart() method instead
To use WC_product set_price() method instead
Here is the code:
// Set custom cart item price
add_action( 'woocommerce_before_calculate_totals', 'add_custom_price', 1000, 1);
function add_custom_price( $cart ) {
// This is necessary for WC 3.0+
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Avoiding hook repetition (when using price calculations for example | optional)
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
// Loop through cart items
foreach ( $cart->get_cart() as $cart_item ) {
$cart_item['data']->set_price( 40 );
}
}
And for mini cart (update):
// Mini cart: Display custom price
add_filter( 'woocommerce_cart_item_price', 'filter_cart_item_price', 10, 3 );
function filter_cart_item_price( $price_html, $cart_item, $cart_item_key ) {
if( isset( $cart_item['custom_price'] ) ) {
$args = array( 'price' => 40 );
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).
This code is tested and works (still works on WooCommerce 5.1.x).
Note: you can increase the hook priority from 20 to 1000 (or even 2000) when using some few specific plugins or others customizations.
Related:
Set cart item price from a hidden input field custom price in Woocommerce 3
Change cart item prices based on custom cart item data in Woocommerce
Set a specific product price conditionally on Woocommerce single product page & cart
Add a select field that will change price in Woocommerce simple products
With WooCommerce version 3.2.6, #LoicTheAztec's answer works for me if I increase the priority to 1000.
add_action( 'woocommerce_before_calculate_totals', 'add_custom_price', 1000, 1);
I tried priority values of 10,99 and 999 but the price and total in my cart did not change (even though I was able to confirm with get_price() that set_price() had actually set the price of the item.
I have a custom hook that adds a fee to my cart and I'm using a 3rd party plugin that adds product attributes. I suspect that these WooCommerce "add-ons" introduce delays that require me to delay my custom action.
Hi i tried to replace woocommerce product price with acf field with the following snippet but this snippet can only show only one field, i'm asking How to Display multiple fields in just one php line
Here the snippet:
add_filter( 'woocommerce_product_get_price', 'yl_get_dish_price', 20, 2 );
add_filter( 'woocommerce_product_get_regular_price', 'yl_get_dish_price', 20, 2 );
function yl_get_dish_price( $price, $product ) {
if ( $product->is_type('dish') ) {
$price = get_field( 'price', $product->get_id() );
}
return $price;
}
Here is a screenshot on how to display the price with acf
Here the full answer: Using ACF number field as price for WooCommerce custom product type
This code currently works to clear cart before adding a product to cart.
// Empty cart when product is added to cart, so we can't have multiple products in cart
add_action( 'woocommerce_add_cart_item_data', function( $cart_item_data ) {
wc_empty_cart();
return $cart_item_data;
} );
However I'm having trouble with making it product specific.
I'd like it to function as - if product ID#1 is added to cart, empty cart then add product (at whatever quantity).
How can I determine via the woocommerce_add_cart_item_data hook which product is added to the cart?
The woocommerce_add_cart_item_data hook contains 3 arguments versus 1,
and since $product_id is the 2nd it can be used to answer your question:
function filter_woocommerce_add_cart_item_data( $cart_item_data, $product_id, $variation_id ) {
// Get product ID
$product_id = $variation_id > 0 ? $variation_id : $product_id;
// When product ID is in array (multiple product IDs can be added, separated by a comma)
if ( in_array( $product_id, array( 1, 32, 30, 817 ) ) ) {
wc_empty_cart();
}
return $cart_item_data;
}
add_filter( 'woocommerce_add_cart_item_data', 'filter_woocommerce_add_cart_item_data', 10, 3 );
I'm new to wordpress and woocommerce. I've to hide add to cart button if the product weight is greater than 8 grams. I've used this code to do so.
add_action( 'woocommerce_checkout_process', 'action_woocommerce_checkout_process', 10, 0 );
add_filter( 'woocommerce_loop_add_to_cart_link', 'replace_default_button', 10, 2 );
function replace_default_button( $button, $product ){
$weight = $product->get_weight();
preg_replace('/\D/', '', $weight);
if ( $weight > 8 ){
$button = '';
}
return $button;
}
Its working great on shop page. But its not working on the single product page. Kindly help me to hide the add to cart button in single product page only if the product's weight is greater than 8 grams.
Your code is good for the shop page, for the single page you need to use a different hook:
// Removing the single product button add to cart
add_action( 'woocommerce_single_product_summary', 'replace_single_add_to_cart_button', 1 );
function replace_single_add_to_cart_button() {
global $product;
$weight = $product->get_weight();
preg_replace('/\D/', '', $weight);
if ( $weight > 8 ){
// For variable product types
remove_action( 'woocommerce_single_variation','woocommerce_single_variation_add_to_cart_button', 20 );
// For all other product types
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
}
}
I working on a WordPress website where users can purchase a membership and I want to Show that if a user is a member how much he can save. I'm using ACF.
My Question is how can I show ACF field on a checkout page next to product???
WordPress in general and WooCommerce have the thing called "Hook" to help us add our custom data.
This article is an introduction and example about WooCommerce Hooks
This is a virtual version of hooks on the WooCommerce checkout page.
So as your needs, you may use woocommerce_review_order_before_cart_contents hook, add this code to functions.php file in your theme:
Add to the checkout page in general
add_action( 'woocommerce_review_order_before_cart_contents', 'add_member_info' );
function add_member_info() {
echo '<div class=”member-message”>Become a member will save $100</div>';
}
Get data from a custom field in the product
I. To show in the single product
function cw_change_product_price_display( $price, $product ) {
$my_field = get_field('member_save', $product->ID);
$price .= " | Member save $$my_field";
return $price;
}
add_filter( 'woocommerce_get_price_html', 'cw_change_product_price_display', 10, 2);
add_filter( 'woocommerce_cart_item_price', 'cw_change_product_price_display', 10, 2);
II. To show in the checkout page
Add custom data with the product to Card
add_filter( 'woocommerce_add_cart_item_data', function ( $cartItemData, $productId, $variationId ) {
$member_saved = get_field('member_save', $productId);
$cartItemData['myCustomData'] = $member_saved;
return $cartItemData;
}, 10, 3 );
add_filter( 'woocommerce_get_cart_item_from_session', function ( $cartItemData, $cartItemSessionData, $cartItemKey ) {
if ( isset( $cartItemSessionData['myCustomData'] ) ) {
$cartItemData['myCustomData'] = $cartItemSessionData['myCustomData'];
}
return $cartItemData;
}, 10, 3 );
Show the data in the cart and checkout page
add_filter( 'woocommerce_get_item_data', function ( $data, $cartItem ) {
if ( isset( $cartItem['myCustomData'] ) ) {
$data[] = array(
'name' => 'Member saved',
'value' => $cartItem['myCustomData']
);
}
return $data;
}, 10, 2 );