I need to append a text string to the order title when an order is placed in Woocommerce.
In order to send extra order data to our accounting software (Sage), I am using a plugin; Woosage as the bridge. Woosage requires this extra data to be appended to the title of the product.
I suspect I need to hook into the woocommerce_checkout_create_order.
Attached is what I am trying to achieve (apparently this site doesn't include my attachments).
<img src="https://i.imgur.com/EXG8Ijb.png">
Arnav Joy answers:
Can you please explain what extra info you want to add?
Keith Donegan comments:
Hi Amav,
Just some text (this is not important).
I need to this to happen after a customer clicks 'place order' on checkout: https://i.imgur.com/EXG8Ijb.png
Cesar Contreras answers:
use el gancho de woocommerce para cuando se realiza el pedido y allí realiza la modificación que necesita
Mohamed Ahmed answers:
Hello Keith,
This code will do the trick (tested and works)
The result : https://prnt.sc/yom2hx
function ProWPsite_output_elDigital_field()
{
global $product;
/* You can enable this in a custom product only (if you want to track a product campaign directly) */
/*
if ($product->get_id() !== 282) {
return;
}
*/
?>
<div class="ProWPsite-elDigital-field">
<input type="hidden" id="ProWPsite-elDigital" name="ProWPsite-elDigital" value="." maxlength="20">
</div>
<?php
}
add_action('woocommerce_before_add_to_cart_button', 'ProWPsite_output_elDigital_field', 10);
function ProWPsite_add_elDigital_text_to_cart_item($cart_item_data, $product_id, $variation_id)
{
$elDigital_text = filter_input(INPUT_POST, 'ProWPsite-elDigital');
if (empty($elDigital_text)) {
return $cart_item_data;
}
$cart_item_data['ProWPsite-elDigital'] = $elDigital_text;
return $cart_item_data;
}
add_filter('woocommerce_add_cart_item_data', 'ProWPsite_add_elDigital_text_to_cart_item', 10, 3);
function ProWPsite_add_elDigital_text_to_order_items($item, $cart_item_key, $values, $order)
{
/* Change your custom text */
$your_custom_text = 'Append this text';
if (empty($values['ProWPsite-elDigital'])) {
return;
}
$item->add_meta_data(' ', $your_custom_text);
}
add_action('woocommerce_checkout_create_order_line_item', 'ProWPsite_add_elDigital_text_to_order_items', 10, 4);
Regards
Naveen Chand answers:
If you want to append text to "Product" title, then use this filter:
add_filter( 'woocommerce_order_item_name', 'append_string_order_item_name', 10, 1 );
function append_string_order_item_name( $itemname ) {
$new_itemname = 'mytextString' .$itemname;
return $new_itemname;
}
However, if you are looking to edit the Order title (and not the product title), then you can use this filter:
add_filter( 'woocommerce_order_number', 'append_string_order_number' );
function append_string_order_number( $order_id ) {
$prefix = 'mytextString';
$new_order_id = $prefix . $order_id;
return $new_order_id;
}
Does this solve your problem?
Keith Donegan comments:
Hi Naveen,
But this won't happen when an order is placed?
I need to dynamically add text when a customer clicks the place order button.
Naveen Chand comments:
Hi Keith,
May be you should use the filter: 'wocoomerce_before_calculate_totals', and then you can alter the product name in Cart, Checkout Page and Order Details page in the backend.
add_action( 'woocommerce_before_calculate_totals', 'append_string_order_item_name', 10, 1 );
function append_string_order_item_name( $cart ) {
foreach ( $cart->get_cart() as $cart_item ) {
// Get an instance of the WC_Product object
$product = $cart_item['data'];
// Get the product name (compatible with Woocommerce 3+)
$original_name = method_exists( $product, 'get_name' ) ? $product->get_name() : $product->post->post_title;
// SET THE NEW NAME
$suffix = 'mytextString';
$new_name = $original_name .$suffix;
// Set the new name (WooCommerce versions 2.5.x to 3+)
if( method_exists( $product, 'set_name' ) )
$product->set_name( $new_name );
else
$product->post->post_title = $new_name;
}
}
Keith Donegan comments:
This looks good, I'll, test when I get home, thanks.
Echeverri answers:
you will use some static or dynamic text, it requires that in the database it be saved with the name you need?
Ali mosbah answers:
You must added the input field and send it's value to append it to the order title