Custom Simple Type Options

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

  • Author
    Posts
  • #91447
    Avatar
    SSC
    Participant

    Hello,
    Do you have a solution to add custom options to the simple product type?
    For example, by default, there is: Virtual and Downloadable.
    I would like to add another.
    I have tried making changes to the product-manage file. The checkbox I add either only shows on the admin side or does not save the value, when it’s on the wcfm side.

  • #91806
    Avatar
    Sushobhan
    Keymaster

    For this, you need to first add the element using ‘wcfm_product_manage_fields_general’ filter, which I think you already figured out. For save use this hook – do_action( ‘after_wcfm_products_manage_meta_save’, $new_product_id, $wcfm_products_manage_form_data );

  • #91839
    Avatar
    SSC
    Participant

    Hmm, I seem to be doing something wrong xD
    That does seem to be what I needed. I’m having problems with my code now though =/ I just copied what I already made back, and now it’s not even working on the back-end.
    I’ll figure it out. Thank you for the info.

  • #91869
    Avatar
    SSC
    Participant

    I solved my issue, but I am still having problems getting it to save or show properly.
    When I use the ‘wcfm_product_manage_fields_general’ hook, it does not add the option to WCFM.
    When I hardcode the option into the form, the ‘after_wcfm_products_manage_meta_save’ hook does not trigger.

  • #91996
    Avatar
    Sushobhan
    Keymaster

    Check the sample code below. Add this code to your child theme’s functions.php
    In case you do not have child theme then add code using this plugin – https://wordpress.org/plugins/code-snippets/

    function add_new_general_field($fields, $product_id, $product_type, $wcfm_is_translated_product, $wcfm_wpml_edit_disable_element) {
        $is_new_val = ( get_post_meta( $product_id, '_is_new_val', true) == 'yes' ) ? 'enable' : '';
        $new_field = array("is_new_val" => array('desc' => __('New Value', 'wc-frontend-manager') , 'type' => 'checkbox', 'class' => 'wcfm-checkbox wcfm_ele wcfm_half_ele_checkbox simple non-booking non-variable-subscription non-job_package non-resume_package non-redq_rental non-accommodation-booking' . ' ' . $wcfm_wpml_edit_disable_element, 'desc_class' => 'wcfm_title wcfm_ele virtual_ele_title checkbox_title simple non-booking non-variable-subscription non-job_package non-resume_package non-redq_rental non-accommodation-booking' . ' ' . $wcfm_wpml_edit_disable_element, 'value' => 'enable', 'dfvalue' => $is_new_val));
        $fields = array_slice($fields, 0, 1, true) + $new_field + array_slice($fields, 1, count($fields) - 1, true);
        return $fields;
    }
    
    add_filter('wcfm_product_manage_fields_general', 'add_new_general_field', 9, 5);
    
    function save_new_field_value($new_product_id, $wcfm_products_manage_form_data) {
        $is_new_val = !empty( $wcfm_products_manage_form_data['is_new_val'] ) ? 'yes' : 'no';
        update_post_meta( $new_product_id, '_is_new_val', $is_new_val );
    }
    add_action('after_wcfm_products_manage_meta_save', 'save_new_field_value', 10, 2);

    Kindly, made the changes as per your requirement before using it on production environment.

  • #92009
    Avatar
    SSC
    Participant

    Thank you so much, That code does give errors x) But it helped me figure out my problem.

    Fatal error: Uncaught ArgumentCountError: Too few arguments to function add_new_general_field(), 3 passed in /var...... on line *** and exactly 5 expected in

    Just removing ‘$wcfm_is_translated_product, $wcfm_wpml_edit_disable_elementg’ fixes that problem, not sure if it may cause issues later on x) but they seem like un-needed arguments.

    I seem to be missing something to add it to the capability controller though.
    I have added some restrictions for product types earlier, and they work fine. I used pretty much the same code with slight changes, but it’s not working for me.

    add_filter( 'wcfm_capability_settings_fields_product_types', function( $fields, $wcfm_capability_options = array() ) {
    $wcfm_capability_options = get_option( 'wcfm_capability_options', array() );
    $is_new_val = ( isset( $wcfm_capability_options['is_new_val'] ) ) ? $wcfm_capability_options['is_new_val'] : 'no';
    $fields['is_new_val'] = array('label' => __('is_new_val', 'wc-frontend-manager') , 'name' => 'wcfm_capability_options[is_new_val]','type' => 'checkboxoffon', 'class' => 'wcfm-checkbox wcfm_ele', 'value' => 'yes', 'label_class' => 'wcfm_title checkbox_title', 'dfvalue' => $is_new_val );
    return $fields;
    }, 60, 3 );
    
    function _allow_new_general_field( $fields, $wcfm_capability_options = array() ) {
    $wcfm_capability_options = get_option( 'wcfm_capability_options', array() );
    $is_new_val = ( isset( $wcfm_capability_options['is_new_val'] ) ) ? $wcfm_capability_options['is_new_val'] : 'no';
    if( $is_new_val == 'yes' ) unset( $fields[ 'is_new_val' ] );
    
    return $fields;
    }
    add_filter( 'wcfm_product_types', '_allow_new_general_field', 700 );
  • #92178
    Avatar
    Sushobhan
    Keymaster

    Hello,
    The filter ‘wcfm_product_manage_fields_general’ sends 5 arguments to its callback function. The error can only appear if you pass the last argument as 3 instead of 5.
    add_filter(‘wcfm_product_manage_fields_general’, ‘add_new_general_field’, 9, 5);
    The remaining two arguments are used for translation purposes and if you are not using WPML you can safely omit those.

    The code you shares seems okay, what error are you getting? Is it related to your first query? Please explain the whole purpose.

  • #92407
    Avatar
    SSC
    Participant

    I managed to fix my original problem with the meta_save hook .
    The code I sent, should have it work with the capability settings. I believe.
    It’s pretty much the same method I used for product types. But it doesn’t seem to be working for the checkbox. There are no errors.

  • #92448
    Avatar
    Sushobhan
    Keymaster

    Hi,
    Glad it worked out. Coming to the next section, what problem are you facing? Is it not showing under capability page? Or is the value not saving? I might need the full code along with what you want to achieve, to help you out.
    Thanks

  • #92487
    Avatar
    SSC
    Participant

    It does show on the add new product page. It does show on the capability options. It does save on both the product page and capability page. But the problem is that it still shows on the add new product page when they do not have the capability to.

    //Capability
    add_filter( 'wcfm_capability_settings_fields_product_types', function( $fields, $wcfm_capability_options = array() ) {
    $wcfm_capability_options = get_option( 'wcfm_capability_options', array() );
    $is_new_val = ( isset( $wcfm_capability_options['is_new_val'] ) ) ? $wcfm_capability_options['is_new_val'] : 'no';
    $fields['is_new_val'] = array('label' => __('is_new_val', 'wc-frontend-manager') , 'name' => 'wcfm_capability_options[is_new_val]','type' => 'checkboxoffon', 'class' => 'wcfm-checkbox wcfm_ele', 'value' => 'yes', 'label_class' => 'wcfm_title checkbox_title', 'dfvalue' => $is_new_val );
    return $fields;
    }, 60, 3 );
    
    function _allow_new_general_field( $fields, $wcfm_capability_options = array() ) {
    $wcfm_capability_options = get_option( 'wcfm_capability_options', array() );
    $is_new_val = ( isset( $wcfm_capability_options['is_new_val'] ) ) ? $wcfm_capability_options['is_new_val'] : 'no';
    if( $is_new_val == 'yes' ) unset( $fields[ 'is_new_val' ] );
    
    return $fields;
    }
    add_filter( 'wcfm_product_types', '_allow_new_general_field', 700 );
    //Meta Save
    function add_new_general_field($fields, $product_id, $product_type) {
        $is_new_val = ( get_post_meta( $product_id, '_is_new_val', true) == 'yes' ) ? 'enable' : '';
        $new_field = array("is_new_val" => array('desc' => __('New Value', 'wc-frontend-manager') , 'type' => 'checkbox', 'class' => 'wcfm-checkbox wcfm_ele wcfm_half_ele_checkbox simple non-booking non-variable-subscription non-job_package non-resume_package non-redq_rental non-accommodation-booking' . ' ', 'desc_class' => 'wcfm_title wcfm_ele virtual_ele_title checkbox_title simple non-booking non-variable-subscription non-job_package non-resume_package non-redq_rental non-accommodation-booking' . ' ', 'value' => 'enable', 'dfvalue' => $is_new_val));
        $fields = array_slice($fields, 0, 1, true) + $new_field + array_slice($fields, 1, count($fields) - 1, true);
        return $fields;
    }
    
    add_filter('wcfm_product_manage_fields_general', 'add_new_general_field', 9, 3);
    
    function save_new_field_value($new_product_id, $wcfm_products_manage_form_data) {
        $is_new_val = !empty( $wcfm_products_manage_form_data['is_new_val'] ) ? 'yes' : 'no';
        update_post_meta( $new_product_id, '_is_new_val', $is_new_val );
    }
    add_action('after_wcfm_products_manage_meta_save', 'save_new_field_value', 10, 2);
  • #92538
    Avatar
    Sushobhan
    Keymaster

    The hook ‘wcfm_product_types’ is inappropriate in your case as the new field we are adding here is not a “Product Type” like Simple, Variable, etc. You will not need that code block. Instead modify ‘wcfm_product_manage_fields_general’ hook function in such a way that it consider the capability setting. Please refer to the code below

    function add_new_general_field( $fields, $product_id, $product_type, $wcfm_is_translated_product, $wcfm_wpml_edit_disable_element ) {
        $wcfm_capability_options = get_option( 'wcfm_capability_options', array() );
        $is_new_val_cap = ( isset( $wcfm_capability_options['is_new_val'] ) ) ? $wcfm_capability_options['is_new_val'] : 'no';
        if($is_new_val_cap=='yes') return $fields;
        
        $is_new_val = ( get_post_meta( $product_id, '_is_new_val', true ) == 'yes' ) ? 'enable' : '';
        $new_field = array( "is_new_val" => array( 'desc' => __( 'New Value', 'wc-frontend-manager' ), 'type' => 'checkbox', 'class' => 'wcfm-checkbox wcfm_ele wcfm_half_ele_checkbox simple non-booking non-variable-subscription non-job_package non-resume_package non-redq_rental non-accommodation-booking' . ' ' . $wcfm_wpml_edit_disable_element, 'desc_class' => 'wcfm_title wcfm_ele virtual_ele_title checkbox_title simple non-booking non-variable-subscription non-job_package non-resume_package non-redq_rental non-accommodation-booking' . ' ' . $wcfm_wpml_edit_disable_element, 'value' => 'enable', 'dfvalue' => $is_new_val ) );
        return array_slice( $fields, 0, 1, true ) + $new_field + array_slice( $fields, 1, count( $fields ) - 1, true );
    }
    
    add_filter( 'wcfm_product_manage_fields_general', 'add_new_general_field', 9, 5 );
    
    function save_new_field_value( $new_product_id, $wcfm_products_manage_form_data ) {
        $is_new_val = ! empty( $wcfm_products_manage_form_data['is_new_val'] ) ? 'yes' : 'no';
        update_post_meta( $new_product_id, '_is_new_val', $is_new_val );
    }
    
    add_action( 'after_wcfm_products_manage_meta_save', 'save_new_field_value', 10, 2 );
    
    function wcfmcap_new_field_product_types( $product_types, $handler = 'wcfm_capability_options', $wcfm_capability_options = array() ) {
        $new_field = ( isset( $wcfm_capability_options['is_new_val'] ) ) ? $wcfm_capability_options['is_new_val'] : 'no';
        $product_types["is_new_val"] = array( 'label' => __( 'New Value', 'wc-frontend-manager' ), 'name' => $handler . '[is_new_val]', 'type' => 'checkboxoffon', 'class' => 'wcfm-checkbox wcfm_ele', 'value' => 'yes', 'label_class' => 'wcfm_title checkbox_title', 'dfvalue' => $new_field );
        return $product_types;
    }
    
    add_filter( 'wcfm_capability_settings_fields_product_types', 'wcfmcap_new_field_product_types', 60, 3 );

    Thanks

  • #92657
    Avatar
    SSC
    Participant

    Makes sense x) I really confused myself adding different types of fields. Thank you for the help

  • #92751
    Avatar
    Sushobhan
    Keymaster

    You are always welcome 🙂

    Let me know if there’s anything else we can help you with.

    Can we ask for a favor? Would you mind taking a few minutes to review our plugin at https://wordpress.org/support/plugin/wc-multivendor-marketplace/reviews/ and let others know about your 5 Star experience with WCFM Marketplace. Also, follow us on Twitter https://twitter.com/wcfmmp for more exciting news, important updates, and irresistible offers.

  • #92987
    Avatar
    SSC
    Participant

    Certainly. Thank you

The topic ‘Custom Simple Type Options’ is closed to new replies.