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 );
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 wanted to add a Custom Field to WooCommerce. The field is supposed to be plain Text Field.
Then it should be displayed on the Frontend. Unfortunately, I didn’t find such a Text Field Code Snippet. But the Code below worked:
/**
* #snippet Display RRP/MSRP # WooCommerce Single Product Page
* #how-to Get CustomizeWoo.com FREE
* #author Rodolfo Melogli
* #compatible WooCommerce 5
* #donate $9 https://businessbloomer.com/bloomer-armada/
*/
// -----------------------------------------
// 1. Add RRP field input # product edit page
add_action( 'woocommerce_product_options_pricing', 'bbloomer_add_RRP_to_products' );
function bbloomer_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',
));
}
// -----------------------------------------
// 2. Save RRP field via custom field
add_action( 'save_post_product', 'bbloomer_save_RRP' );
function bbloomer_save_RRP( $product_id ) {
global $typenow;
if ( 'product' === $typenow ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
if ( isset( $_POST['rrp'] ) ) {
update_post_meta( $product_id, 'rrp', $_POST['rrp'] );
}
}
}
// -----------------------------------------
// 3. Display RRP field # single product page
add_action( 'woocommerce_single_product_summary', 'bbloomer_display_RRP', 9 );
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 problem is that this Code is for Price. I needed something that is just plain Text, plus a few adjustments.
I want:
Title: PRODUCT YEAR
Field Type: Text Field
And when it is displayed, Title should be in Bold Font.
How can I make these happen?
Regards.
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>';
}
}
I've added a Custom SKU in products to use for vendor SKU for order confirmation email. What I've created works only for simple products and not variable products.
Custom SKU,
I added this to the functions.php
function jk_add_custom_sku() {
$args = array(
'label' => __( 'Custom SKU', 'woocommerce' ),
'placeholder' => __( 'Enter custom SKU here', 'woocommerce' ),
'id' => 'jk_sku',
'desc_tip' => true,
'description' => __( 'This SKU is for internal use only.', 'woocommerce' ),
);
woocommerce_wp_text_input( $args );
}
add_action( 'woocommerce_product_options_sku', 'jk_add_custom_sku' );
function jk_save_custom_sku( $post_id ) {
// grab the custom SKU from $_POST
$custom_sku = isset( $_POST[ 'jk_sku' ] ) ? sanitize_text_field( $_POST[ 'jk_sku' ] ) : '';
// grab the product
$product = wc_get_product( $post_id );
// save the custom SKU using WooCommerce built-in functions
$product->update_meta_data( 'jk_sku', $custom_sku );
$product->save();
}
add_action( 'woocommerce_process_product_meta', 'jk_save_custom_sku' );
Next I modified the email template email-order-items.php adding to the SKU section to check if Custom SKU exists.
<?php
// Show title/image etc.
if ( $show_image ) {
echo wp_kses_post( apply_filters( 'woocommerce_order_item_thumbnail', $image, $item ) );
}
// Product name.
echo wp_kses_post( apply_filters( 'woocommerce_order_item_name', $item->get_name(), $item, false ) );
// SKU.
if ( $show_sku && $sku ) {
echo wp_kses_post( ' (#' . $sku . ')' );
// load the custom SKU
$custom_sku = get_post_meta( $product->get_id(), 'jk_sku', true );
if ( is_string( $custom_sku ) ) { // only show the custom SKU if it's set
echo "<br>" . wp_kses_post( "Custom SKU: $custom_sku" ); // change this line if needed
}
}
// allow other plugins to add additional product information here.
do_action( 'woocommerce_order_item_meta_start', $item_id, $item, $order, $plain_text );
wc_display_item_meta(
$item,
array(
'label_before' => '<strong class="wc-item-meta-label" style="float: ' . esc_attr( $text_align ) . '; margin-' . esc_attr( $margin_side ) . ': .25em; clear: both">',
)
);
// allow other plugins to add additional product information here.
do_action( 'woocommerce_order_item_meta_end', $item_id, $item, $order, $plain_text );
?>
</td>
The Custom SKU only shows up on the email for Simple products. I need it to work with Variable products. There is only one Custom SKU for each product not for each variable product.
I have revisited a bit your code and changed the meta key to _sku2 just like WooCommerce meta keys starting with an underscore:
add_action( 'woocommerce_product_options_sku', 'add_product_sku2_custom_field' );
function add_product_sku2_custom_field() {
$field_key = '_sku2';
woocommerce_wp_text_input( array(
'id' => $field_key,
'label' => __( 'Custom SKU', 'woocommerce' ),
'placeholder' => __( 'Enter custom SKU here', 'woocommerce' ),
'desc_tip' => true,
'description' => __( 'This SKU is for internal use only.', 'woocommerce' ),
) );
}
add_action( 'woocommerce_admin_process_product_object', 'save_product_sku2_custom_field_value' );
function save_product_sku2_custom_field_value( $product ) {
$field_key = '_sku2';
if ( isset($_POST[$field_key]) ) {
$product->update_meta_data( $field_key, sanitize_text_field($_POST[$field_key]) );
}
}
For product variations, you need to get the parent variable product to get a "Custom sku" displayed (see at the code at the end)
Now to enable an additional custom SKU to product variations of a variable product too, use the following:
add_action( 'woocommerce_variation_options_pricing', 'add_product_variation_sku2_custom_field', 10, 3 );
function add_product_variation_sku2_custom_field( $loop, $variation_data, $variation ){
$field_key = '_sku2';
woocommerce_wp_text_input( array(
'id' => $field_key.'['.$loop.']',
'label' => __( 'Custom SKU', 'woocommerce' ),
'placeholder' => __( 'Enter custom SKU here', 'woocommerce' ),
'desc_tip' => true,
'description' => __( 'This SKU is for internal use only.', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, $field_key, true )
) );
}
add_action( 'woocommerce_save_product_variation', 'save_product_variation_sku2_custom_field_value', 10, 2 );
function save_product_variation_sku2_custom_field_value( $variation_id, $i ){
$field_key = '_sku2';
if( isset($_POST[$field_key][$i]) ){
update_post_meta( $variation_id, $field_key, sanitize_text_field($_POST[$field_key][$i]) );
}
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
And in your template custom code you will replace:
// SKU.
if ( $show_sku && $sku ) {
echo wp_kses_post( ' (#' . $sku . ')' );
// load the custom SKU
$custom_sku = get_post_meta( $product->get_id(), 'jk_sku', true );
if ( is_string( $custom_sku ) ) { // only show the custom SKU if it's set
echo "<br>" . wp_kses_post( "Custom SKU: $custom_sku" ); // change this line if needed
}
}
by the following (that will work for variable products or product variations):
// SKU (and SKU2)
if ( $show_sku && $sku ) {
echo wp_kses_post( ' (#' . $sku . ')' );
// load the custom SKU
$sku2 = $product->get_meta('_sku2');
// For product variations with empty SKU (get the parent variable product SKU)
if ( empty($sku2) && $product->is_type('variation') ) {
// Get parent variable product Id
$parent_product_id = $product->get_parent_id();
$parent_product = wc_get_product($parent_product_id);
$sku2 = $parent_product->get_meta('_sku2');
}
// only show the custom SKU if it's set (and product variation too)
if ( ! empty($sku2) ) {
echo "<br>" . wp_kses_post( sprintf( __("Custom SKU: %s", "woocommerce"), $sku2 ) ); // change this line if needed
}
}
For product variations, if there is not any "custom sku", it will try to get the parent variable product "custom sku".
It should work.
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 );