I have a WooCommerce webshop with different kind of beers from all over the world. If you return a empty bottle you get a deposit of 0,10 or something like that. I made an ACF (Advanced Custom Field) where I can add the deposit price for an empty bottle. I managed to add this to my cart page and display it under the single price of a bottle (0,10) and under the subtotal if there are more bottles (0,40).
This is working in cart and checkout but I want to make a sum off the total deposit money if you return the bottles to the store.
How can I create a function that calculate the sum of all bottles in the cart that has a deposit?
I managed to get the first this working with this functions:
add_filter( 'woocommerce_cart_item_price', 'add_deposit_value_to_cart_single' , 10, 3 );
add_filter( 'woocommerce_cart_item_subtotal', 'add_deposit_value_to_cart_total', 10, 3 );
/**
* Statiegeld toevoegen onder single prijs product winkelwagen
*/
function add_deposit_value_to_cart_single( $product_price, $values ) {
$statiegeld = get_field( "statiegeld", $values['product_id']);
$dep_total = $statiegeld;
if( ! empty( $dep_total ) )
return $product_price . '<br /><small class="deposit_label">' . sprintf( __( 'statiegeld %s' ), wc_price( $dep_total ) ) . '</small>';
return $product_price;
}
/**
* Statiegeld toevoegen aan subtotaal in winkelwagen
*/
function add_deposit_value_to_cart_total( $product_price, $values ) {
$statiegeld = get_field( "statiegeld", $values['product_id']);
$dep_total = $statiegeld * $values['quantity'];
if( ! empty( $dep_total ) )
return $product_price . '<br /><small class="deposit_label">' . sprintf( __( 'statiegeld totaal %s' ), wc_price( $dep_total ) ) . '</small>';
return $product_price;
}
If someone has an idea I am very grateful because I can't figure it out.
There are some errors with the arguments of both functions.
Try this:
/**
* Statiegeld toevoegen onder single prijs product winkelwagen
*/
add_filter( 'woocommerce_cart_item_price', 'add_deposit_value_to_cart_single' , 10, 3 );
function add_deposit_value_to_cart_single( $price, $cart_item, $cart_item_key ) {
$statiegeld = get_field( "statiegeld", $cart_item['product_id']);
$dep_total = $statiegeld;
if( ! empty( $dep_total ) )
return $price . '<br /><small class="deposit_label">' . sprintf( __( 'statiegeld %s' ), wc_price( $dep_total ) ) . '</small>';
return $price;
}
/**
* Statiegeld toevoegen aan subtotaal in winkelwagen
*/
add_filter( 'woocommerce_cart_item_subtotal', 'add_deposit_value_to_cart_total', 10, 3 );
function add_deposit_value_to_cart_total( $price, $cart_item, $cart_item_key ) {
$statiegeld = get_field( "statiegeld", $cart_item['product_id']);
$dep_total = $statiegeld * $cart_item['quantity'];
if ( ! empty( $dep_total ) ) {
return $price . '<br /><small class="deposit_label">' . sprintf( __( 'statiegeld totaal %s' ), wc_price( $dep_total ) ) . '</small>';
}
return $price;
}
I have tested the code and it works fine. Here is the result:
I changed my whole code to this and this is working. This wil show a deposit in the cart summary under the price and a total deposit if it are more items under the subtotal.
Also in the cart total summary it is displaying the depost total of all items and in checkout too.
add_filter( 'woocommerce_cart_item_price', 'add_deposit_value_to_cart_single' , 10, 3 );
add_filter( 'woocommerce_cart_item_subtotal', 'add_deposit_value_to_cart_total', 10, 3 );
/**
* Statiegeld toevoegen aan single product
*/
function add_deposit_value_to_cart_single( $product_price, $values, $cart_item_key ) {
// Get your custom fields data
$custom_field1 = get_post_meta( $values['product_id'], '_aantal_statiegeld', true );
//$statiegeld = get_field( "statiegeld", $values['product_id']);
$dep_total = $custom_field1;
if( ! empty( $dep_total ) )
return $product_price . '<br /><small class="deposit_label">' . sprintf( __( 'statiegeld %s' ), wc_price( $dep_total ) ) . '</small>';
return $product_price;
}
/**
* Statiegeld toevoegen aan totaal
*/
function add_deposit_value_to_cart_total( $product_price, $values, $cart_item_key ) {
// Get your custom fields data
$custom_field1 = get_post_meta( $values['product_id'], '_aantal_statiegeld', true );
//$custom_field1 = get_field( "statiegeld", $values['product_id']);
$dep_total = $custom_field1 * $values['quantity'];
if( ! empty( $dep_total ) )
return $product_price . '<br /><small class="deposit_label">' . sprintf( __( 'statiegeld totaal %s' ), wc_price( $dep_total ) ) . '</small>';
return $product_price;
}
// Display Fields using WooCommerce Action Hook
add_action( 'woocommerce_product_options_general_product_data', 'woocom_general_product_data_custom_field' );
function woocom_general_product_data_custom_field() {
// Create a custom text field
// Number Field
woocommerce_wp_text_input(
array(
'id' => '_aantal_statiegeld',
'label' => __( 'Statiegeld', 'woocommerce' ),
'placeholder' => '',
'description' => __( 'Vul het statiegeld hier in', 'woocommerce' ),
'type' => 'number',
'custom_attributes' => array(
'step' => 'any',
'min' => '0,10'
)
)
);
}
// Hook to save the data value from the custom fields
add_action( 'woocommerce_process_product_meta', 'woocom_save_general_proddata_custom_field' );
/** Hook callback function to save custom fields information */
function woocom_save_general_proddata_custom_field( $post_id ) {
// Save Number Field
$number_field = $_POST['_aantal_statiegeld'];
if( ! empty( $number_field ) ) {
update_post_meta( $post_id, '_aantal_statiegeld', esc_attr( $number_field ) );
}
}
add_action( 'woocommerce_product_additional_information', 'custom_data_in_product_add_info_tab', 20, 1 );
function custom_data_in_product_add_info_tab( $product ) {
//Product ID - WooCommerce compatibility
$product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;
// Get your custom fields data
$custom_field1 = get_post_meta( $product_id, '_aantal_statiegeld', true );
// Set your custom fields labels (or names)
$label1 = __( 'Statiegeld', 'woocommerce');
// The Output
echo '<h3>'. __('Statiegeld totaal', 'woocommerce') .'</h3>
<table class="custom-fields-data">
<tbody>
<tr class="custom-field1">
<th>'. $label1 .'</th>
<td>'. $custom_field1 .'</td>
</tr>
</tbody>
</table>';
}
add_action( 'woocommerce_cart_totals_before_shipping', 'display_cart_volume_total', 20 );
add_action( 'woocommerce_review_order_before_shipping', 'display_cart_volume_total', 20 );
function display_cart_volume_total() {
$total_volume = 0;
// Loop through cart items and calculate total volume
foreach( WC()->cart->get_cart() as $cart_item ){
$product_volume = (float) get_post_meta( $cart_item['product_id'], '_aantal_statiegeld', true );
$total_volume += $product_volume * $cart_item['quantity'];
}
if( $total_volume > 0 ){
// The Output
echo ' <tr class="cart-total-volume">
<th>' . __( "Statiegeld totaal", "woocommerce" ) . '</th>
<td data-title="total-statiegeld">€' . number_format($total_volume, 2, ',', '.') . '</td>
</tr>';
}
}
Related
With WooCommerce, I'm using the Advanced Custom Fields plugin to Add product design URL
I tide this code to display "design_url"
// Display on orders and email notifications (save as custom order item meta data)
add_action( 'woocommerce_checkout_create_order_line_item', 'display_acf_on_orders_and_emails', 10, 4 );
function display_acf_on_orders_and_emails( $item, $cart_item_key, $values, $order ) {
$plant = get_field( 'plant', $values['product_id'] );
$amount = get_field( 'amount', $values['product_id'] );
if ( ! empty($plant) ) {
$item->add_meta_data( __("Size", "woocommerce"), $plant );
}
if ( ! empty($amount) ) {
$item->add_meta_data( __("Amount", "woocommerce"), $amount );
}
}
But I want display it only on order Admin page
add_action( 'woocommerce_order_item_meta_start', 'display_acf_on_order_admin_page', 10, 3 );
function display_acf_on_order_admin_page( $item_id, $item, $order ) {
$product_id = $item->get_product_id();
$plant = get_field( 'plant', $product_id );
$amount = get_field( 'amount', $product_id );
if ( ! empty( $plant ) ) {
echo '<p><strong>' . __( 'Size', 'woocommerce' ) . ':</strong> ' . $plant . '</p>';
}
if ( ! empty( $amount ) ) {
echo '<p><strong>' . __( 'Amount', 'woocommerce' ) . ':</strong> ' . $amount . '</p>';
}
}
This function will be called for each order item in the order, and it will display the "plant" and "amount" custom field values if they are present.
I'm currently trying to add a new text-field to my product page. I want to display the MSRP price of my products next to the product. Like on the image below. The MSRP should be on where the red line is.
Here is the image
I've done quite a bit of research on the topic. There are a few plugins that would fix the problem. However most of them have either bad reviews or are paid. I've succesfully added a meta field for the MSRP. However when i add a price to this field it doesn't show up on the product page at all.
function bbloomer_display_RRP() {
global $product;
if ( $product->get_type() <> 'variable' && $rrp = get_post_meta( $product->get_id(), 'rrp', true ) )
{
echo '<div class="woocommerce_rrp">';
_e( 'RRP: ', 'woocommerce' );
echo '<span>' . wc_price( $rrp ) . '</span>';
echo '</div>';
}
}
The code above should display the MSRP price on the product page. However it doesn't
Regards,
Luuc
Are you adding bbloomer_display_RRP to any hook? The function by itself, won't do anything. Based on your screenshot, I would add it to the woocommerce_template_single_price hook with a priority of 5 so that it will appear just before the price does.
/**
* Add RRP Field to product data metabox
*/
function kia_add_RRP_to_products() {
woocommerce_wp_text_input( array(
'id' => 'rrp',
'class' => 'short wc_input_price',
'label' => __( 'RRP', 'woocommerce' ) . ' (' . get_woocommerce_currency_symbol() . ')',
'data_type' => 'price',
));
}
add_action( 'woocommerce_product_options_pricing', 'kia_add_RRP_to_products' );
/**
* Process, verify and save product data
*
* #param WC_Product $product
*/
function kia_save_RRP( $product ) {
if ( isset( $_POST['rrp'] ) ) {
$rrp = wc_format_decimal( wc_clean( wp_unslash( $_POST['rrp'] ) ) );
$product->update_meta_data( 'rrp', $rrp );
} else {
$product->delete_meta_data( 'rrp' );
}
}
add_action( 'woocommerce_admin_process_product_object', 'kia_save_RRP' );
/**
* Display RRP on front-end in product summary
*/
function kia_display_RRP() {
global $product;
$rrp = '10';
if ( ! $product->is_type( 'variable' ) ) {
$rrp = $product->get_meta( 'rrp', true );
if ( $rrp ) {
echo '<div class="woocommerce_rrp">';
printf( __( 'RRP: %s', 'your-textdomain' ), wc_price( $rrp ) );
echo '</div>';
}
}
}
add_action( 'woocommerce_single_product_summary', 'kia_display_RRP', 5 );
Right now i am displaying the tax for all hte prices like total and shipping, now i have the requirement to add the same tax values for the subtotals as well as shown in the following image
I have found nothing related to this but for the cart page to add new field with the others, SO actually i want to add this tax information with the subtotal field.
Show subtotal excl. tax, add subtotal tax as separate row on Woocommerce checkout
To display in cart subtotal (orders and notifications) like "71,540.20€ (includes 11400,00€ VAT)", you can try to use the following:
// Cart and checkout (for display including taxes - not compound)
add_filter('woocommerce_cart_subtotal', 'wc_cart_subtotal_incl_tax_amount_filter', 10, 3 );
function wc_cart_subtotal_incl_tax_amount_filter( $cart_subtotal, $compound, $cart ) {
if ( wc_tax_enabled() && $cart->get_subtotal_tax() > 0 && ! $compound ) {
$subtotal_tax = wc_price( $cart->get_subtotal_tax() );
$cart_subtotal = wc_price( $cart->get_subtotal() + $cart->get_subtotal_tax() );
$tax_label = in_array( WC()->countries->get_base_country(), array_merge( WC()->countries->get_european_union_countries( 'eu_vat' ), array( 'NO' ) ), true ) ? __( 'VAT', 'woocommerce' ) : __( 'Tax', 'woocommerce' );
$cart_subtotal .= sprintf( ' <small>' . esc_html__( '(includes %s %s)', 'woocommerce' ) . '</small>', $subtotal_tax, $tax_label );
}
return $cart_subtotal;
}
// Orders and emails (for display including taxes - not compound)
add_filter('woocommerce_order_subtotal_to_display', 'wc_order_subtotal_incl_tax_amount_filter', 10, 3 );
function wc_order_subtotal_incl_tax_amount_filter( $subtotal, $compound, $order ) {
if ( wc_tax_enabled() && $order->get_total_tax() > 0 && ! $compound ) {
$subtotal_tax = $subtotal = 0; // Initializing
// Loop through order items
foreach ( $order->get_items() as $item ) {
$subtotal += $item->get_subtotal() + $item->get_subtotal_tax();
$subtotal_tax += $item->get_subtotal_tax();
}
$subtotal = wc_price( $subtotal, array( 'currency' => $order->get_currency() ) );
$subtotal_tax = wc_price( $subtotal_tax, array( 'currency' => $order->get_currency() ) );
$tax_label = in_array( WC()->countries->get_base_country(), array_merge( WC()->countries->get_european_union_countries( 'eu_vat' ), array( 'NO' ) ), true ) ? __( 'VAT', 'woocommerce' ) : __( 'Tax', 'woocommerce' );
$subtotal .= sprintf( ' <small>' . esc_html__( '(includes %s %s)', 'woocommerce' ) . '</small>', $subtotal_tax, $tax_label );
}
return $subtotal;
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
i have tried this code below and perfect working on variations product, but i want wholesale price only show on specific role user are logged in. if not login the price not showing.
Any help will be greatly appreciated.
// Add "Wholesale Price" custom field to Products option pricing
add_action( 'woocommerce_product_options_pricing', 'w4dev_add_product_options_pricing' );
function w4dev_add_product_options_pricing()
{
woocommerce_wp_text_input( array(
'id' => '_wholesale_price',
'class' => 'wc_input_wholesale_price short',
'label' => __( 'Wholesale Price', 'woocommerce' ) . ' ('.get_woocommerce_currency_symbol().')',
'type' => 'text'
));
}
// Add custom field to VARIATIONS option pricing
add_action( 'woocommerce_variation_options_pricing', 'w4dev_add_variation_options_pricing', 20, 3 );
function w4dev_add_variation_options_pricing( $loop, $variation_data, $post_variation )
{
$value = get_post_meta( $post_variation->ID, '_wholesale_price', true );
$symbol = ' (' . get_woocommerce_currency_symbol() . ')';
$key = 'wholesale_price[' . $loop . ']';
echo '<div class="variable_wholesale-price"><p class="form-row form-row-first">
<label>' . __( "Wholesale Price", "woocommerce" ) . $symbol . '</label>
<input type="text" size="5" name="' . $key .'" value="' . esc_attr( $value ) . '" />
</p></div>';
}
i have tried the code on Enable Wholesale prices in Woocommerce 3
// Save "Wholesale Price" custom field to Products
add_action( 'woocommerce_process_product_meta_simple', 'w4dev_save_product_wholesale_price', 20, 1 );
function w4dev_save_product_wholesale_price( $product_id ) {
if( isset($_POST['_wholesale_price']) )
update_post_meta( $product_id, '_wholesale_price', $_POST['_wholesale_price'] );
}
// Save "Wholesale Price" custom field to VARIATIONS
add_action( 'woocommerce_save_product_variation', 'w4dev_save_product_variation_wholesale_price', 20, 2 );
function w4dev_save_product_variation_wholesale_price( $variation_id, $i ){
if ( isset( $_POST['wholesale_price'][$i] ) ) {
update_post_meta( $variation_id, '_wholesale_price', floatval( $_POST['wholesale_price'][$i] ) );
}
}
// Simple, grouped and external products
add_filter('woocommerce_product_get_price', 'w4dev_custom_price', 90, 2 );
add_filter('woocommerce_product_get_regular_price', 'w4dev_custom_price', 90, 2 );
// Product variations (of a variable product)
add_filter('woocommerce_product_variation_get_regular_price', 'w4dev_custom_price', 99, 2 );
add_filter('woocommerce_product_variation_get_price', 'w4dev_custom_price', 90, 2 );;
function w4dev_custom_price( $price, $product ) {
if( get_post_meta( $product->get_id(), '_wholesale_price', true ) > 0 )
$price = get_post_meta( $product->get_id(), '_wholesale_price', true );
return $price;
}
// Variable product price ramge
add_filter('woocommerce_variation_prices_price', 'w4dev_custom_variation_price', 90, 3 );
add_filter('woocommerce_variation_prices_regular_price', 'w4dev_custom_variation_price', 90, 3 )
function w4dev_custom_variation_price( $price, $variation, $product ) {
if( get_post_meta( $variation->get_id(), '_wholesale_price', true ) > 0 )
$price = get_post_meta( $variation->get_id(), '_wholesale_price', true );
return $price;
}
I'm new to PHP, so this is confusing me a little. I'm using the Woocommerce plugin for Wordpress and I'm trying to add a custom field to display rental prices for certain products. However, not all products have a rental option, so I want this to only display on products that I give a rental price.
Here's the code I'm using, which works fine. The only problem is that it displays a rental price of $0 on products I haven't specified a rental price. Instead of display $0, I just want it to not display at all.
//add rental field
add_action( 'woocommerce_product_options_pricing', 'wc_rent_product_field' );
function wc_rent_product_field() {
woocommerce_wp_text_input( array( 'id' => 'rent_price', 'class' => 'wc_input_price short', 'label' => __( 'rent', 'woocommerce' ) . ' (' . get_woocommerce_currency_symbol() . ')' ) );
}
//save rental field
add_action( 'save_post', 'wc_rent_save_product' );
function wc_rent_save_product( $product_id ) {
// If this is a auto save do nothing, we only save when update button is clicked
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
if ( isset( $_POST['rent_price'] ) ) {
if ( is_numeric( $_POST['rent_price'] ) )
update_post_meta( $product_id, 'rent_price', $_POST['rent_price'] );
} else delete_post_meta( $product_id, 'rent_price' );
}
//display rental field
add_action( 'woocommerce_single_product_summary', 'wc_rent_show', 5 );
function wc_rent_show() {
global $product;
// Do not show this on variable products
if ( $product->product_type <> 'variable' ) {
$rent = get_post_meta( $product->id, 'rent_price', true );
echo '<div class="woocommerce_msrp">';
_e( 'Rent: ', 'woocommerce' );
echo '<span class="woocommerce-rent-price">' . woocommerce_price( $rent ) . '</span>';
echo '</div>';
}
}
Can anyone help with this? I've searched around the internet looking for an answer, but it seems to be going over my head.
if ( $product->product_type <> 'variable' ) {
$rent = get_post_meta( $product->id, 'rent_price', true );
if($rent)>0
{
echo '<div class="woocommerce_msrp">';
_e( 'Rent: ', 'woocommerce' );
echo '<span class="woocommerce-rent-price">' . woocommerce_price( $rent ) . '</span>';
echo '</div>';
}
}
This was exactly what I was looking for and after a little tweaking to the above answer the following code displays an annual rental price only for products with a rental price. It also orders the rental price to display after the RRP and discount prices on both the single product and archive pages.
//add rental field
add_action( 'woocommerce_product_options_pricing', 'wc_rent_product_field' );
function wc_rent_product_field() {
woocommerce_wp_text_input( array( 'id' => 'rent_price', 'class' => 'wc_input_price short', 'label' => __( 'Annual Rental', 'woocommerce' ) . ' (' . get_woocommerce_currency_symbol() . ')' ) );
}
//save rental field
add_action( 'save_post', 'wc_rent_save_product' );
function wc_rent_save_product( $product_id ) {
// If this is a auto save do nothing, we only save when update button is clicked
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
if ( isset( $_POST['rent_price'] ) ) {
if ( is_numeric( $_POST['rent_price'] ) )
update_post_meta( $product_id, 'rent_price', $_POST['rent_price'] );
} else delete_post_meta( $product_id, 'rent_price' );
}
//display rental field
add_action( 'woocommerce_single_product_summary', 'wc_rent_show', 20 );
function wc_rent_show() {
global $product;
// Do not show this on variable products
if ( $product->product_type <> 'variable' ) {
$rent = get_post_meta( $product->id, 'rent_price', true );
if ($rent > 0) {
echo '<div class="woocommerce_msrp">';
_e( 'Annual Rental: ', 'woocommerce' );
echo '<span class="woocommerce-rent-price">' . woocommerce_price( $rent ) . '</span>';
echo '</div>';
}
}
}
// Optional: To show on archive pages
add_action( 'woocommerce_after_shop_loop_item_title', 'wc_rent_show', 50 );