Custom WooCommerce product fields not saving in database - php

I made a custom field for a WooCommerce product but when I am trying to save it, Its value is not saving in the database
function product_certification_number() {
$args = array(
'id' => 'product_certification_number',
'label' => sanitize_text_field( 'Product Certification Number' ),
);
woocommerce_wp_text_input( $args );
}
add_action('woocommerce_product_options_general_product_data','product_certification_number' );
function product_certification_number_save( $post_id ) {
if ( ! ( isset( $_POST['woocommerce_meta_nonce'], $_POST[ 'product_certification_number' ] ) || wp_verify_nonce( sanitize_key( $_POST['woocommerce_meta_nonce'] ), 'woocommerce_save_data' ) ) ) {
return false;
}
$product_teaser = sanitize_text_field(
wp_unslash( $_POST[ 'product_certification_number' ] )
);
update_post_meta(
$post_id,
'product_certification_number',
esc_attr( $product_teaser )
);
}
add_action('woocommerce_process_product_meta','product_certification_number_save');

EDIT: used woocommerce_admin_process_product_object to save instead of outdated woocommerce_process_product_meta. Thnx to: #LoicTheAztec
// Add field
function product_certification_number() {
$args = array(
'id' => '_product_certification_number',
'label' => __( 'Product Certification Number', 'woocommerce' ),
'class' => 'custom-field',
'desc_tip' => true,
'description' => __( 'My description', 'woocommerce' ),
);
woocommerce_wp_text_input( $args );
}
add_action('woocommerce_product_options_general_product_data','product_certification_number', 10, 0 );
// Save
function product_certification_number_save( $product ){
if( isset($_POST['_product_certification_number']) ) {
$product->update_meta_data( '_product_certification_number', sanitize_text_field( $_POST['_product_certification_number'] ) );
}
}
add_action( 'woocommerce_admin_process_product_object', 'product_certification_number_save', 10, 1 );

I've wasted my two hours in this problem.
I finally found out that you need to call save_meta_data after update_meta_data:
/** #var WC_Product */
$product->update_meta_data($key, $value);
$product->save_meta_data();

Related

Woocommerce Checkbox

I've got a client who needs a checkbox for "accepting the terms and conditions etc." The tricky thing is that he will be adding in-person purchases on the admin panel and needs the checkbox there as well. I got the checkbox on both the front and back-end but I'm having some trouble getting them to talk to each other. When placing an order on the front-end, it shows as admin-panel-pic
But when you click into edit the order, the checkbox in not checked and if you update the order at all the value will disappear. I'm sure I'm missing something obvious but this is the first time I've had to modify Woocommerce fields like this.
Here's the code pertaining to the checkbox I have currently in my functions.php:
<?php
add_action('woocommerce_before_order_notes', 'wps_add_select_checkout_field');
function wps_add_select_checkout_field( $checkout ) {
woocommerce_form_field( 'rem_terms', array(
'type' => 'checkbox',
'class' => array('form-row mycheckbox'),
'label_class' => array('woocommerce-form__label woocommerce-form__label-for-checkbox
checkbox'),
'input_class' => array('woocommerce-form__input woocommerce-form__input-checkbox input-
checkbox'),
'required' => true,
'label' => 'I agree with the terms and conditions described in the Privacy Policy and the <a
target="_blank" rel="noopener" href="/liability-release-and-express-assumption-of-risk">
Liability Release </a>',) // Label and Link
);
$checkout->get_value( 'rem_terms' );
}
add_action('woocommerce_checkout_update_order_meta',
'wps_select_checkout_field_update_order_meta');
add_action( 'woocommerce_checkout_process', 'bt_add_checkout_checkbox_warning' );
/**
* Alert if checkbox not checked
*/
function bt_add_checkout_checkbox_warning() {
if ( ! (int) isset( $_POST['rem_terms'] ) ) {
wc_add_notice( __( 'Please agree to our terms and conditions before placing your order' ),
'error' );
}
}
function wps_select_checkout_field_update_order_meta( $order_id ) {
if ( ! empty( $_POST['rem_terms'] ) ) {
update_post_meta( $order_id, 'rem_terms', sanitize_text_field( $_POST['rem_terms'] ) );
}
}
add_action( 'woocommerce_admin_order_data_after_order_details',
'misha_editable_order_meta_general' );
function misha_editable_order_meta_general( $order ){ ?>
<br class="clear" />
<h4>Checkbox</h4>
<?php
/*
* get all the meta data values we need
*/
$rem_terms = get_post_meta( $order->get_id(), 'rem_terms', true );
?>
<div class="address">
<p><strong>Customer accepts REM's terms and conditions described in the
privacy policy</strong><?php echo $rem_terms?></p>
</div>
<div class="edit_address"><?php
woocommerce_wp_checkbox( array(
'id' => 'rem_terms',
'label' => 'Customer Agrees to terms and conditions',
'value' => $rem_terms,
'wrapper_class' => 'form-field-wide'
) );
?></div>
<?php }
add_action( 'woocommerce_process_shop_order_meta', 'save_new_fields' );
function save_new_fields( $ord_id ){
update_post_meta( $ord_id, 'rem_terms', wc_clean( $_POST[ 'rem_terms' ] ) );
}
function example_custom_order_fields( $fields, $order ) {
$new_fields = array();
if( get_post_meta( $order->id, 'rem_terms', true ) ) {
$new_fields['rem_terms'] = array(
'label' => 'Cust Accepts Terms',
'value' => get_post_meta( $order->id, 'rem_terms', true )
);
}
return array_merge( $fields, $new_fields );
}
add_filter( 'wcdn_order_info_fields', 'example_custom_order_fields', 10, 2 );
I was able to sort this out. Simply reassigning the value of the checkbox from 1 to "yes" as needed.

Custom metabox content displayed in single product additional tabs on Woocommerce

Thanks to these two posts I have come up with this solution for adding multiple tabs to Woocommerce product detail page.
Editing Custom product tab content in Woocommerce Admin product pages
Adding multiple tabs to WooCommerce single product pages
However, I am still having trouble saving the data and can't figure out how to make each meta_box save. I thought of a foreach statement but I'm not that well versed in PHP to know how the syntax is.
Here is my code. I have 6 tabs I added which show up on the page, and the meta boxes show up in the admin panel but they won't save any input. I only have one that is trying to save right now and I feel like the problem is in the save function.
add_action( 'add_meta_boxes', 'create_custom_meta_box' );
if ( ! function_exists( 'create_custom_meta_box' ) )
{
function create_custom_meta_box()
{
add_meta_box(
'custom_product_cost_field',
__( 'Cost and Performance Tab', 'woocommerce' ),
'add_custom_content_meta_box',
'product',
'normal',
'high'
);
add_meta_box(
'custom_product_environment_field',
__( 'Environment Tab', 'woocommerce' ),
'add_custom_content_meta_box',
'product',
'normal',
'high'
);
add_meta_box(
'custom_product_dilution_field',
__( 'Dilution Directions Tab', 'woocommerce' ),
'add_custom_content_meta_box',
'product',
'normal',
'high'
);
add_meta_box(
'custom_product_packaging_field',
__( 'Packaging and Handling', 'woocommerce' ),
'add_custom_content_meta_box',
'product',
'normal',
'high'
);
add_meta_box(
'custom_product_application_field',
__( 'Use and Application Tab', 'woocommerce' ),
'add_custom_content_meta_box',
'product',
'normal',
'high'
);
add_meta_box(
'custom_product_specification_field',
__( 'Product Specification Tab', 'woocommerce' ),
'add_custom_content_meta_box',
'product',
'normal',
'high'
);
}
}
// Custom metabox content in admin product pages
if ( ! function_exists( 'add_custom_content_meta_box' ) )
{
function add_custom_content_meta_box( $post )
{
$value = get_post_meta( $post->ID, 'cost_performance_tab', true ) ? get_post_meta( $post->ID, 'cost_performance_tab', true ) : '';
wp_editor( $value, 'custom_cost_performance_tab', array( 'editor_height' => 100 ) );
echo '<input type="hidden" name="custom_product_field_nonce" value="' . wp_create_nonce() . '">';
}
}
//Save the data of the Meta field
add_action( 'save_post', 'save_custom_content_meta_box', 10, 1 );
if ( ! function_exists( 'save_custom_content_meta_box' ) )
{
function save_custom_content_meta_box( $post_id ) {
// We need to verify this with the proper authorization (security stuff).
// Check if our nonce is set.
if ( ! isset( $_POST[ 'custom_product_field_nonce' ] ) ) {
return $post_id;
}
$nonce = $_REQUEST[ 'custom_product_field_nonce' ];
//Verify that the nonce is valid.
if ( ! wp_verify_nonce( $nonce ) ) {
return $post_id;
}
// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
}
// Check the user's permissions.
if ( 'page' == $_POST[ 'post_type' ] ) {
if ( ! current_user_can( 'edit_page', $post_id ) ) {
return $post_id;
}
} else {
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return $post_id;
}
}
// --- Its safe for us to save the data ! --- //
// Sanitize user input and update the meta field in the database.
update_post_meta( $post_id, 'cost_performance_tab', wp_kses_post($_POST[ 'custom_cost_performance_tab' ]) );
}
}
add_filter( 'woocommerce_product_tabs', 'woo_custom_product_tabs' );
function woo_custom_product_tabs( $tabs ) {
// 1) Removing tabs
unset( $tabs['description'] ); // Remove the description tab
// unset( $tabs['reviews'] ); // Remove the reviews tab
unset( $tabs['additional_information'] ); // Remove the additional information tab
// 2 Adding new tabs and set the right order
//Attribute Cost and Performance tab
$tabs['cost_performance_tab'] = array(
'title' => __( 'Cost + Performance', 'woocommerce' ),
'priority' => 100,
'callback' => 'woo_cost_performance_tab_content'
);
// Adds the environment tab
$tabs['environment_tab'] = array(
'title' => __( 'Environment', 'woocommerce' ),
'priority' => 110,
'callback' => 'woo_environment_tab_content'
);
// Adds the dilution tab
$tabs['dilution_tab'] = array(
'title' => __( 'Suggested Dilution Directions', 'woocommerce' ),
'priority' => 120,
'callback' => 'woo_dilution_tab_content'
);
// Adds the packaging tab
$tabs['packaging_tab'] = array(
'title' => __( 'Packaging + Handling', 'woocommerce' ),
'priority' => 120,
'callback' => 'woo_packaging_tab_content'
);
// Adds the application tab
$tabs['application_tab'] = array(
'title' => __( 'Use + Application', 'woocommerce' ),
'priority' => 120,
'callback' => 'woo_application_tab_content'
);
// Adds the application tab
$tabs['specification_tab'] = array(
'title' => __( 'Product Specification', 'woocommerce' ),
'priority' => 120,
'callback' => 'woo_specification_tab_content'
);
return $tabs;
}
function woo_cost_performance_tab_content() {
// The new tab content
$prod_id = get_the_ID();
echo'<div><p>'.get_post_meta( get_the_ID(), 'cost_performance_tab' ,true ).'</p></div>';
}
function woo_environment_tab_content() {
// The new tab content
$prod_id = get_the_ID();
echo'<div><p>'.get_post_meta( get_the_ID(), 'environment_tab' ,true ).'</p></div>';
}
function woo_dilution_tab_content() {
// The new tab content
$prod_id = get_the_ID();
echo'<div><p>'.get_post_meta( get_the_ID(), 'dilution_tab' ,true ).'</p></div>';
}
function woo_packaging_tab_content() {
// The new tab content
$prod_id = get_the_ID();
echo'<div><p>'.get_post_meta( get_the_ID(), 'packaging_tab' ,true ).'</p></div>';
}
function woo_application_tab_content() {
// The new tab content
$prod_id = get_the_ID();
echo'<div><p>'.get_post_meta( get_the_ID(), 'application_tab' ,true ).'</p></div>';
}
function woo_specification_tab_content() {
// The new tab content
$prod_id = get_the_ID();
echo'<div><p>'.get_post_meta( get_the_ID(), 'specification_tab' ,true ).'</p></div>';
}
You don't need 6 metaboxes in admin product edit pages and there is a lot of mistakes… Try the following instead:
// Add a custom metabox
add_action( 'add_meta_boxes', 'additional_product_tabs_metabox' );
function additional_product_tabs_metabox()
{
add_meta_box(
'add_product_metabox_additional_tabs',
__( 'Additional product Tabs', 'woocommerce' ),
'additional_product_tabs_metabox_content',
'product',
'normal',
'high'
);
}
// Add custom metabox content
function additional_product_tabs_metabox_content( $post )
{
// Cost and Performance
echo '<h4>' . __( 'Cost and Performance', 'woocommerce' ) . '</h4>';
$value = get_post_meta( $post->ID, '_cost_performance', true );
wp_editor( $value, '_cost_performance', array( 'editor_height' => 100 ) );
// Environment
echo '<br><hr><h4>' . __( 'Environment', 'woocommerce' ) . '</h4>';
$value = get_post_meta( $post->ID, '_environment', true );
wp_editor( $value, '_environment', array( 'editor_height' => 100 ) );
// Dilution Directions
echo '<br><hr><h4>' . __( 'Dilution Directions', 'woocommerce' ) . '</h4>';
$value = get_post_meta( $post->ID, '_dilution_directions', true );
wp_editor( $value, '_dilution_directions', array( 'editor_height' => 100 ) );
// Environment
echo '<br><hr><h4>' . __( 'Packaging and Handling', 'woocommerce' ) . '</h4>';
$value = get_post_meta( $post->ID, '_packaging_and_handling', true );
wp_editor( $value, '_packaging_and_handling', array( 'editor_height' => 100 ) );
// Use and Application
echo '<br><hr><h4>' . __( 'Use and Application', 'woocommerce' ) . '</h4>';
$value = get_post_meta( $post->ID, '_use_and_application', true );
wp_editor( $value, '_use_and_application', array( 'editor_height' => 100 ) );
// Environment
echo '<br><hr><h4>' . __( 'Specification', 'woocommerce' ) . '</h4>';
$value = get_post_meta( $post->ID, '_specification', true );
wp_editor( $value, '_specification', array( 'editor_height' => 100 ) );
// Nonce field (for security)
echo '<input type="hidden" name="additional_product_tabs_nonce" value="' . wp_create_nonce() . '">';
}
// Save product data
add_action( 'save_post_product', 'save_additional_product_tabs', 10, 1 );
function save_additional_product_tabs( $post_id ) {
// Security check
if ( ! isset( $_POST[ 'additional_product_tabs_nonce' ] ) ) {
return $post_id;
}
//Verify that the nonce is valid.
if ( ! wp_verify_nonce( $_POST[ 'additional_product_tabs_nonce' ] ) ) {
return $post_id;
}
// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
}
if ( ! current_user_can( 'edit_product', $post_id ) ) {
return $post_id;
}
// Sanitize user input and save the post meta fields values.
if( isset($_POST[ '_cost_performance' ]) )
update_post_meta( $post_id, '_cost_performance', wp_kses_post($_POST[ '_cost_performance' ]) );
if( isset($_POST[ '_environment' ]) )
update_post_meta( $post_id, '_environment', wp_kses_post($_POST[ '_environment' ]) );
if( isset($_POST[ '_dilution_directions' ]) )
update_post_meta( $post_id, '_dilution_directions', wp_kses_post($_POST[ '_dilution_directions' ]) );
if( isset($_POST[ '_packaging_and_handling' ]) )
update_post_meta( $post_id, '_packaging_and_handling', wp_kses_post($_POST[ '_packaging_and_handling' ]) );
if( isset($_POST[ '_use_and_application' ]) )
update_post_meta( $post_id, '_use_and_application', wp_kses_post($_POST[ '_use_and_application' ]) );
if( isset($_POST[ '_specification' ]) )
update_post_meta( $post_id, '_specification', wp_kses_post($_POST[ '_specification' ]) );
}
add_filter( 'woocommerce_product_tabs', 'woo_custom_product_tabs' );
function woo_custom_product_tabs( $tabs ) {
// 1) Removing tabs
unset( $tabs['description'] ); // Remove the description tab
unset( $tabs['additional_information'] ); // Remove the additional information tab
// 2 Adding new tabs and set the right order
//Attribute Cost and Performance tab
$tabs['cost_performance_tab'] = array(
'title' => __( 'Cost + Performance', 'woocommerce' ),
'priority' => 10,
'callback' => 'woo_cost_performance_tab_content'
);
// Adds the environment tab
$tabs['environment_tab'] = array(
'title' => __( 'Environment', 'woocommerce' ),
'priority' => 20,
'callback' => 'woo_environment_tab_content'
);
// Adds the dilution tab
$tabs['dilution_tab'] = array(
'title' => __( 'Suggested Dilution Directions', 'woocommerce' ),
'priority' => 30,
'callback' => 'woo_dilution_tab_content'
);
// Adds the packaging tab
$tabs['packaging_tab'] = array(
'title' => __( 'Packaging + Handling', 'woocommerce' ),
'priority' => 40,
'callback' => 'woo_packaging_tab_content'
);
// Adds the application tab
$tabs['application_tab'] = array(
'title' => __( 'Use + Application', 'woocommerce' ),
'priority' => 60,
'callback' => 'woo_application_tab_content'
);
// Adds the specification tab
$tabs['specification_tab'] = array(
'title' => __( 'Specification', 'woocommerce' ),
'priority' => 70,
'callback' => 'woo_specification_tab_content'
);
$tabs['reviews']['priority'] = 80;
return $tabs;
}
function woo_cost_performance_tab_content() {
global $product;
echo'<div><p>'. $product->get_meta( '_cost_performance' ) . '</p></div>';
}
function woo_environment_tab_content() {
global $product;
echo'<div><p>'. $product->get_meta( '_environment' ) . '</p></div>';
}
function woo_dilution_tab_content() {
global $product;
echo'<div><p>'. $product->get_meta( '_dilution_directions' ) . '</p></div>';
}
function woo_packaging_tab_content() {
global $product;
echo'<div><p>'. $product->get_meta( '_packaging_and_handling' ) . '</p></div>';
}
function woo_application_tab_content() {
global $product;
echo'<div><p>'. $product->get_meta( '_use_and_application' ) . '</p></div>';
}
function woo_specification_tab_content() {
global $product;
echo'<div><p>'. $product->get_meta( '_specification' ) . '</p></div>';
}
Code goes in function.php file of your active child theme (or active theme). Tested and work.

woocommerce_wp_select options array from product attribute terms

I am trying to create a drop down list box in woocommerce but have it populated with data from the database.
I have the majority of the code working but the portion to populate the list box is killing me. This is what I have so far.
add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' ); // Display Extra Fields on General Tab Section
add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' ); // Save Fields
function woo_add_custom_general_fields() {
global $woocommerce, $post, $product;
echo '<div class="options_group">';
// Select
$options = array(
'hide_empty' => false,
'order' => 'ASC',
'fields' => 'names'
);
$DogBreeds = get_terms('pa_breed', $options);
foreach ($DogBreeds as $key => $value) {
$theArray = "'{$value}' => __( '{$value}' , 'woocommerce' ), ";
}
woocommerce_wp_select(
array(
'id' => '_select',
'label' => __( 'My Select Field', 'woocommerce' ),
'options' => $theArray //this is where I am having trouble
)
);
echo $theArray;
echo '<pre>';
var_dump($DogBreeds);
echo '</pre>';
echo '</div>';
}
// Save Fields
function woo_add_custom_general_fields_save( $post_id ){
// Select
$woocommerce_select = $_POST['_select'];
if( !empty( $woocommerce_select ) )
update_post_meta( $post_id, '_select', esc_attr( $woocommerce_select ) );
else {
update_post_meta( $post_id, '_select', Null );
}
}
This should pull information from the "ATTRIBUTES" section in WooCommerce. I created a Breed Attribute and put a few dog breeds in there.
Any direction is greatly appreciated!
I know the 'options' section on the array is totally wrong but I put it there so you would know what I am trying to accomplish.
I have revisited your code a bit. The main problem was in the select <option> array.
You will see in the code the changes:
// Display Extra Fields on General Tab Section
add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );
function woo_add_custom_general_fields() {
global $post;
// Set HERE the product attribute taxonomy
$taxonomy = 'pa_breed';
// Get the selected value <== <== (updated)
$value = get_post_meta( $post->ID, '_select', true );
if( empty( $value ) ) $value = '';
$dog_breeds = get_terms( $taxonomy, array(
'hide_empty' => false,
'order' => 'ASC',
'fields' => 'names'
) );
$options[''] = __( 'Select a value', 'woocommerce'); // default value
foreach ($dog_breeds as $key => $term)
$options[$term] = $term; // <=== <=== <=== Here the correct array
echo '<div class="options_group">';
woocommerce_wp_select( array(
'id' => '_select',
'label' => __( 'My Select Field', 'woocommerce' ),
'options' => $options, //this is where I am having trouble
'value' => $value,
) );
echo '</div>';
}
// Save Fields
add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' );
function woo_add_custom_general_fields_save( $post_id ){
// Select
$woocommerce_select = $_POST['_select'];
if( !empty( $woocommerce_select ) )
update_post_meta( $post_id, '_select', esc_attr( $woocommerce_select ) );
else {
update_post_meta( $post_id, '_select', '' );
}
}
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. You will get something similar to this (with your breeds):
This will work for sure:
woocommerce_wp_select(array(
'id' => '_select',
'label' => __('My Select Field', 'woocommerce'),
'options' => array(
'red' => 'Red',
'blue' => 'Blue'
),
));

Custom Variable get_post_meta is returning a blank value

http://www.remicorson.com/woocommerce-custom-fields-for-variations/
I followed a tutorial on how to create custom fields for product variations. This worked, however now I am trying to export custom fields data into the email customizer template file.
My attempt to do this output a blank value; "array(0) { }"
Note: "$product_idMe" is working, and puts in the correct product ID.
$product_attr2 = get_post_meta( $product_idMe, '_variable_loc');
echo var_dump( $product_attr2 );
Here is the original tutorial code to get the custom variables working in the Wordpress admin, if that helps to reference how it was made:
add_action( 'woocommerce_product_after_variable_attributes', 'variation_settings_fields', 10, 3 );
add_action( 'woocommerce_save_product_variation', 'save_variation_settings_fields', 10, 2 );
function variation_settings_fields( $loop, $variation_data, $variation ) {
woocommerce_wp_text_input(
array(
'id' => '_variable_loc[' . $variation->ID . ']',
'label' => __( 'Inv Location', 'woocommerce' ),
'placeholder' => '',
'desc_tip' => 'true',
'description' => __( 'Enter the custom value here.', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, '_variable_loc', true )
)
);
function save_variation_settings_fields( $post_id ) {
$variable_loc = $_POST['_variable_loc'][ $post_id ];
if( ! empty( $variable_loc) ) {
update_post_meta( $post_id, '_variable_loc', esc_attr( $variable_loc ) );
}
add_filter( 'woocommerce_available_variation', 'load_variation_settings_fields' );
function load_variation_settings_fields( $variations ) {
$variations['text_field'] = get_post_meta( $variations[ 'variation_id' ], '_text_field', true );
return $variations;
}

WPML - Cart item data custom Fields don't update when switching language

I am using Woocommerce plugin with WPML Multilingual plugin, and I simply can't get to work like I would expect.
My products have serveral Custom Fields that I need to display in the cart, Checkout, Order views and emails notifications.
The Fields are displayed correctly on frontend but when I switch the language the data from sessions are not updated.
How does WPML handle the extra data?
Is there a way to get this to work, that the data update when i switch the language?
Here is my code:
add_filter( 'woocommerce_product_data_tabs', 'mbextra_product_data_tab' , 99 , 1 );
function mbextra_product_data_tab( $product_data_tabs ) {
$product_data_tabs['mbextraproducttab'] = array(
'label' => __( 'EXTRA', 'mbg' ),
'target' => 'mbextraproductdata',
'class' => array();
);
return $product_data_tabs;
}
add_action( 'woocommerce_product_data_panels', 'mbextra_product_data_fields' );
function mbextra_product_data_fields() {
?>
<div id="mbextraproductdata" class="panel woocommerce_options_panel">
<div class="options_group">
<?php
woocommerce_wp_textarea_input(
array(
'id' => 'company',
'label' => __( 'Company', 'mbg' ),
'placeholder' => 'Company Adress here',
'desc_tip' => 'true',
'description' => __( 'Enter Company Adress here', 'mbg' )
)
);
?>
</div>
<div class="options_group">
<?php
woocommerce_wp_textarea_input(
array(
'id' => 'shortdescription',
'label' => __( 'Short-Description', 'mbg' ),
'placeholder' => 'Enter some short info',
'desc_tip' => 'true',
'description' => __( 'Enter Short description here', 'mbg' )
)
);
?>
</div>
</div><!-- #extraproductdata Tab -->
<?php
}
add_action( 'woocommerce_process_product_meta', 'mbprocess_product_meta_fields_save', 99 );
function mbprocess_product_meta_fields_save( $post_id ){
// if set > save the fields
$company = $_POST['company'];
if( isset( $company ) )
update_post_meta( $post_id, 'company', esc_attr( $company ) );
// if set > save data to post_meta
$shortdescription = $_POST['shortdescription'];
if( isset( $shortdescription ) )
update_post_meta( $post_id, 'shortdescription', esc_attr( $shortdescription ) );
}
add_filter( 'woocommerce_add_cart_item_data', 'custom_product_field', 10, 3 );
function custom_product_field( $cart_item_data, $product_id, $variation_id ) {
$shortdesciption = get_post_meta( $product_id , 'shortdesciption' , true );
$company = get_post_meta( $product_id , 'company' , true );
if( !empty( $shortdesciption ) )
{
$cart_item_data['shortdesciption'] = $shortdesciption;
}
if( !empty( $company ) )
{
$cart_item_data['company'] = $company;
}
return $cart_item_data;
}
add_filter( 'woocommerce_get_cart_item_from_session', 'mbget_cart_item_from_session', 10, 3);
function mbget_cart_item_from_session( $cart_item_data, $cart_item_session_data, $cart_item_key ) {
if ( isset( $cart_item_session_data['shortdesciption'] ) ) {
$cart_item_data['shortdesciption'] = $cart_item_session_data['shortdesciption'];
}
if ( isset( $cart_item_session_data['company'] ) ) {
$cart_item_data['company'] = $cart_item_session_data['company'];
}
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 ) {
$data = array();
if( !empty( $data ) ) {
$data = $cart_data;
}
if( isset( $cart_item['shortdesciption'] ) ) {
$data[] = array(
'name' => __( 'Stuff', 'mbg' ),
'value' => $cart_item['shortdesciption'] );
}
if( isset( $cart_item['company'] ) ) {
$data[] = array(
'name' => __( 'Company', 'mbg' ),
'value' => $cart_item['company'] );
}
return $data;
}
Thanks
YES you should need to destroy the cart session or to remove all cart items when switching language. But this kind of case never really happen:
As it is NOT really a customer behavior, adding items in cart (for some language) and then switch to another language before checkout for example.
So this should not be really a problem. This is just the behavior of a developer that is testing an e-commmerce in all possible ways, isn't it?

Categories