WooCommerce: Show schedule option in quick edit of the product in backend product summary

Date: 
9. February 2024
Author: 
mbs
Category: 

Do you want to add the "Schedule" options for sale price in WooCoommerce, when using quick edit?

Here is a code that works with simple products

function style_sale_price_dates_quick_edit() {
    ?>
     <style>
        .ls-display-none{
          display: none;
        }
        .ls-schedule:hover{
          cursor: pointer;
        }
    </style> 
    <script type="text/javascript">
      jQuery(document).ready(function(){
        jQuery(document).on("click",".ls-schedule",function(){
          var trid=jQuery(this).closest("tr").attr("id");
          jQuery("#"+trid+" div").removeClass("ls-display-none");
          jQuery("#"+trid+" .ls-schedule").addClass("ls-display-none");
        });

        jQuery('#the-list').on('click', '.editinline', function() {
          var post_id = jQuery(this).closest('tr').attr('id');
          post_id = post_id.replace('post-', '');
          var sale_price_dates_start = jQuery('#sale_price_dates_start_' + post_id).text();
          var sale_price_dates_end = jQuery('#sale_price_dates_end_' + post_id).text();
          jQuery('input[name=\'sale_price_dates_start\']', '.inline-edit-row').val(sale_price_dates_start);
          jQuery('input[name=\'sale_price_dates_end\']', '.inline-edit-row').val(sale_price_dates_end);
        });

      })
    </script>
    <?php
}
add_action('admin_head', 'style_sale_price_dates_quick_edit');

add_action( 'woocommerce_product_quick_edit_start', 'ls_show_custom_field_quick_edit' );
function ls_show_custom_field_quick_edit() {
   global $post;
   if ($post->post_type !== 'product') return;
   ?>
   <script>
    jQuery(document).ready(function(){
      var aaaa='<a hred="javascript:void(0)" class="ls-schedule">Schedule</a><div class="ls-display-none"><label><span class="title">Sale price dates</span><span class="input-text-wrap"><input type="date" name="sale_price_dates_start" class="text" value=""><input type="date" name="sale_price_dates_end" class="text" value=""></span></label><br class="clear" /></div>';
      jQuery(".price_fields").append(aaaa);
    });
   </script>
   <?php
}
 
add_action( 'manage_product_posts_custom_column', 'ls_show_custom_field_quick_edit_data', 9999, 2 );
function ls_show_custom_field_quick_edit_data( $column, $post_id ){
    if ( 'name' !== $column ) return;

    $product = wc_get_product($post_id);
    $sale_start_date = $product->get_date_on_sale_from() ? date("Y-m-d",strtotime($product->get_date_on_sale_from())) : '';
    $sale_end_date = $product->get_date_on_sale_to() ? date("Y-m-d",strtotime($product->get_date_on_sale_to())) : '';

    echo '<div style="display:none;">Start Date: <span id="sale_price_dates_start_' . $post_id . '">' . esc_html($sale_start_date) . '</span></div><div style="display:none;">End Date: <span id="sale_price_dates_end_' . $post_id . '">' . esc_html($sale_end_date) . '</span></div>';
}
 
add_action( 'woocommerce_product_quick_edit_save', 'ls_save_custom_field_quick_edit' );
function ls_save_custom_field_quick_edit( $product ) {
    //$post_id = $product->get_id();
    
    if (isset($_REQUEST['sale_price_dates_start'])) {
        $sale_price_dates_start = sanitize_text_field($_REQUEST['sale_price_dates_start']);
        if ($sale_price_dates_start) {
            $product->set_date_on_sale_from(strtotime($sale_price_dates_start));
        } else {
            $product->set_date_on_sale_from('');
        }
    }
    if (isset($_REQUEST['sale_price_dates_end'])) {
        $sale_price_dates_end = sanitize_text_field($_REQUEST['sale_price_dates_end']);
        if ($sale_price_dates_end) {
            $product->set_date_on_sale_to(strtotime($sale_price_dates_end));
        } else {
            $product->set_date_on_sale_to('');
        }
    }
    $product->save();
}

Leave a Reply

Your email address will not be published. Required fields are marked *

© Copyright 2007-2024
mbsTECH.dk

Build with
and 
Oxygenbuilder
I'm using 
Ahoy