Force post meta values according to product types

We're shifting our Forum based support to a more dedicated support system!

We'll be closing our Forum support from 10th June, 2020 and move to Email Support assistance.

  • If you are a WCFM premium add-ons user, contact us- here
  • Want to know more before buying our add-ons? Send Pre sale queries- here
  • If you are a WCFM free user, please open a support ticket at WordPress.org
  • For WCFM App related queries, reach us- here
From now the forum will be read-only!

Multi Vendor Marketplace Plugin | WCFM Marketplace Forums WCFM – Ultimate Force post meta values according to product types

Viewing 2 reply threads
  • Author
    Posts
    • #102343
      paattal
      Participant

      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

    • #103626
      paattal
      Participant

      Do you have a solution please?

    • #104645
      Sushobhan
      Keymaster

      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

Viewing 2 reply threads
  • You must be logged in to reply to this topic.