limit title name number of caracters
-
Greetings
I tried to read the documentation on github but some how got stucked by my skills limitation it seems
https://github.com/woocommerce/woocommerce-paypal-payments/wiki/Actions-and-Filters
I have some problem with the product title displayed on my paypal message on my phone .. when title is too long then the product variation is not visible anymore (for exemple a tshirt with a long title, I can’t see the size)
is there a snipet to limite the number of letters of the title sent to paypal?
thank you in advance
-
if I want to limit to 29 caracters do you think the following will work?
function max_title_length( $title, $max ) {
if( strlen( $title ) > $max ) {
return substr( $title, 0, $max ). ” …”;
} else {
return $title;
}
}add_filter(‘wc_ppcp_get_order_item’, function($item, $order_item){
$item->setName($order_item->max_title_length(get_name(),29));
return $item;
}, 10, 2);thank you in advance
-
This reply was modified 11 months, 3 weeks ago by
steffykristiensen.
in the scenario I would rather change the title name by the sku, would the following work, I tried to adapt from here https://gist.github.com/InpsydeNiklas/bebf38d610dd3d928412c5ecde4925ee
add_filter('ppcp_create_order_request_body_data', function (array $data): array { foreach ($data['purchase_units'] as $index => $purchase_unit) { // extract order number from custom_id $order_number = isset($purchase_unit['custom_id']) ? $purchase_unit['custom_id'] : 'N/A'; foreach ($purchase_unit['items'] as $item_index => $item) { $data['purchase_units'][$index]['items'][$item_index]['name'] = $data['purchase_units'][$index]['items'][$item_index]['sku']; } } return $data; }); add_filter('ppcp_patch_order_request_body_data', function (array $patches_array): array { foreach ($patches_array as $patch_index => $patch) { if (isset($patch['value']['items'])) { // extract order number from custom_id $order_number = isset($patch['value']['custom_id']) ? $patch['value']['custom_id'] : 'N/A'; unset($patches_array[$patch_index]['value']['items'][$item_index]['sku']); $patches_array[$patch_index]['value']['items'][$item_index]['name'] = $patches_array[$patch_index]['value']['items'][$item_index]['sku']; } } } return $patches_array; });
thank you in advance
Hello @steffykristiensen
I didn’t test your filter; instead, I created my own. For testing purposes, I made a product named “1234567890,1234567890,1234567890,1234567890” which consists of 40 digits and 3 commas. I applied my filter to this product, and it successfully met the PayPal order details constraints as intended.
Order details title is limited to 29 characters.function limit_title_name_length( $patches_array ) { // Check if the 'shipping' key exists within the first element of $patches_array if ( isset( $patches_array[0]['value']['shipping']['name']['full_name'] ) ) { $original_full_name = $patches_array[0]['value']['shipping']['name']['full_name']; // Limit the 'full_name' to 29 characters if it's longer if (strlen($original_full_name) > 29) { $patches_array[0]['value']['shipping']['name']['full_name'] = substr($original_full_name, 0, 29); } } // Also ensure to limit each item's name to 29 characters if ( isset( $patches_array[0]['value']['items'] ) && is_array($patches_array[0]['value']['items']) ) { foreach ($patches_array[0]['value']['items'] as $key => $item) { if (strlen($item['name']) > 29) { $patches_array[0]['value']['items'][$key]['name'] = substr($item['name'], 0, 29); } } } return $patches_array; } add_filter( 'ppcp_patch_order_request_body_data', 'limit_title_name_length', 10 );
Keep in mind that this function is primarily designed to accommodate your needs, so feel free to modify it as necessary, but it should work as requested.
Please feel free to reach out if you have any more questions or need further assistance!
Kind Regards,
Krystian
Hi Krystian
Thank you very very much for your kind assistance…
I will review your code to learn from it and see how it is different with what I came with..Thank you again
what about the part to replace the name by the sku? is my adaptation of your code correct?
thank you very much in advance
Steffy
Hello?@steffykristiensen
what about the part to replace the name by the sku? is my adaptation of your code correct?
Please provide all the specific details and requirements for your order details (besides this one with 29 characters limit), and we’ll try to share some examples.
what about the part to replace the name by the sku? is my adaptation of your code correct?
I recommend checking everything first with a sandbox account to ensure it works properly, just as I did during my preliminary tests.
Kind regards,
KrystianHi Krystian,
thanks again for your help, on this one I don’t try to shorten the name but to replace it by the sku
here is the code I have modified
add_filter('ppcp_create_order_request_body_data', function (array $data): array { foreach ($data['purchase_units'] as $index => $purchase_unit) { // extract order number from custom_id $order_number = isset($purchase_unit['custom_id']) ? $purchase_unit['custom_id'] : 'N/A'; foreach ($purchase_unit['items'] as $item_index => $item) { $data['purchase_units'][$index]['items'][$item_index]['name'] = $data['purchase_units'][$index]['items'][$item_index]['sku']; } } return $data; }); add_filter('ppcp_patch_order_request_body_data', function (array $patches_array): array { foreach ($patches_array as $patch_index => $patch) { if (isset($patch['value']['items'])) { // extract order number from custom_id $order_number = isset($patch['value']['custom_id']) ? $patch['value']['custom_id'] : 'N/A'; unset($patches_array[$patch_index]['value']['items'][$item_index]['sku']); $patches_array[$patch_index]['value']['items'][$item_index]['name'] = $patches_array[$patch_index]['value']['items'][$item_index]['sku']; } } } return $patches_array; });
is it possible to have a sand box account used only for admin? my problem is the site is live, what would be the best for test purpose on paypal?
thank you again
Steffy
Hello?@steffykristiensen
On my end, your version triggered a syntax error.
This is a code review from openAI. When I used the version corrected by GPT it worked as intended:
PayPal order details:
Product details:
is it possible to have a sand box account used only for admin? my problem is the site is live, what would be the best for test purpose on paypal?
No, this scenario is not possible. Setting up a staging site is the best practice for testing new code changes without affecting your live environment.
Kind regards,
KrystianHi Krystian
Thank you again for your support, the 29 character worked like a charm
I will try to set up a staging site and test the sku version.. did not thought of using chatgpt for testing that
thank you again, have a good day
Steffy
Hello?@steffykristiensen
Thank you again for your support, the 29 character worked like a charm
Thank you for your feedback. I’m glad that that filter worked perfectly for you. Please feel free to reach out if you need any more help.
Have a great weekend too!
Kind regards,
Krystian -
This reply was modified 11 months, 3 weeks ago by
- The topic ‘limit title name number of caracters’ is closed to new replies.