ResolvedMinimum Number of Selectable Options

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #23729
    mediamix
    Participant

    Hi
    Is possible to add “Minimum Number of Selectable Options” in Lafka Global Add-on?

    #23899
    Althemist
    Keymaster

    Hello mediamix,

    No, I am afraid it’s currently not possible. You can only set a max number of options, but I will discuss with the team if we can add a minimum too. Can’t promise, though.

    #23907
    mediamix
    Participant

    Hi guys,
    i have add this snippet into my child theme

    
    add_action( 'woocommerce_validate_posted_addon_data', 'min_limit_validate');
    function min_limit_validate ( $addon ) {
    	
    	 
    	if ( isset( $_POST ) ) {
    		$post_data = $_POST;
    	} else {
    		return true;
    	}
    	
    	if (!isset($addon['minlimit'])) {
    		return true;
    	}
    	
    	$value = isset( $post_data[ 'addon-' . $addon['field-name'] ] ) ? $post_data[ 'addon-' . $addon['field-name'] ] : '';
    	
    	if (count($value) < $addon['minlimit']) {
    		error_log (print_r($addon,true));
    		wc_add_notice( 'Select minimum ' . $addon['minlimit'] . ' elements from ' . $addon['name'], 'error' );
    		return false;
    	} else {
    		return true;
    	}
    		
    }
    add_action( 'lafka_product_addons_panel_before_options', 'add_minimum_selectable_options_field', 10, 3) ;
    function add_minimum_selectable_options_field ($post, $addon, $loop ) {
    ?>
    	<tr>
    		<td class="addon_minlimit">
    			<label for="addon_minlimit_<?php echo $loop; ?>"><?php esc_html_e( 'Minimum Number of Selectable Options', 'lafka-plugin' ); ?></label>
    		</td>
    		<td class="addon_minlimit">
    			<label for="addon_minlimit_<?php echo $loop; ?>">
    				<input type="number" id="addon_minlimit_<?php echo $loop; ?>" name="product_addon_minlimit[<?php echo $loop; ?>]" value="<?php echo isset($addon['minlimit']) ? esc_attr( $addon['minlimit'] ) : '' ?>" />
    			</label>
    		</td>
    	</tr>
    <?php
    }
    add_filter( 'lafka_product_addons_save_data', 'filter_lafka_product_addons_save_data', 10, 2);
    function filter_lafka_product_addons_save_data($data, $i ) {
    	$addon_minlimit         = $_POST['product_addon_minlimit'];
    	$data['minlimit'] = sanitize_text_field( stripslashes( $addon_minlimit[ $i ] ) );
    	return $data;
    }
Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in and have valid license to reply to this topic.

License required for the following item
Login and Registration Log in · Register