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.
Related
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 am building a site using WooCommerce and Advanced Custom Fields (ACF) plugin. I've created two ACF custom fields with names "plant" and "amount".
I am trying to display them both on front end but currently only the custom field "plant" is displaying everywhere. I've been looking around the net with no luck.
add_action( 'woocommerce_before_add_to_cart_button', 'add_custom_field', 0 );
function add_custom_field() {
global $product; // Changed this
// Added this too (compatibility with WC +3)
$product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;
echo "<div class='produto-informacoes-complementares'>";
echo get_field( 'plant', $product_id );
echo "</div>";
echo "<div class='produto-informacoes-complementares'>";
echo get_field( 'amount', $product_id );
echo "</div>";
return true;
}
add_filter( 'woocommerce_add_cart_item_data', 'save_my_custom_product_field', 10, 2 );
function save_my_custom_product_field( $cart_item_data, $product_id ) {
$custom_field_value = get_field( 'plant', $product_id, true );
$custom_field_value = get_field( 'amount', $product_id, true );
if( !empty( $custom_field_value ) )
{
$cart_item_data['plant'] = $custom_field_value;
$cart_item_data['amount'] = $custom_field_value;
// below statement make sure every add to cart action as unique line item
$cart_item_data['unique_key'] = md5( microtime().rand() );
}
return $cart_item_data;
}
add_filter( 'woocommerce_get_item_data', 'render_meta_on_cart_and_checkout', 10, 2 );
function render_meta_on_cart_and_checkout( $cart_data, $cart_item ) {
$custom_items = array();
// Woo 2.4.2 updates
if( !empty( $cart_data ) ) {
$custom_items = $cart_data;
}
if( isset( $cart_item['plant'] )) {
$custom_items[] = array( "name" => "גודל עציץ", "value" => $cart_item['plant'] );
$custom_items[] = array( "name" => "כמות במגש", "value" => $cart_item['amount'] );
}
return $custom_items;
}
function add_order_item_meta_acf( $item_id, $values ) {
wc_add_order_item_meta( $item_id, 'גודל עציץ', $values [ 'plant' ] );
wc_add_order_item_meta( $item_id, 'גודל עציץ', $values [ 'amount' ] );
}
add_action( 'woocommerce_add_order_item_meta', 'add_order_item_meta_acf' , 10, 2);
Your code is a bit outdated and with some mistakes. Use the following to display product ACF custom fields everywhere (product page, cart, checkout, orders and email notifications):
// Display on product page
add_action( 'woocommerce_before_add_to_cart_button', 'display_acf_single_product_pages', 1 );
function display_acf_single_product_pages() {
global $product;
$plant = get_field( 'plant', $product->get_id() );
$amount = get_field( 'amount', $product->get_id() );
if ( ! empty($plant) && ! empty($amount) ) {
echo '<div class="produto-informacoes-complementares">';
if ( ! empty($plant) ) {
echo '<div class="plant"><strong>' . __("Size", "woocommerce") . '</strong>: ' . $plant . '</div>';
}
if ( ! empty($amount) ) {
echo '<div class="amount"><strong>' . __("Amount", "woocommerce") . '</strong>: ' . $amount . '</div>';
}
echo '</div>';
}
}
// Display on cart and checkout
add_filter( 'woocommerce_get_item_data', 'display_acf_on_cart_and_checkout', 10, 2 );
function display_acf_on_cart_and_checkout( $cart_data, $cart_item ) {
$plant = get_field( 'plant', $cart_item['product_id'] );
$amount = get_field( 'amount', $cart_item['product_id'] );
if ( ! empty($plant) ) {
$custom_items[] = array( "name" => __("Size", "woocommerce"), "value" => $plant );
}
if ( ! empty($amount) ) {
$custom_items[] = array( "name" => __("Amount", "woocommerce"), "value" => $amount );
}
return $custom_items;
}
// 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 );
}
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
I have a code that shows custom fields in the "General" tab on the product edit page.
After the manager has filled these fields, the data is displayed on the Archive/Category pages and in the Single Product page.
Also, these fields are in the cart and on the checkout page.
Here is my code:
// 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
// Custom Weight Field
woocommerce_wp_text_input(
array(
'id' => '_custom_weight',
'label' => __( 'Weight dishes', 'woocommerce' ),
'placeholder' => '',
'desc_tip' => 'true',
'description' => __( '', 'woocommerce' )
)
);
// Calories Field
woocommerce_wp_text_input(
array(
'id' => '_сalories',
'label' => __( 'Calories', 'woocommerce' ),
'placeholder' => '',
'desc_tip' => 'false',
'description' => __( '', 'woocommerce' )
)
);
// Ingredients Field
woocommerce_wp_textarea_input(
array(
'id' => '_ingredients',
'label' => __( 'Ingredients', 'woocommerce' ),
'placeholder' => '',
'desc_tip' => 'true',
'description' => __( '', 'woocommerce' )
)
);
}
// 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 Custom Weight Field
$custom_weight = $_POST['_custom_weight'];
if( ! empty( $custom_weight ) ) {
update_post_meta( $post_id, '_custom_weight', esc_attr( $custom_weight ) );
} else {
delete_post_meta( $post_id, '_custom_weight' );
}
// Save Calories Field
$сalories = $_POST['_сalories'];
if( ! empty( $сalories ) ) {
update_post_meta( $post_id, '_сalories', esc_attr( $сalories ) );
} else {
delete_post_meta( $post_id, '_сalories' );
}
// Save Ingredients Field
$ingredients = $_POST['_ingredients'];
if( ! empty( $ingredients ) ) {
update_post_meta( $post_id, '_ingredients', esc_html( $ingredients ) );
} else {
delete_post_meta( $post_id, '_ingredients' );
}
}
// Displaying the custom field value (on single product pages under short description)
add_action('woocommerce_before_add_to_cart_form', 'display_custom_meta_field_value', 25 );
add_action('woocommerce_after_shop_loop_item', 'display_custom_meta_field_value', 25 );
function display_custom_meta_field_value() {
global $product;
$custom_weight = get_post_meta( $product->get_id(),'_custom_weight', true );
if( ! empty( $custom_weight ) )
echo '<p id="value-on-single-product">' . 'Weight: ' . $custom_weight . 'g' . '</p>';
$сalories = get_post_meta( $product->get_id(),'_сalories', true );
if( ! empty( $сalories ) )
echo '<p id="value-on-single-product">' . 'Calories: ' . $сalories . ' kcal.' . '</p>';
$ingredients = get_post_meta( $product->get_id(),'_ingredients', true );
if( ! empty( $ingredients ) )
echo '<p id="value-on-single-product">' . 'Ingredients: ' . $ingredients . '</p>';
}
// Render the custom product field in cart and checkout
add_filter( 'woocommerce_get_item_data', 'woocom_custom_fields_cart', 10, 2 );
function woocom_custom_fields_cart( $cart_data, $cart_item )
{
$custom_items = array();
if( !empty( $cart_data ) )
$custom_items = $cart_data;
// Get the product ID
$product_id = $cart_item['product_id'];
if( $custom_field_value = get_post_meta( $product_id, '_custom_weight', true ) )
$custom_items[] = array(
'name' => __( 'Weight', 'woocommerce' ),
'value' => $custom_field_value,
'display' => $custom_field_value . 'g',
);
return $custom_items;
}
But unfortunately, it’s impossible for me to add these custom fields after the product name on the Thank You page, in the E-mail and on the order editing page.
I also have doubts whether the above code is correct, although everything works.
I will be glad for your help!
I have revisited your code and made some changes, adding the necessary code to display your custom fields data on cart, checkout, order received, my account order view and on email notifications (for Woocommerce 3+):
// Backend: Display additional product fields
add_action( 'woocommerce_product_options_general_product_data', 'add_fields_to_options_general_product_data' );
function add_fields_to_options_general_product_data() {
// Custom Weight Field
woocommerce_wp_text_input( array(
'id' => '_custom_weight',
'label' => __( 'Weight dishes', 'woocommerce' ),
));
// Calories Field
woocommerce_wp_text_input( array(
'id' => '_сalories',
'label' => __( 'Calories', 'woocommerce' ),
));
// Ingredients Field
woocommerce_wp_textarea_input( array(
'id' => '_ingredients',
'label' => __( 'Ingredients', 'woocommerce' ),
));
}
// Backend: Save the data value from the custom fields
add_action( 'woocommerce_admin_process_product_object', 'save_admin_product_custom_fields_values' );
function save_admin_product_custom_fields_values( $product ) {
// Save Custom Weight Field
if( isset( $_POST['_custom_weight'] ) ) {
$product->update_meta_data( '_custom_weight', sanitize_text_field( $_POST['_custom_weight'] ) );
}
// Save Calories Field
if( isset( $_POST['_сalories'] ) ) {
$product->update_meta_data( '_сalories', sanitize_text_field( $_POST['_сalories'] ) );
}
// Save Ingredients Field
if( isset( $_POST['_ingredients'] ) ) {
$product->update_meta_data( '_ingredients', sanitize_textarea_field( $_POST['_ingredients'] ) );
}
}
// Display product custom fields values on single product pages under short description and on archive pages
add_action('woocommerce_before_add_to_cart_form', 'display_custom_meta_field_value', 25 );
add_action('woocommerce_after_shop_loop_item', 'display_custom_meta_field_value', 25 );
function display_custom_meta_field_value() {
global $product;
if( $custom_weight = $product->get_meta('_custom_weight') )
echo '<p id="value-on-single-product">' . __("Weight:", "woocommerce") . ' ' . $custom_weight . 'g' . '</p>';
if( $сalories = $product->get_meta('_сalories') )
echo '<p id="value-on-single-product">' . __("Calories:", "woocommerce") . ' ' . $сalories . ' kcal.' . '</p>';
if( $ingredients = $product->get_meta('_ingredients') )
echo '<p id="value-on-single-product">' . __("Ingredients:", "woocommerce") . ' ' . $ingredients . '</p>';
}
// Add custom fields values under cart item name in cart
add_filter( 'woocommerce_cart_item_name', 'custom_cart_item_name', 10, 3 );
function custom_cart_item_name( $item_name, $cart_item, $cart_item_key ) {
if( ! is_cart() )
return $item_name;
if( $value1 = $cart_item['data']->get_meta('_custom_weight') ) {
$item_name .= '<br><span class="custom-field"><strong>' . __("Weight", "woocommerce") . ':</strong> ' . $value1 . 'g</span>';
}
if( $value2 = $cart_item['data']->get_meta('_сalories') ) {
$item_name .= '<br><span class="custom-field"><strong>' . __("Calories", "woocommerce") . ':</strong> ' . $value2 . 'kcal</span>';
}
if( $value3 = $cart_item['data']->get_meta('_ingredients') ) {
$item_name .= '<br><span class="custom-field"><strong>' . __("Ingredients", "woocommerce") . ':</strong> <br>' . $value3 . '</span>';
}
return $item_name;
}
// Display custom fields values under item name in checkout
add_filter( 'woocommerce_checkout_cart_item_quantity', 'custom_checkout_cart_item_name', 10, 3 );
function custom_checkout_cart_item_name( $item_qty, $cart_item, $cart_item_key ) {
if( $value1 = $cart_item['data']->get_meta('_custom_weight') ) {
$item_qty .= '<br><span class="custom-field"><strong>' . __("Weight", "woocommerce") . ':</strong> ' . $value1 . 'g</span>';
}
if( $value2 = $cart_item['data']->get_meta('_сalories') ) {
$item_qty .= '<br><span class="custom-field"><strong>' . __("Calories", "woocommerce") . ':</strong> ' . $value2 . 'kcal</span>';
}
if( $value3 = $cart_item['data']->get_meta('_ingredients') ) {
$item_qty .= '<br><span class="custom-field"><strong>' . __("Ingredients", "woocommerce") . ':</strong> <br>' . $value3 . '</span>';
}
return $item_qty;
}
// Display custom fields values on orders and email notifications
add_filter( 'woocommerce_order_item_name', 'custom_order_item_name', 10, 2 );
function custom_order_item_name( $item_name, $item ) {
$product = $item->get_product();
if( $value1 = $product->get_meta('_custom_weight') ) {
$item_name .= '<br><span class="custom-field"><strong>' . __("Weight", "woocommerce") . ':</strong> ' . $value1 . 'g</span>';
}
if( $value2 = $product->get_meta('_сalories') ) {
$item_name .= '<br><span class="custom-field"><strong>' . __("Calories", "woocommerce") . ':</strong> ' . $value2 . 'kcal</span>';
}
if( $value3 = $product->get_meta('_ingredients') ) {
$item_name .= '<br><span class="custom-field"><strong>' . __("Ingredients", "woocommerce") . ':</strong> <br>' . $value3 . '</span>';
}
return $item_name;
}
Code goes on function.php file of your active child theme (or active theme). Tested and works.
On cart page:
On checkout page:
On order received page:
On email notifications:
In functions.php
add_action( 'woocommerce_add_order_item_meta', 'custom_add_item_sku', 10, 3 );
function custom_add_item_sku( $item_id, $values, $cart_item_key ) {
$item_sku = get_post_meta( $values[ 'product_id' ], '_sku', true );
wc_add_order_item_meta( $item_id, 'SKU', $item_sku , false );
}
But its showing SKU value below Product title but i want it like this
SKU:123sd - Product Name
Update 2018-04-01 - Changing of hook and Improved code.
Use woocommerce_order_item_name filter hook instead. I have make some changes in your code:
add_filter( 'woocommerce_order_item_name', 'sku_before_order_item_name', 30, 3 );
function sku_before_order_item_name( $item_name, $item, $is_visible ) {
$product = $item->get_product();
$sku = $product->get_sku();
// When sku doesn't exist we exit
if( empty( $sku ) ) return $item_name;
$sku_text = __( 'SKU', 'woocommerce' ) . ': ' . $sku;
// Add product permalink when argument $is_visible is true
$product_permalink = $is_visible ? $product->get_permalink( $item ) : '';
if( $product_permalink )
return sprintf( '%s - %s', $product_permalink, $sku_text, $item->get_name() );
else
return $sku_text . ' - ' . $item->get_name();
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Tested in WooCommerce 3+ and works.
Displaying the SKU in cart items name (Updated: with the link for cart page):
add_filter( 'woocommerce_cart_item_name', 'sku_before_cart_item_name', 10, 3 );
function sku_before_cart_item_name( $product_name, $cart_item, $cart_item_key ) {
$product = $cart_item['data'];
$sku = $product->get_sku();
// When sku doesn't exist we exit
if( empty( $sku ) ) return $product_name;
$sku_text = __( 'SKU', 'woocommerce' ) . ': ' . $sku;
$product_permalink = $product->get_permalink( $cart_item );
if ( is_cart() )
return sprintf( '%s - %s', esc_url( $product_permalink ), $sku_text, $product->get_name() );
else
return $sku_text . ' - ' . $product_name;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Tested in WooCommerce 3+ and works.
I would change the hook to 'woocommerce_new_order_item' as it seems like there may be some deprecation issues with the 'woocommerce_add_order_item_meta'
Something like this should add the name after the SKU, but I don't know if that's exactly what you're looking for.
add_action( 'woocommerce_new_order_item', 'custom_add_item_sku', 10, 3 );
function custom_add_item_sku( $item_id, $values, $cart_item_key ) {
$item_sku = get_post_meta( $values[ 'product_id' ], '_sku', true );
$name = = $values['name'];
wc_add_order_item_meta( $item_id, 'SKU', $item_sku . ' - ' . $name , false );
}
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 );