Multi Vendor Marketplace Plugin | WCFM Marketplace › Forums › WCFM – Ultimate › Force post meta values according to product types
- This topic has 2 replies, 2 voices, and was last updated 4 years, 2 months ago by Sushobhan.
Viewing 2 reply threads
- AuthorPosts
- January 16, 2020 at 11:29 pm #102343paattalParticipant
Hello,
I would like to force some default values according to the products types on new/update product.
I use the following code but I am missing the test condition on the product type:
add_action( 'woocommerce_new_product', 'mp_sync_on_product_save', 10, 4 ); add_action( 'woocommerce_update_product', 'mp_sync_on_product_save', 10, 4 ); function mp_sync_on_product_save( $product_id ) { $product = wc_get_product( $product_id ); /* if simple product */ update_post_meta($product_id,'meta'', 'value' ); ... /* if booking product */ update_post_meta($product_id,'meta'', 'value' ); ... }
But maybe there is a simpler solution.
Thank you
- January 24, 2020 at 6:28 pm #103626paattalParticipant
Do you have a solution please?
- January 30, 2020 at 1:03 pm #104645SushobhanKeymaster
Hello,
Thanks for contacting to us. You can try doing it the following way-function force_custom_product_meta_on_save( $product_id, $wcfm_products_manage_form_data ) { $product_type = empty( $wcfm_products_manage_form_data['product_type'] ) ? WC_Product_Factory::get_product_type( $product_id ) : sanitize_title( stripslashes( $wcfm_products_manage_form_data['product_type'] ) ); switch ( $product_type ) { //simple, variable, grouped, external, booking case 'simple': update_post_meta( $product_id, 'meta', 'value' ); break; case 'booking': update_post_meta( $product_id, 'meta', 'value' ); break; //... } } add_action( 'after_wcfm_products_manage_meta_save', 'force_custom_product_meta_on_save', 10, 2 );
Let me know how it goes.
Thanks
- AuthorPosts
Viewing 2 reply threads
- You must be logged in to reply to this topic.