Skip Navigation

[Resolved] Auto insert or save value custom field

This support ticket is created 5 years, 3 months 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 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

Tagged: 

This topic contains 13 replies, has 2 voices.

Last updated by jelle-jacobd 5 years, 2 months ago.

Assisted by: Shane.

Author
Posts
#1318057

Hi I would like to automatically save a value for a cf when a product is created or updated. Is this possible? Now I need to manually save the products to get the auto value for this cf saved with the product.

Thanks.

#1318099

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Jelle,

Thank you for getting in touch.

Are the products created through the backend or are they dont through the frontend?

For the backend you can hook into the post save action using this hook here
https://codex.wordpress.org/Plugin_API/Action_Reference/save_post

This also has an example of how to update the custom field using the update_post_meta() function.

Please let me know if this helps.
Thanks,
Shane

#1318245

Hi Shane, thanks for your response. The products are created/imported with WP ALL import. I just can't put this link:

[woo_sku]

in here that I need for a specific link to get the SKU field linkable as I can't insert a custom link in the Products Tabel from Barn2.co.uk.That's why I need to solve this by putting the link in a custom field as far as I know at least.

https://toolset.com/2017/06/wordpress-table-plugins-integration-with-toolset-types/

The WP All import fails on the '$' sign when including the link in the import, which would be the easiest way to go with. That is why I thought I could solve it by having the value saved automatically. I have looked at the link, but I am not sure. I simply have too little experience with PHP to get this done. Hope you point me in the right direction. Thanks.

#1319107

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Jelle,

Because of the new relationship method, this actually won't work in the link [wpv-post-id id=“$parent”]

Is the parent in another custom post type ?

If so then your selector for the parent data would look something like [wpv-post-id item='@manufacturer-car.parent'] where manufacturer-car in my case is the relationship slug.

Please let me know if this helps.

Thanks,
Shane

#1319175

Hi Shane
Thanks for your comment. I do not use the link for a CPT or relation. I use the link to show extra information for a WooCommerce product variation. Please check the link below for a live demo. De SKU in the variation table links to a page with extra product variation info called a datasheet. This is where the link is used for. It filters the custom view on these 2 values (product_id & variation_id)

The plugin form the people at Barn2 works really nice, but it doesn't allow me to add a custom link. That's why i've created a CF with the link. But this link doesn't get saved automatically when a product is created or updated by import.

hidden link

Thanks.

#1319213

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Jelle,

So you essentially want populate the custom field when the post is being created.

I'm still a little confused about the use of [wpv-post-id id=“$parent”] , this to me suggest there is a Post relationship here. That you are trying to get the Parent ID of that post.

I need some clarity on this before we can proceed.

Thanks,
Shane

#1319235

Hi Shane,

Yes that's correct. Maybe the following threat makes more sense about why the link is created this way. Luo helped me to set it up like this.

https://toolset.com/forums/topic/filter-view-with-variation-post-id/

#1319521

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Jelle,

I think I have a pretty good grasp of the issue and the context here now.

So first I noticed some formatting issue with your url that you want to import.

<a href="/datasheet/?product_id=[wpv-post-id id='$parent']&variation_id=[wpv-post-id]">[woo_sku]</a>

Notice I change to the use of "" single quotes around the $parent. You can try importing it with the links like this to see if it works. The other alternative is to contact the WP all import plugin team to see why the $ is breaking the import.

Any other solution that I can provide will require the manual creation of the products where I would use the save post hook to populate the custom field.

Please let me know.
Thanks,
Shane

#1320061
Schermafdruk 2019-08-20 12.27.58.png

Hi Shane,

Thanks. Unfortunately WP All Import doesn't allow single quotes. Same goes for the $ sign. I do have an option to execute a function form within the import. Could this be something we could use?

#1322179

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Jelle,

This might be able to work but not sure how this editor here works.

Perhaps you can ask the WP All Import team if their import fires the save_post hook ? If it does then we can write a hook here that will hook into the save post to populate the field for you.

Thanks,
Shane

#1324827

Hi Shane,

Thanks. Sounds good:) the people at WP All Import confirm that the save_post hook is fired.

Best Jelle

#1325279

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Jelle,

Ok so would you mind allowing me to have access to the website as well as the export file ?

This will get a little technical because I need to know what the export file looks like and what information is being passed in the export.

I should note that I cannot guarantee that I can resolve this issue for you but I can try.

Thanks,
Shane

#1329715

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Jelle,

Try using this hook here. I did some re-reading of you post and I believe the solution would be.

function update_my_post( $post_id ) {

update_post_meta($post_id,"wpcf-sku","<a href="/datasheet/?product_id=[wpv-post-id id='$parent']&variation_id=[wpv-post-id]" target="_blank">[woo_sku]</a>");
}
add_action( 'save_post', 'update_my_post' );

Please try this and let me know if it helps.
Given that the mentioned information that the WP All imports team says the php hooks are fired here then this should work.

Thanks,
Shane

#1335329
Schermafdruk 2019-09-09 16.24.15.png

Hi Shane,

Sorry wasn't able to respond for a while. Thanks a lot. Unfortunately the script throws an error like shown in the image. According to Google this could be due to an old PHP version (below 7.x) on my webserver. I have checked and it 7.3.5, so this seems to be oke. What should I do?

Thanks.