Force post meta values according to product types

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

This topic contains 2 replies, has 2 voices, and was last updated by Avatar Sushobhan 2 weeks, 6 days ago.

  • Author
    Posts
  • #102343
    Avatar
    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
    Avatar
    paattal
    Participant

    Do you have a solution please?

  • #104645
    Avatar
    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

You must be logged in to reply to this topic.