Update cart item custom data on quantity change in Woocommerce - php

I have added a custom field to the woocommerce product named 'Donation', which stores donation of the individual product. Then, I added line item meta named 'line_donation'. Now I need to update 'line_donation' on product quantity change and after clicking update cart Button like product total changes.
function cfwc_add_custom_field_item_data( $cart_item_data, $product_id, $variation_id, $quantity ) {
// Add the item data
$cart_item_data['line_donation'] = get_post_meta($product_id,'donation', true);
return $cart_item_data;
}
add_filter( 'woocommerce_add_cart_item_data', 'cfwc_add_custom_field_item_data', 10, 4 );
/**
* Display the custom field value in the cart
* #since 1.0.0
*/
function cfwc_cart_item_name( $name, $cart_item, $cart_item_key ) {
if( isset( $cart_item['line_donation'] ) ) {
$name .= sprintf(
'<p>%s</p>',
esc_html( $cart_item['line_donation'] )
);
}
return $name;
}
add_filter( 'woocommerce_cart_item_name', 'cfwc_cart_item_name', 10, 3 );
function add_line_donation_to_order( $item, $cart_item_key, $values, $order ) {
foreach( $item as $cart_item_key=>$values ) {
if( isset( $values['line_donation'] ) ) {
$item->add_meta_data( __( 'line_donation', 'woocommerce' ), $values['line_donation'], true );
}
}
}
add_action( 'woocommerce_checkout_create_order_line_item', 'add_line_donation_to_order', 10, 4 );
Any help is welcome.

You just simply need to make a little change in your 2 last functions as follows:
add_filter( 'woocommerce_cart_item_name', 'cfwc_cart_item_name', 10, 3 );
function cfwc_cart_item_name( $name, $cart_item, $cart_item_key ) {
if( isset( $cart_item['line_donation'] ) )
$name .= '<p>' . $cart_item['line_donation'] * $cart_item['quantity'] . '</p>';
return $name;
}
add_action( 'woocommerce_checkout_create_order_line_item', 'add_line_donation_to_order', 10, 4 );
function add_line_donation_to_order( $item, $cart_item_key, $values, $order ) {
foreach( $item as $cart_item_key=>$values ) {
if( isset( $values['line_donation'] ) ) {
$item->add_meta_data( __( 'line_donation', 'woocommerce' ), $values['line_donation'] * $values['quantity'], true );
}
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.

Related

WooCommerce: Get product variation custom field value in a hooked function

This is how, I add an admin custom field for product Variations:
add_action( 'woocommerce_variation_options_pricing', 'add_custom_field_to_variations', 10, 3 );
function add_custom_field_to_variations( $loop, $variation_data, $variation ) {
woocommerce_wp_text_input( array(
'id' => 'custom_field[' . $loop . ']',
'class' => 'short',
'label' => __( 'Custom Field', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, 'custom_field', true )
) );
}
Save custom field on product variation save:
add_action( 'woocommerce_save_product_variation', 'save_custom_field_variations', 10, 2 );
function save_custom_field_variations( $variation_id, $i ) {
$custom_field = $_POST['custom_field'][$i];
if ( isset( $custom_field ) ) update_post_meta( $variation_id, 'custom_field', esc_attr( $custom_field ) );
}
Store custom field value into variation data
add_filter( 'woocommerce_available_variation', 'add_custom_field_variation_data' );
function add_custom_field_variation_data( $variations ) {
$variations ['custom_field'] = '<div class="woocommerce_custom_field">Custom Field: <span>' . get_post_meta( $variations[ 'variation_id' ], 'custom_field', true ) . '</span></div>';
return $variations;
}
The value is saving and displaying in the single product page but the below function is not returning the value
Trying to retrieve Value of the custom field in a function:
add_filter('woocommerce_variation_prices_price', 'get_custom_variable_price' )
function get_custom_variable_price() {
global $post;
$custom_value = get_post_meta( $variations[ 'variation_id' ], 'custom_field', true );
$custom_value = (float)$custom_value;
return $custom_value;
}
I need to get just the value (which is float number):
There are some mistakes and missing things in your las function (see below).
Now there are 2 ways to get the custom field value in that hooked function:
1). Using WC_Data get_meta() method (since WooCommerce 3):
add_filter('woocommerce_variation_prices_price', 'custom_variable_price', 100, 3 );
function custom_variable_price( $price, $variation, $product ) {
$value = floatval( $variation->get_meta( 'custom_field' ) );
if( $value > 0 ) {
$price = $value;
}
return $price;
}
2). Or using WordPress get_post_meta() function (the old way):
add_filter('woocommerce_variation_prices_price', 'custom_variable_price', 100, 3 );
function custom_variable_price( $price, $variation, $product ) {
$value = floatval( floatval( get_post_meta( $variation->get_id(), 'custom_field', true ) );
if( $value > 0 ) {
$price = $value;
}
return $price;
}
Code goes in functions.php file of the active child theme (or active theme). Tested, works for both.
Important note: You may not see the result in front end, as variable product price range is cached in WooCommerce to improve performances (see the linked thread below for better understanding).
Related thread: Change product prices via a hook in WooCommerce 3+
Addition related to your others functions
Since WooCommerce 3 you could update your 2nd and 3rd function as follows:
add_action( 'woocommerce_admin_process_variation_object', 'save_custom_field_variation_value', 10, 2 );
function save_custom_field_variation_value( $variation, $i ) {
if( isset($_POST['custom_field'][$i]) ) {
$variation->update_meta_data( 'custom_field', floatval( sanitize_text_field($_POST['custom_field'][$i]) ) );
}
}
and 3rd function
add_filter( 'woocommerce_available_variation', 'add_variation_custom_field_value_to_variation_data', 10, 3 );
function add_variation_custom_field_value_to_variation_data( $variation_data, $product, $variation ) {
if ( $value = $variation->get_meta( 'custom_field' ) ) {
$variation_data['custom_field'] = '<div class="woocommerce_custom_field">' .__("Custom Field") . ': <span>' . $value . '</span></div>';
}
return $variation_data;
}

Allow customer to set the product price and add to cart with certain validations in WooCommerce

I'm working on a giftcard product whereof I need the customer to be able to set the price as long as it is 100 or more. Problem is, I'm not sure how to create the value check.
The customer should then be able to add to cart as normal and to checkout as usual.
I've included a remove_action for the product price (which for some reason does not work) if the product is assigned to the giftcard category.
The field input has been created and the data should be carried over to the cart and checkout and into the order -- but it does not work for some reason.
The next step is to set the product price into whatever the customer submits as the giftcard value (as long as it is 100 or more) and to display that as the product price on cart and checkout.
If anyone can review and help me out, that would be awesome.
add_action( 'woocommerce_before_add_to_cart_form', 'giftcard_price_field' );
function giftcard_price_field() {
global $product;
if( has_term('giftcard', 'product_cat', $product->get_id() ) ) {
// if the product is assigned to the giftcard category, remove the product price
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
// add a new input field for the price, allowing the customer to set the price
echo '<div class="giftcard-product-price">
<label for="giftcard-product-price">Giftcard value: </label>
<input type="text" id="giftcard-product-price" name="giftcard-product-price" placeholder="Giftcard value" maxlength="1000">
</div>';
}
}
add_filter( 'woocommerce_add_cart_item_data', 'giftcard_price_field_cart_data', 10, 3 );
function giftcard_price_field_cart_data( $cart_item_data, $product_id, $variation_id ) {
if( ! empty ( $_POST[ 'giftcard-product-price' ] ) ) {
// need to check that the value is NOT below 100 and if so, create a wc_notice warning
$cart_item_data['giftcard-product-price'] = sanitize_text_field( $_POST['giftcard-product-price']);
}
return $cart_item_data;
}
add_filter( 'woocommerce_get_item_data', 'giftcard_price_field_display_data', 10, 2 );
function giftcard_price_field_display_data( $item_data, $cart_item ) {
if( ! empty ( $cart_item[ 'giftcard-product-price' ] ) ) {
$item_data[] = array (
'key' => 'Giftcard value',
'value' => $cart_item['giftcard-product-price'],
'display' => '',
);
}
return $item_data;
}
add_action( 'woocommerce_checkout_create_order_line_item', 'giftcard_price_field_order_data', 10, 4 );
function giftcard_price_field_order_data( $item, $cart_item_key, $values, $order ) {
if( ! empty ( $values[ 'giftcard-product-price' ] ) ) {
$item->add_meta_data( 'Giftcard value', $values['giftcard-product-price'] );
}
}
Add a new field 'giftcard_product_price' on single product page if has_term()
Removes the original product price on the single product page
Various validations have been added and are possible
The price of the product (giftcard) is adjusted to the price entered by the customer
function giftcard_price_field() {
global $product;
// Instanceof
if ( $product instanceof WC_Product ) {
// Set category(ies)
$cats = array ( 'giftcard' );
// True
if ( has_term( $cats, 'product_cat', $product->get_id() ) ) {
// add a new input field for the price, allowing the customer to set the price
echo '<div class="giftcard-product-price">
<label for="giftcard-product-price">Giftcard value: </label>
<input type="text" id="giftcard_product_price" name="giftcard_product_price" placeholder="Giftcard value" maxlength="1000">
</div>';
}
}
}
add_action( 'woocommerce_before_add_to_cart_button', 'giftcard_price_field', 10, 0 );
// Remove price
function action_woocommerce_single_product_summary() {
global $product;
// Instanceof
if ( $product instanceof WC_Product ) {
// Set category(ies)
$cats = array ( 'giftcard' );
// True
if ( has_term( $cats, 'product_cat', $product->get_id() ) ) {
remove_action('woocommerce_single_product_summary','woocommerce_template_single_price', 10 );
}
}
}
add_action( 'woocommerce_single_product_summary', 'action_woocommerce_single_product_summary', 5 );
// Validate
function filter_woocommerce_add_to_cart_validation( $passed, $product_id, $quantity, $variation_id = null, $variations = null ) {
// Isset
if ( isset ( $_POST['giftcard_product_price'] ) ) {
$giftcard_product_price = $_POST['giftcard_product_price'];
// Error = empty, not numeric or less than 100
if ( empty ( $giftcard_product_price ) ) {
wc_add_notice( __( 'Field is empty', 'woocommerce' ), 'error' );
$passed = false;
} elseif ( ! is_numeric ( $giftcard_product_price ) ) {
wc_add_notice( __( 'NOT a number or a numeric string', 'woocommerce' ), 'error' );
$passed = false;
} elseif ( $giftcard_product_price < 100 ) {
wc_add_notice( __( 'Less than 100', 'woocommerce' ), 'error' );
$passed = false;
}
}
return $passed;
}
add_filter( 'woocommerce_add_to_cart_validation', 'filter_woocommerce_add_to_cart_validation', 10, 5 );
function filter_add_cart_item_data( $cart_item_data, $product_id, $variation_id ) {
if ( isset ( $_POST['giftcard_product_price'] ) ) {
$cart_item_data['giftcard_product_price'] = sanitize_text_field( $_POST['giftcard_product_price'] );
}
return $cart_item_data;
}
add_filter( 'woocommerce_add_cart_item_data', 'filter_add_cart_item_data', 10, 3 );
// Set price
function action_woocommerce_before_calculate_totals( $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['giftcard_product_price'] ) ) {
$cart_item['data']->set_price( $cart_item['giftcard_product_price'] );
}
}
}
add_action( 'woocommerce_before_calculate_totals', 'action_woocommerce_before_calculate_totals', 10, 1 );

Pass custom product text field as cart item data in WooCommerce

The goal here is to have a text field on the product page.
The customer fills it in and adds to cart. The text is added to the product and included in the cart and on the checkout and on the order.
The field works and the validation works fine, but the text is not included / transferred to the cart, checkout and order.
Here's the code:
add_action( 'woocommerce_before_add_to_cart_button', 'product_add_on', 9 );
function product_add_on() {
$value = isset( $_POST['_custom_text_add_on'] ) ? sanitize_text_field( $_POST['_custom_text_add_on'] ) : '';
echo '<div><label>Custom Text Add-On <abbr class="required" title="required">*</abbr></label><p><input name="_custom_text_add_on" value="' . $value . '"></p></div>';
}
add_filter( 'woocommerce_add_to_cart_validation', 'product_add_on_validation', 10, 3 );
function product_add_on_validation( $passed, $product_id, $qty ){
if( isset( $_POST['_custom_text_add_on'] ) && sanitize_text_field( $_POST['_custom_text_add_on'] ) == '' ) {
wc_add_notice( 'Custom Text Add-On is a required field', 'error' );
$passed = false;
}
return $passed;
}
add_filter( 'woocommerce_add_cart_item_data', 'product_add_on_cart_item_data', 10, 2 );
function product_add_on_cart_item_data( $cart_item, $product_id ){
if( isset( $_POST['_custom_text_add_on'] ) ) {
$cart_item['custom_text_add_on'] = sanitize_text_field( $_POST['custom_text_add_on'] );
}
return $cart_item;
}
add_filter( 'woocommerce_get_item_data', 'product_add_on_display_cart', 10, 2 );
function product_add_on_display_cart( $_data, $cart_item ) {
if ( isset( $cart_item['custom_text_add_on'] ) ){
$data[] = array(
'name' => 'Custom Text Add-On',
'value' => sanitize_text_field( $cart_item['custom_text_add_on'] )
);
}
return $data;
}
add_action( 'woocommerce_add_order_item_meta', 'product_add_on_order_item_meta', 10, 2 );
function product_add_on_order_item_meta( $item_id, $values ) {
if ( ! empty( $values['custom_text_add_on'] ) ) {
wc_add_order_item_meta( $item_id, 'Custom Text Add-On', $values['custom_text_add_on'], true );
}
}
add_filter( 'woocommerce_order_item_product', 'product_add_on_display_order', 10, 2 );
function product_add_on_display_order( $cart_item, $order_item ){
if( isset( $order_item['custom_text_add_on'] ) ){
$cart_item_meta['custom_text_add_on'] = $order_item['custom_text_add_on'];
}
return $cart_item;
}
add_filter( 'woocommerce_email_order_meta_fields', 'product_add_on_display_emails' );
function product_add_on_display_emails( $fields ) {
$fields['custom_text_add_on'] = 'Custom Text Add-On';
return $fields;
}
I have copied the code and done a quick test and could found that you are missing underscore _ for the field name in two functions. You were using $_POST['custom_text_add_on'] instead of $_POST['_custom_text_add_on'] in the product_add_on_cart_item_data function. That was just a mistake.
add_filter( 'woocommerce_add_cart_item_data', 'product_add_on_cart_item_data', 10, 2 );
function product_add_on_cart_item_data( $cart_item, $product_id ){
if( isset( $_POST['_custom_text_add_on'] ) ) {
$cart_item['custom_text_add_on'] = sanitize_text_field( $_POST['_custom_text_add_on'] );
}
return $cart_item;
}
Hope you got the issue resolved

Single product custom date fields not validated and saved in Woocommerce

I've added two customer date inputs to the single product page. I need them to be required and validated before adding to the cart, and would also like the dates to be shown on the cart/checkout page and in the order emails.
I found the snippets needed here, however it was only for one custom field so I adjusted to make it for two: https://www.kathyisawesome.com/add-a-custom-field-to-woocommerce-product/
The input fields show up fine, but once you hit the Add to Cart button it doesn't carry throughout the order.
Here is the code used in my functions.php file:
/*
* Display inputs on single product page
*/
function amp_custom_option_1(){
$value = isset( $_POST['_est_delivery'] ) ? sanitize_text_field( $_POST['_est_delivery'] ) : '';
printf( '<div id="dates"><div class="delivery"><label>%s</label><input name="_est_delivery" value="%s" type="date" required /></div>', __( 'Estimated Delivery Date:', 'amp-plugin-textdomain-1' ), esc_attr( $value ) );
}
add_action( 'woocommerce_before_add_to_cart_form', 'amp_custom_option_1', 9 );
function amp_custom_option_2(){
$value = isset( $_POST['_est_pickup'] ) ? sanitize_text_field( $_POST['_est_pickup'] ) : '';
printf( '<div class="pickup"><label>%s</label><input name="_est_pickup" value="%s" type="date" required /></div></div>', __( 'Estimated Pickup Date:', 'amp-plugin-textdomain-2' ), esc_attr( $value ) );
}
add_action( 'woocommerce_before_add_to_cart_form', 'amp_custom_option_2', 9 );
/*
* Validate when adding to cart
*/
function amp_add_to_cart_validation_1($passed, $product_id, $qty){
if( isset( $_POST['_est_delivery'] ) && sanitize_text_field( $_POST['_est_delivery'] ) == '' ){
$product = wc_get_product( $product_id );
wc_add_notice( sprintf( __( '%s cannot be added to the cart until you enter a delivery date.', 'amp-plugin-textdomain-1' ), $product->get_title() ), 'error' );
return false;
}
return $passed;
}
add_filter( 'woocommerce_add_to_cart_validation', 'amp_add_to_cart_validation_1', 10, 3 );
function amp_add_to_cart_validation_2($passed, $product_id, $qty){
if( isset( $_POST['_est_pickup'] ) && sanitize_text_field( $_POST['_est_pickup'] ) == '' ){
$product = wc_get_product( $product_id );
wc_add_notice( sprintf( __( '%s cannot be added to the cart until you enter a pickup date.', 'amp-plugin-textdomain-2' ), $product->get_title() ), 'error' );
return false;
}
return $passed;
}
add_filter( 'woocommerce_add_to_cart_validation', 'amp_add_to_cart_validation_2', 10, 3 );
/*
* Add custom data to the cart item
*/
function amp_add_cart_item_data_1( $cart_item, $product_id ){
if( isset( $_POST['_est_delivery'] ) ) {
$cart_item['est_delivery'] = sanitize_text_field( $_POST['_est_delivery'] );
}
return $cart_item;
}
add_filter( 'woocommerce_add_cart_item_data', 'amp_add_cart_item_data_1', 10, 2 );
function amp_add_cart_item_data_2( $cart_item, $product_id ){
if( isset( $_POST['_est_pickup'] ) ) {
$cart_item['est_pickup'] = sanitize_text_field( $_POST['_est_pickup'] );
}
return $cart_item;
}
add_filter( 'woocommerce_add_cart_item_data', 'amp_add_cart_item_data_2', 10, 2 );
/*
* Load cart data from session
*/
function amp_get_cart_item_from_session_1( $cart_item, $values ) {
if ( isset( $values['est_delivery'] ) ){
$cart_item['est_delivery'] = $values['est_delivery'];
}
return $cart_item;
}
add_filter( 'woocommerce_get_cart_item_from_session', 'amp_get_cart_item_from_session_1', 20, 2 );
function amp_get_cart_item_from_session_2( $cart_item, $values ) {
if ( isset( $values['est_pickup'] ) ){
$cart_item['est_pickup'] = $values['est_pickup'];
}
return $cart_item;
}
add_filter( 'woocommerce_get_cart_item_from_session', 'amp_get_cart_item_from_session_2', 20, 2 );
/*
* Add meta to order item
*/
function amp_add_order_item_meta_1( $item_id, $values ) {
if ( ! empty( $values['est_delivery'] ) ) {
woocommerce_add_order_item_meta( $item_id, 'est_delivery', $values['est_delivery'] );
}
}
add_action( 'woocommerce_add_order_item_meta', 'amp_add_order_item_meta_1', 10, 2 );
function amp_add_order_item_meta_2( $item_id, $values ) {
if ( ! empty( $values['est_pickup'] ) ) {
woocommerce_add_order_item_meta( $item_id, 'est_pickup', $values['est_pickup'] );
}
}
add_action( 'woocommerce_add_order_item_meta', 'amp_add_order_item_meta_2', 10, 2 );
/*
* Get item data to display in cart
*/
function amp_get_item_data_1( $other_data, $cart_item ) {
if ( isset( $cart_item['est_delivery'] ) ){
$other_data[] = array(
'name' => __( 'Estimated Delivery Date:', 'amp-plugin-textdomain-1' ),
'value' => sanitize_text_field( $cart_item['est_delivery'] )
);
}
return $other_data;
}
add_filter( 'woocommerce_get_item_data', 'amp_get_item_data_1', 10, 2 );
function amp_get_item_data_2( $other_data, $cart_item ) {
if ( isset( $cart_item['est_pickup'] ) ){
$other_data[] = array(
'name' => __( 'Estimated Pickup Date', 'amp-plugin-textdomain-2' ),
'value' => sanitize_text_field( $cart_item['est_pickup'] )
);
}
return $other_data;
}
add_filter( 'woocommerce_get_item_data', 'amp_get_item_data_2', 10, 2 );
/*
* Show custom field in order overview
*/
function amp_order_item_product_1( $cart_item, $order_item ){
if( isset( $order_item['est_delivery'] ) ){
$cart_item_meta['est_delivery'] = $order_item['est_delivery'];
}
return $cart_item;
}
add_filter( 'woocommerce_order_item_product', 'amp_order_item_product_1', 10, 2 );
function amp_order_item_product_2( $cart_item, $order_item ){
if( isset( $order_item['est_pickup'] ) ){
$cart_item_meta['est_pickup'] = $order_item['est_pickup'];
}
return $cart_item;
}
add_filter( 'woocommerce_order_item_product', 'amp_order_item_product_2', 10, 2 );
/*
* Add the field to order emails
*/
function amp_email_order_meta_fields_1( $fields ) {
$fields['est_delivery'] = __( 'Estimated Delivery Date:', 'amp-plugin-textdomain-1' );
return $fields;
}
add_filter('woocommerce_email_order_meta_fields', 'amp_email_order_meta_fields_1');
function amp_email_order_meta_fields_2( $fields ) {
$fields['est_delivery'] = __( 'Estimate Pickup Date:', 'amp-plugin-textdomain-2' );
return $fields;
}
add_filter('woocommerce_email_order_meta_fields', 'amp_email_order_meta_fields_2');
I'm not sure what is wrong with my code? Any help is appreciated.
There was some errors and mistakes. I have changed and removed some hooks, remove unnecessary code, merged functions, revisited all your code. As Your 2 dates fields are on single product pages, they will be related to cart items and order items (so order items meta data).
I have set your 2 date fields slugs and labels in the first function, inside an array. Then I call that function everywhere else and I use a foreach loop to process each field. This avoid repetitions, optimize and compact the code.
The code (commented):
// Utility function that contain the 2 field keys and labels pairs used on all other functions
function get_date_label_keys(){
$text_domain = 'woocommerce';
return array( 'est_delivery' => __( 'Estimated Delivery Date', $text_domain ),
'est_pickup' => __( 'Estimated Pickup Date', $text_domain ) );
}
// Display custom fields on single product page (hook replaced)
add_action( 'woocommerce_before_add_to_cart_button', 'amp_display_custom_fields', 20 );
function amp_display_custom_fields(){
echo '<div id="dates">';
// Loop through each custom field
foreach( get_date_label_keys() as $key => $label ){
$class = str_replace('est_', '', $key); // The class
$value = isset($_POST[$key]) ? sanitize_text_field($_POST[$key]) : ''; // Display the value
printf( '<div class="%s"><label>%s:</label> <input type="date" name="%s" value="%s" required /></div>', $class, $label, $key, $value );
}
echo '</div><br clear="all">';
}
// Add to cart fields validation (in case of need)
add_filter( 'woocommerce_add_to_cart_validation', 'amp_add_to_cart_validation', 20, 3 );
function amp_add_to_cart_validation( $passed, $product_id, $qty ){
// Loop through each custom field
foreach( get_date_label_keys() as $key => $label ){
if( isset( $_POST[$key] ) && empty( $_POST[$key] ) ){
wc_add_notice( sprintf( __( '%s cannot be added to the cart until you enter a delivery date.', $domain ), get_the_title() ), 'error' );
$passed = false;
}
}
return $passed;
}
// Add to cart items the custom data
add_filter( 'woocommerce_add_cart_item_data', 'amp_add_cart_item_data', 20, 2 );
function amp_add_cart_item_data( $cart_item, $product_id ){
// Loop through each custom field
foreach( get_date_label_keys() as $key => $label ){
if( isset( $_POST[$key] ) )
$cart_item['dates'][$key] = sanitize_text_field( $_POST[$key] );
}
return $cart_item;
}
// Display the dates in cart items on cart and checkout pages
add_filter( 'woocommerce_get_item_data', 'amp_get_item_data', 20, 2 );
function amp_get_item_data( $item_data, $cart_item = null ) {
// Loop through each custom field
foreach( get_date_label_keys() as $key => $label ){
if ( isset( $cart_item['dates'][$key] ) )
$item_data[] = array(
'name' => $label,
'value' => sanitize_text_field( $cart_item['dates'][$key] )
);
}
return $item_data;
}
// Add order item meta data and Display the data in order items (hook replaced)
add_action( 'woocommerce_checkout_create_order_line_item', 'amp_add_order_item_meta', 20, 4 );
function amp_add_order_item_meta( $item, $cart_item_key, $values, $order ) {
foreach( get_date_label_keys() as $key => $label ){
// Loop through each custom field
if ( ! empty( $values['dates'][$key] ) )
$item->update_meta_data( $label, $values['dates'][$key] );
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
On cart page (and checkout too):
Order received and order view pages (in admin order edit pages and email notifications too):

Show custom field value in cart in WooCommerce

I'm using Wordpress together with WooCommerce for my shop.
I want to output the value of my custom field in my cart and in the email order confirmation.
I have created a custom field in my functions.php:
// Display Fields
add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );
// Save Fields
add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' );
function woo_add_custom_general_fields() {
global $woocommerce, $post;
echo '<div class="options_group">';
// Input
woocommerce_wp_text_input(
array(
'id' => '_gram',
'label' => __( 'somelabel', 'woocommerce' ),
'placeholder' => '',
'description' => __( 'sometext', 'woocommerce' ),
'type' => 'number',
'custom_attributes' => array(
'step' => 'any',
'min' => '0'
)
)
);
echo '</div>';
}
function woo_add_custom_general_fields_save( $post_id ){
// Number Field
$woocommerce_number_field = $_POST['_gram'];
if( !empty( $woocommerce_number_field ) )
update_post_meta( $post_id, '_gram', esc_attr( $woocommerce_number_field ) );
}
On my pages I'm using:
get_post_meta( get_the_ID(), '_gram', true );
To show the value and that work perfect.
Now I want to show this same value under the product name in my cart and in the email confirmations.
But I cant figure out how to do this.
Does anyone knows how to do this?
Well it's a long process, you have to implement two filters & one action to accomplish this.
Also there is a plugin for this exact purpose along with lot of other options related to woocommerce custom fields.
Here is the direct solution for your question.
/**
* Here we are trying to add your custom data as Cart Line Item
* SO that we can add this custom data on your cart, checkout, order and email later
*/
function save_custom_data( $cart_item_data, $product_id ) {
$custom_data = get_post_meta( $product_id, '_gram', true );
if( $custom_data != null && $custom_data != "" ) {
$cart_item_data["gram"] = $custom_data;
}
return $cart_item_data;
}
add_filter( 'woocommerce_add_cart_item_data', 'save_custom_data', 10, 2 );
/**
* Here we are trying to display that custom data on Cart Table & Checkout Order Review Table
*/
function render_custom_data_on_cart_checkout( $cart_data, $cart_item = null ) {
$custom_items = array();
/* Woo 2.4.2 updates */
if( !empty( $cart_data ) ) {
$custom_items = $cart_data;
}
if( isset( $cart_item["gram"] ) ) {
$custom_items[] = array( "name" => "Gram", "value" => $cart_item["gram"] );
}
return $custom_items;
}
add_filter( 'woocommerce_get_item_data', 'render_custom_data_on_cart_checkout', 10, 2 );
/**
* We are adding that custom data ( gram ) as Order Item Meta,
* which will be carried over to EMail as well
*/
function save_custom_order_meta( $item_id, $values, $cart_item_key ) {
if( isset( $values["gram"] ) ) {
wc_add_order_item_meta( $item_id, "Gram", $values["gram"] );
}
}
add_action( 'woocommerce_add_order_item_meta', 'save_custom_order_meta', 10, 3 );

Categories