Skip Navigation

[Resolved] Extend product name on submission of the form

This support ticket is created 2 years, 1 month ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 3 replies, has 2 voices.

Last updated by Waqar 2 years ago.

Assisted by: Waqar.

Author
Posts
#2484679

Hi there,

I want to extend the productname with the sku on front end product submission of the form.
Productname must be set to sku + space + productname.
sku is randomly generated by the plugin Auto sku generator.
There is alreeady custom code availble for setting the regular price. I don't know if that can be extended.
Thx in advance for your help.

Regards,
Loek

#2485955

Hi Loek,

Thank you for contacting us and I'd be happy to assist.

If you're using a Toolset Form to create a new product from the front end, I'll assume that your custom code for setting the regular price is using the hook like "cred_save_data":
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

If that is correct, you can extend the same function to get the 'SKU' value from the created product and update the post title, accordingly.

1. Not sure how the Auto sku generator plugin works, but WooCommerce by default stores the SKU in the custom field with the key '_sku'.

2. To get the existing custom field value(s) you can use the "get_post_meta" function and to update the post title and slug, you can use the "wp_update_post" function:
https://developer.wordpress.org/reference/functions/get_post_meta/
https://developer.wordpress.org/reference/functions/wp_update_post/

I hope this helps and please let me know if you need any further assistance with this.

regards,
Waqar

#2485977

Hi Waqar,
Thx for quick response.
Is it for you a problem to extend the custom code for this?
I cannot code at all.
My WordPress username is LJVDLINDEN and my password is Lhp4365!lj5411
I would be very pleased if you can do this for me.
Many thx in advance

#2486907

Thanks for writing back.

Although 1-1 code customization assistance is beyond the scope of support that we can provide over the support forum, we do our best to guide you in the right direction whenever possible.

I see your website has the following custom code, to update the product price when the form is submitted:


function func_custom_update_product_regular_price($post_id, $form_data) {
// if a specific form
if ($form_data['id']==15) {
update_post_meta($post_id,'_regular_price', 50);
update_post_meta($post_id,'_price', 50);
}
}
add_action('cred_save_data', 'func_custom_update_product_regular_price', 10, 2);

In theory, extending that code to the following should work to update the product's title and slug to also include the "SKU" at the beginning:


function func_custom_update_product_regular_price($post_id, $form_data) {
	// if a specific form
	if ($form_data['id']==15) {
		update_post_meta($post_id,'_regular_price', 50);
		update_post_meta($post_id,'_price', 50);

		$sku = get_post_meta($post_id, '_sku', true);

		$title  = get_the_title($post_id);
		$new_title = $sku.' '.$title;
		$slug   = sanitize_title($new_title);

		$my_post_data = array(
			'ID'            => $post_id,
			'post_title'    => $new_title,
			'post_name'     => $slug,
		);

		// Update the post into the database
		wp_update_post( $my_post_data );
	}
}
add_action('cred_save_data', 'func_custom_update_product_regular_price', 10, 2);

However, there is a limitation on the part of the "Easy Auto SKU Generator for WooCommerce" plugin. It can only generate and update the product 'SKU' if the product is created from the post-edit screen in the admin area. In this case, where you're using a front-end form to generate the product, it doesn't generate and save the 'SKU' value, which is why it may look like the code is not working.
(the actual reason will be that the 'SKU' value is empty when the product is generated through the form)

To overcome this limitation, you can either:

1. Get in touch with the "Easy Auto SKU Generator for WooCommerce" plugin's support to see if it can be made to generate SKU, no matter how a product is created.

OR

2. You can switch this plugin with a different one that generates the 'SKU', irrespective of how a product is created.

Note: The custom code examples from our forum are shared to get you started in the right direction. You're welcome to adjust them as needed and for more personalized customization assistance, you can consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/