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);
}
}
}`