Display purchase note and attribute on cart and checkout page in WooCommerce - php

The following code works and adds the purchase note to the cart items.
add_filter( 'woocommerce_get_item_data', 'wc_test', 10, 2 );
function wc_test ( $other_data, $cart_item ){
$post_data = get_post( $cart_item['product_id'] );
$other_data[] = array( 'name' => $post_data->_purchase_note );
return $other_data;
}
However, I always get ":" as result, for products that don't have a note.
I also need to add a specific attribute aswell under the note.
Any advice?

The colon is automatically added to the key. Because you only use name, this will be added at the end.
You can use get_purchase_note()
So you get:
// Display on cart & checkout pages
function filter_woocommerce_get_item_data( $item_data, $cart_item ) {
// Get an instance of the WC_Product object
$product = $cart_item['data'];
// Get purchase_note
$purchase_note = $product->get_purchase_note();
// Get the product attribute value (adjust if desired)
$attribute = $product->get_attribute( 'pa_color' );
// When empty
if ( empty ( $attribute ) ) {
$attribute = '';
}
// NOT empty
if ( ! empty( $purchase_note ) ) {
$item_data[] = array(
'key' => __( 'Note', 'woocommerce' ),
'value' => $purchase_note . $attribute,
'display' => $purchase_note . $attribute,
);
}
return $item_data;
}
add_filter( 'woocommerce_get_item_data', 'filter_woocommerce_get_item_data', 10, 2 );

Related

Add a select field on WooCommerce single product pages that allows an additional product to be added to cart

I am looking to add a selection of products to some of our single product pages.
I have added a select field that is grabbing all of the products of a specific category and adding them as options.
Based on the users selection, I would like to add the selected product to the cart along with the main product. This only if the additional product is not yet in the cart.
Here is my code attempt:
function comp_bracket_selection( $selection ) {
echo '<div><br></div>';
global $product;
$domain = 'woocommerce';
$args = array(
'category' => 'compressor-brackets',
'limit' => -1,
);
$product_array = wc_get_products( $args );
foreach ($product_array as $comp_product) {
$comp_id = $comp_product->get_id();
echo 'Product id: ' . $comp_id . ' | ';
$options[$comp_id] = $comp_product->get_name();
}
array_unshift($options, 'Choose an option');
woocommerce_form_field('compressor-options', array(
'id' => 'compressor-selection',
'type' => 'select',
'label' => __('Choose Compressor & Bracket', $domain),
'required' => false,
'options' => $options,
),''
);
echo '<p>Number of Compressor & Bracket options: ' . sizeof( $product_array ) . '</p>';
}
add_action( 'woocommerce_before_add_to_cart_button', 'comp_bracket_selection' );
function custom_add_to_cart() {
if (isset($_POST['compressor-options'])) {
$product_id = $_POST['compressor-options'];
$found = false;
if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
if ( $_product->id == $product_id )
$found = true;
}
if ( ! $found )
WC()->cart->add_to_cart( $product_id );
} else {
WC()->cart->add_to_cart( $product_id );
}
}
}
add_action( 'woocommerce_add_to_cart', 'custom_add_to_cart' );
But I have issues getting the selected products added to the cart. Any advice would be much appreciated.
Some notes on your code attempt/question:
The mistake you make is using array_unshift() causing all numerical array keys will be modified to start counting from zero
You use $_POST['compressor-options'] and $_POST['compressor-selection'] while they should be equal
To check whether a product is already in cart you can use WC_Cart::find_product_in_cart()
So you get:
function action_woocommerce_before_add_to_cart_button() {
global $product;
$domain = 'woocommerce';
$args = array(
'category' => 'compressor-bracket',
'limit' => -1,
);
// Retrieving products
$product_array = wc_get_products( $args );
// NOT empty
if ( ! empty( $product_array ) ) {
foreach ( $product_array as $product ) {
$product_id = $product->get_id();
echo 'Product id: ' . $product_id . ' | ';
$options[$product_id] = $product->get_name();
}
$options = array( 0 => __( 'Choose an option', $domain ) ) + $options;
// Add select field
woocommerce_form_field( 'compressor-options', array(
'id' => 'compressor-selection',
'type' => 'select',
'label' => __( 'Choose Compressor & Bracket', $domain ),
'required' => false,
'options' => $options,
),'' );
echo '<p>Number of Compressor & Bracket options: ' . sizeof( $product_array ) . '</p>';
}
}
add_action( 'woocommerce_before_add_to_cart_button', 'action_woocommerce_before_add_to_cart_button' );
function filter_woocommerce_add_to_cart( $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data ) {
if ( isset( $_POST['compressor-options'] ) ) {
// Get product ID
$the_product_id = sanitize_text_field( $_POST['compressor-options'] );
// WC Cart
if ( WC()->cart ) {
// Get cart
$cart = WC()->cart;
// If cart is NOT empty
if ( ! $cart->is_empty() ) {
// Cart id
$product_cart_id = $cart->generate_cart_id( $the_product_id );
// Find product in cart
$in_cart = $cart->find_product_in_cart( $product_cart_id );
// NOT in cart
if ( ! $in_cart ) {
$cart->add_to_cart( $the_product_id );
}
} else {
$cart->add_to_cart( $the_product_id );
}
}
}
}
add_action( 'woocommerce_add_to_cart', 'filter_woocommerce_add_to_cart', 10, 6 );
Note: if you want to exclude the current product from the select option list
Change:
$args = array(
'category' => 'compressor-bracket',
'limit' => -1,
);
To:
$args = array(
'category' => 'compressor-bracket',
'limit' => -1,
'exclude' => array( $product->get_id() ),
);

Display and save formated dimensions of the product everywhere

Inspired by Display the weight of cart and order items in WooCommerce #LoicTheAztec's answer, making some changes by replacing product weight by product dimensions, I am able to show the items dimensions everywhere on cart items and order items.
The problem is that I don't know how to show the labels or adjectives:
Longitud cm x Ancho cm x Alto cm for a Spanish site.
I really appreciate any help.
// Display the cart item dimensions in cart and checkout pages
add_filter( 'woocommerce_get_item_data', 'display_custom_item_data_dimensions', 10, 2 );
function display_custom_item_data_dimensions( $cart_item_data, $cart_item ) {
if ( $cart_item['data']->has_dimensions() > 0 ){
$cart_item_data[] = array( 'name' => __( 'Dimensions', 'woocommerce' ), 'value' => $cart_item['data']->get_dimensions() . ' ' . get_option('woocommerce_dimensions_unit') );
}
return $cart_item_data;
}
// Save and Display the order item dimensions (everywhere)
add_action( 'woocommerce_checkout_create_order_line_item', 'display_order_item_data_dimensions', 20, 4 );
function display_order_item_data_dimensions( $item, $cart_item_key, $values, $order ) {
if ( $values['data']->has_dimensions() > 0 ){
$item->update_meta_data( __( 'Dimensions', 'woocommerce' ), $values['data']->get_dimensions() . ' ' . get_option('woocommerce_dimensions_unit') );
}
}
First the method get_dimensions() has an option to display formatted dimensions, so you can change dimension formatting using the following, to add dimension labels as desired:
add_filter( 'woocommerce_format_dimensions', 'custom_formated_product_dimentions', 10, 2 );
function custom_formated_product_dimentions( $dimension_string, $dimensions ){
$text_domain = 'woocommerce';
$dimension_unit = get_option('woocommerce_dimensions_unit');
if ( empty( $dimension_string ) )
return __( 'N/A', $text_domain );
// Set here your new array of dimensions based on existing keys/values
$new_dimensions = array(
__('width', $text_domain) => $dimensions['width'],
__('length', $text_domain) => $dimensions['length'],
__('height', $text_domain) => $dimensions['height'],
);
$dimensions = array_filter( array_map( 'wc_format_localized_decimal', $new_dimensions ) );
// Loop through new dimensions array adding spanish labels
foreach( $dimensions as $key => $dimention ){
$dimensions[$key] = ucfirst($key) . ' ' . $dimention. ' ' . $dimension_unit;
}
return implode( ' × ', $dimensions);
}
For Spanish language, you can translate strings with Loco plugin or you can replace:
$new_dimensions = array(
__('width', $text_domain) => $dimensions['width'],
__('length', $text_domain) => $dimensions['length'],
__('height', $text_domain) => $dimensions['height'],
);
by:
$new_dimensions = array(
__('longitud', $text_domain) => $dimensions['width'],
__('ancho', $text_domain) => $dimensions['length'],
__('alto', $text_domain) => $dimensions['height'],
);
Now on your code you will use get_dimensions( true ) method to display formatted dimensions as follow:
// Display the cart item dimensions in cart and checkout pages
add_filter( 'woocommerce_get_item_data', 'display_custom_item_data_dimensions', 10, 2 );
function display_custom_item_data_dimensions( $cart_item_data, $cart_item ) {
$product = $cart_item['data'];
if ( $product->has_dimensions() ){
$cart_item_data[] = array(
'name' => __( 'Dimensions', 'woocommerce' ),
'value' => $product->get_dimensions( true ),
);
}
return $cart_item_data;
}
// Save and Display the order item dimensions (everywhere)
add_action( 'woocommerce_checkout_create_order_line_item', 'display_order_item_data_dimensions', 10, 4 );
function display_order_item_data_dimensions( $item, $cart_item_key, $values, $order ) {
$product = $values['data'];
if ( $product->has_dimensions() ){
$item->update_meta_data( __( 'Dimensions', 'woocommerce' ), $product->get_dimensions( true ) );
}
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
Note: As has_dimensions() is a conditional method returning a boolean value, it doesns't need anything else as a condition.
Similar: Rename Length to Diameter in WooCommerce formatted product dimensions output

Save custom metadata from subscription product variation to order

I need to be able to add admin data to subscription product variations (Woo Subscriptions extension) in order to pass this data on to fulfilment centres when an order is placed.
The issue I have is getting these values out from the product and passing them into the order and email notifications such that they can be properly exported, added to shipping labels and emailed via the various woo notifications.
I have custom meta fields added to variations on a subscription product. I can add data into these and they save data properly into the database on the correct variation ID. No problem there.
I can show custom test data on the cart and checkout page and also save this data into the order as you'll see in the code.
For brevity, the code is reduced below but there are in fact 6 text fields:
// Add custom field inputs to each product variation
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' => '_dose_customer_field[' . $loop . ']',
'label' => __( 'Dose (Customer)', 'woocommerce' ),
'placeholder' => __('Enter the treatment dosage','woocommerce' ),
'desc_tip' => 'true',
'description' => __( 'Enter dosage info for this variation', 'woocommerce' ),
'class' => '',
'value' => get_post_meta( $variation->ID, '_dose_customer_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 ) {
$_dose_customer_field = $_POST['_dose_customer_field'][$i];
if ( isset( $_dose_customer_field ) ) update_post_meta( $variation_id, '_dose_customer_field', esc_attr( $_dose_customer_field ) );
}
// Add metadata to cart item post.
add_filter( 'woocommerce_add_cart_item_data', 'add_custom_fields_cart_item_data', 10, 2 );
function add_custom_fields_cart_item_data( $cart_item_data, $product_id ){
$cart_item_data['custom_data']['_dose_customer_field'] = 'TEST DATA' /* HOW TO GET DATA FROM CUSTOM FIELD HERE */
$cart_item_data['custom_data']['unique_key'] = md5( microtime().rand() );
WC()->session->set( 'custom_data', $cart_item_data['custom_data'] );
return $cart_item_data;
}
// Display custom meta data on cart and checkout page.
add_filter( 'woocommerce_get_item_data', 'display_custom_fields_cart_item_data', 10, 2 );
function display_custom_fields_cart_item_data($item_data, $cart_item){
if( ! array_key_exists( 'custom_data', $cart_item ) )
return $item_data;
if( array_key_exists( '_dose_customer_field', $cart_item['custom_data'] ) )
$item_data[] = array(
'key' => __('Dose (Customer)', 'woocommerce'),
'value' => $cart_item['custom_data']['_dose_customer_field']
);
return $item_data;
}
// Add metadata to the order.
add_action('woocommerce_checkout_create_order_line_item', 'save_custom_fields_as_order_item_meta', 20, 4);
function save_custom_fields_as_order_item_meta($item, $cart_item_key, $values, $order) {
if( ! isset($values['custom_data']) )
return;
$text_domain ='woocommerce';
if( array_key_exists('_dose_customer_field', $values['custom_data']) ){
$item->update_meta_data( __('Dose (Customer)', $text_domain), $values['custom_data']['_dose_customer_field'] );
}
}
// Frontend & emails: Display data on notifications
add_action( 'woocommerce_order_item_meta_start', 'vp_order_item_display_commodity_code', 10, 4 );
function vp_order_item_display_commodity_code( $item_id, $item, $order, $plain_text ) {
// Not on admin
//if( is_admin() ) return;
if( $value = $item->get_meta('_dose_customer_field') ) {
$value = '<strong>' . __("Dose (Customer)", "woocommerce") . ':</strong> ' . esc_attr( $value );
// On orders
if( is_wc_endpoint_url() )
echo '<div class="vp-ccode"><small>' . $value . '</small></div>';
// On Emails
else
echo '<div>' . $value . '</div>';
}
}
So everything is working except getting the data from the custom fields. It seems odd that I have to create a new array (here called 'custom_data') and add it to the cart_item_data, when the data is already available on the product variation, albeit private.
My question is how do get the metadata within add_custom_fields_cart_item_data()? Is this even the correct approach?
Many thanks
As this custom data exist for the product, you can get it easily and save it as order item custom data (so it will be displayed everywhere):
The complete code (changed the 2 last functions):
// Add custom field inputs to each product variation
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' => '_dose_customer_field[' . $loop . ']',
'label' => __( 'Dose (Customer)', 'woocommerce' ),
'placeholder' => __('Enter the treatment dosage','woocommerce' ),
'desc_tip' => 'true',
'description' => __( 'Enter dosage info for this variation', 'woocommerce' ),
'class' => '',
'value' => get_post_meta( $variation->ID, '_dose_customer_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 ) {
$_dose_customer_field = $_POST['_dose_customer_field'][$i];
if ( isset( $_dose_customer_field ) ) update_post_meta( $variation_id, '_dose_customer_field', esc_attr( $_dose_customer_field ) );
}
// Display custom meta data on cart and checkout page.
add_filter( 'woocommerce_get_item_data', 'display_custom_fields_cart_item_data', 10, 2 );
function display_custom_fields_cart_item_data( $item_data, $cart_item ) {
if( $value = $cart_item['data']->get_meta( '_dose_customer_field' ) )
$item_data[] = array(
'key' => __('Dose (Customer)', 'woocommerce'),
'value' => $value
);
return $item_data;
}
// Add metadata to the order item (and display it everywhere).
add_action('woocommerce_checkout_create_order_line_item', 'save_custom_fields_as_order_item_meta', 10, 4);
function save_custom_fields_as_order_item_meta( $item, $cart_item_key, $values, $order ) {
if( $meta_value = $values['data']->get_meta( '_dose_customer_field' ) )
$item->update_meta_data( __('Dose (Customer)', 'woocommerce'), $meta_value );
}
Code goes in functions.php file of your active child theme (or active theme). It should work.

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