Multi Vendor Marketplace Plugin | WCFM Marketplace › Forums › WCFM – Ultimate › Custom Simple Type Options
- This topic has 13 replies, 2 voices, and was last updated 5 years ago by SSC.
- AuthorPosts
- November 8, 2019 at 8:20 pm #91447SSCParticipant
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. - November 11, 2019 at 5:17 pm #91806SushobhanKeymaster
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 );
- November 11, 2019 at 7:06 pm #91839SSCParticipant
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. - November 12, 2019 at 1:09 am #91869SSCParticipant
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. - November 12, 2019 at 6:40 pm #91996SushobhanKeymaster
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.
- November 12, 2019 at 8:01 pm #92009SSCParticipant
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 );
- November 13, 2019 at 7:12 pm #92178SushobhanKeymaster
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.
- November 15, 2019 at 8:26 am #92407SSCParticipant
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. - November 15, 2019 at 6:50 pm #92448SushobhanKeymaster
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 - November 15, 2019 at 9:43 pm #92487SSCParticipant
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);
- November 16, 2019 at 11:31 am #92538SushobhanKeymaster
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
- November 17, 2019 at 6:27 pm #92657SSCParticipant
Makes sense x) I really confused myself adding different types of fields. Thank you for the help
- November 18, 2019 at 4:58 pm #92751SushobhanKeymaster
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.
- November 19, 2019 at 8:32 pm #92987SSCParticipant
Certainly. Thank you
- AuthorPosts
- The topic ‘Custom Simple Type Options’ is closed to new replies.