I have a created a simple gift wrapping plugin, which adds a gift wrap product to the user cart and when they enter a message it adds it as meta data to the line item, this is fine for the woocommerce system, but our backoffice needs this meta data to be on the order and not the line item, is there a way I can add the data to the order also?
function checkout_create_order_line_item($item, $cart_item_key, $values, $order)
{
if (isset($values['_gift_wrap_message'])) {
$item->add_meta_data(__('Your message', '_gift_wrap_message'), $values['_gift_wrap_message'], true);
}
}
add_action('woocommerce_checkout_create_order_line_item', 'checkout_create_order_line_item', 10, 4);
I have the above and and I have tried the folllowing,
function checkout_create_order_line_item($item, $cart_item_key, $values, $order)
{
if (isset($values['_gift_wrap_message'])) {
$item->add_meta_data(__('Your message', '_gift_wrap_message'), $values['_gift_wrap_message'], true);
$order->update_meta_data('_gift_wrap_message', $values['_gift_wrap_message');
$order->save();
}
}
add_action('woocommerce_checkout_create_order_line_item', 'checkout_create_order_line_item', 10, 4);
But this does not work. I would really apprieciate some guidance as to how to get $values['_gift_wrap_message']
(or at least it's contents) into the order meta data
CodePudding user response:
For any dynamic order line meta, you should follow 3 steps
1) Add custom meta fields on the checkout page from where customers can pass their message like this -
/**
* Add a custom field to the checkout page
*/
add_action('woocommerce_after_order_notes', 'custom_gift_wrap_message_field');
function custom_gift_wrap_message_field($checkout)
{
echo '<div id="custom_gift_wrap_message_field"><h2>' . __('Gift Message') . '</h2>';
woocommerce_form_field('_gift_wrap_message', array(
'type' => 'text',
'class' => array(
'my-field-class form-row-wide'
),
'label' => __('Custom Additional Field') ,
'placeholder' => __('Write your message here...') ,
),
$checkout->get_value('_gift_wrap_message'));
echo '</div>';
}
2) Save your custom field data when placing an order from the checkout page
/**
* Save your checkout page custom field value
*/
add_action('woocommerce_checkout_update_order_meta', 'custom_checkout_field_update_order_meta', 20, 2);
function custom_checkout_field_update_order_meta( $order_id ) {
update_post_meta($order_id, '_gift_wrap_message', sanitize_text_field($_POST['_gift_wrap_message']) );
}
3) Fetch or display your custom field value
/**
* You can get your gift message directly anywhere from $order_id like this -
*/
<?php echo get_post_meta( $order_id, '_gift_wrap_message', true ); ?>
Example: Show custom fields value on the welcome page (after checkout page)
/**
* Display Custom Checkout Fields Data on Thankyou page
*/
function gift_wrap_message_display_order_data( $order_id ){ ?>
<table >
<tbody>
<tr>
<th><?php _e( 'Your Gift Message:' ); ?></th>
<td><?php echo get_post_meta( $order_id, '_gift_wrap_message', true ); ?></td>
</tr>
</tbody>
</table>
<?php }
add_action( 'woocommerce_thankyou', 'gift_wrap_message_display_order_data', 20 );
Add the above code to your currently active theme functions.php
Complete refrence URL is - https://www.cloudways.com/blog/how-to-edit-delete-fields-and-email-in-woocommerce-custom-checkout-fields/
CodePudding user response:
Thank you for your help. I have 4 custom fields: custom_question_field, custom_question_text_codice_fiscale, custom_question_text_p_iva, custom_question_text_ragione_sociale.
The customer via radiobutton chooses an option. The tax code fields are ticked if option 1 is chosen, otherwise the VAT number and company name are ticked. is it possible to set the code to show only the calpi that are compiled and not all?