• Resolved amalksuresh

    (@amalksuresh)


    Hi,
    We need to add an option to sort products based on a specified value or weight value. so that we can arrange products based on a particular category, tag, brand as per custom sort order. Currently, we need to drag and drop products for specified listing but in the case of thousands of products, it’s very difficult. so can you suggest how I can add menu order of a product in bulk product edit? so product menu order will update on bulk product edit.

    The page I need help with: [log in to see the link]

Viewing 4 replies - 1 through 4 (of 4 total)
  • Hello @amalksuresh ,

    I understand that managing the order of products is a difficult task when you have a large number of products.

    I could not locate a core feature or a plugin that allows you to sort the products in bulk. However, you can try out the product import feature to bulk update the menu order of the products. In the exported CSV file, you have a “position” column that can be used to define the order of the products.

    More details on – Product CSV Importer and Exporter

    Thank you ??

    Thread Starter amalksuresh

    (@amalksuresh)

    edit menu order by specicing weight value

    // Add product new column in administration
    add_filter( 'manage_edit-product_columns', 'woo_product_weight_column', 20 );
    function woo_product_weight_column( $columns ) {
    
        $columns['sort_weight'] = esc_html__( 'Weight', 'woocommerce' );
            return $columns;
    
    }
    // Populate weight column
    add_action( 'manage_product_posts_custom_column', 'woo_product_weight_column_data', 2 );
    function woo_product_weight_column_data( $column ) {
        global $post;
    
        if ( $column == 'sort_weight' ) {
    			$postval = get_post( $post->ID);
    			$menu_order_new = $postval->menu_order;
    			
    	?>
    	<input type="text" name="menuorder" id="id_<?php echo $post->ID;?>" data-productid="<?php echo $post->ID;?>" value="<?php  echo $menu_order_new; ?>"  class="menuorder"/>
    	<?php     
        }
    }
    
    add_action('admin_head', 'my_column_width');
    
    function my_column_width() {
        echo '<style type="text/css">';
        echo 'table.wp-list-table .column-sort_weight { width: 101px; text-align: left!important;padding: 5px;}';
        echo 'table.wp-list-table .column-wpseo-score { width: 101px; text-align: left!important;padding: 5px;}';
        echo'.menuorder{ width: 101px; }';
        echo '</style>';
        
     
       
        
        
    }   
    
    // this code adds jQuery script to website footer that allows to send AJAX request
    add_action( 'admin_footer', 'misha_jquery_event' );
    function misha_jquery_event(){
     
    	echo "<script>jQuery(function($){
    	var weight_val;
    	var pr_id;
    		
    		
    		
    		 jQuery('.menuorder').on('input', function(){
    				weight_val = $(this).val();
    				pr_id=$(this).attr('data-productid');
    			var dataVariable = {
                'action': 'productmetasave', 
                'product_id': pr_id,
                'value':weight_val
                
                        };
    
            jQuery.ajax({
                url: ajaxurl, 
                type: 'POST',
                data: dataVariable, 
                success: function (response) {
                if(response==1){
                location.reload(true);
                }else{
    				 console.log('Failed To update menu-order ');
                }
                   
                }
            });
    				
    				
    				
    				
    		
    		});
    	});</script>";
     
    }
     
    // this small piece of code can process our AJAX request
    add_action( 'wp_ajax_productmetasave', 'misha_process_ajax' );
    function misha_process_ajax(){
     
    	
    	if($_POST['product_id'] && $_POST['value'] ){
    		
    		$arg=array('ID' => $_POST['product_id'],'menu_order' => $_POST['value']);
    			$rs = wp_update_post($arg);
    			if($rs){
    				echo "1";
    			}else{
    				echo "0";
    			}
    		}
     
    	die();
    }
    
    For bulk edit  below code will add same menu order for all product . Remember lower the menu order value ,item  order will be almost front.
    
    

    add_action( ‘woocommerce_product_bulk_edit_start’, ‘custom_field_product_bulk_edit’, 10, 0 );
    function custom_field_product_bulk_edit() {
    ?>
    <div class=”inline-edit-group”>
    <label class=”alignleft”>
    <span class=”title”><?php _e(‘Custom Sort Weight’, ‘woocommerce’); ?></span>
    <span class=”input-text-wrap”>
    <select class=”change_t_dostawy change_to” name=”change_t_dostawy”>
    <?php
    $options = array(
    ” => __( ‘— No change —’, ‘woocommerce’ ),
    ‘1’ => __( ‘Change to:’, ‘woocommerce’ ),
    );
    foreach ( $options as $key => $value ) {
    echo ‘<option value=”‘ . esc_attr( $key ) . ‘”>’ . $value . ‘</option>’;
    }
    ?>
    </select>
    </span>
    </label>
    <label class=”change-input”>
    <input type=”text” name=”_t_dostawy” class=”text t_dostawy” placeholder=”<?php _e( ‘Enter Weight Here ‘, ‘woocommerce’ ); ?>” value=”” />
    </label>
    </div>
    <?php
    }

    // Save the custom fields data when submitted for product bulk edit
    add_action(‘woocommerce_product_bulk_edit_save’, ‘save_custom_field_product_bulk_edit’, 10, 1);
    function save_custom_field_product_bulk_edit( $product ){
    if ( $product->is_type(‘simple’) || $product->is_type(‘external’) ){
    $product_id = method_exists( $product, ‘get_id’ ) ? $product->get_id() : $product->id;

    if ( isset( $_REQUEST[‘_t_dostawy’] ) ){
    // update_post_meta( $product_id, ‘menu_order’, sanitize_text_field( $_REQUEST[‘_t_dostawy’] ) );

    $arg=array(‘ID’ => $product_id,’menu_order’ => $_REQUEST[‘_t_dostawy’]);
    $result = wp_update_post($arg);

    }
    }
    }`

    Hi there,

    This is a fairly complex development topic. I’m going to leave it open for a bit to see if anyone is able to chime in to help you out.

    I can also recommend the WooCommerce Developer Resources Portal for resources on developing for WooCommerce.

    You can also visit the WooCommerce Facebook group or the #developers channel of the WooCommerce Community Slack. We’re lucky to have a great community of open-source developers for WooCommerce, and many of our developers hang out there, as well.

    If you need a paid solution, you can consider taking help from one of our partner’s website.

    Thank you ??

    Hi there,

    We’ve not heard back from you in a while, so I’m marking this thread as resolved.

    Hopefully, you were able to find a solution to your problem! If you have further questions, please feel free to open a new topic.

    Thank you ??

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘How to add bulk menu order option in bulk edit’ is closed to new replies.