'Last order would be re-order in wooCommerce. How is it possible?

I am developing a woocommerce site that has 3 categories like vegetable, Fast food & Fish. vegetables are the main category of his eCommerce site. My client requirement is that the user orders 3 products from the vegetable category and 2 products from Fish in total 5 products. The user completes their order with payment. After a few days that user comes to the site again he wants to purchase some food, while he goes to the vegetable category then he sees the copy order button. that the user clicks the copy order button  reorder his previous order without 2 products from the fish category. 3 products from the vegetable category in the add to cart page and complete his/her order. I wrote the following script but i can't separate the Fish category products from the single order ID. How to fix it ?

$category = get_queried_object();
        $cat_id = $category->term_id;   //term_taxonomy_id,, term_id
        //all order
        $customer_orders = get_posts(apply_filters('woocommerce_my_account_my_orders_query', array(
            'numberposts' => -1,
            'meta_key' => '_customer_user',
            'meta_value' => get_current_user_id(),
            'post_type' => wc_get_order_types('view-orders'),
            'post_status' => array_keys(wc_get_order_statuses())
        )));
        

        if (is_product_category('qing-ming-festival') && !empty($customer_orders)) {
        
        $get_oder = false;
        foreach ($customer_orders as $single_order) {
            $orders = new WC_Order( $single_order->ID );
            $items = $orders->get_items();
            //var_dump ($orders);
            $get_oder=true;
           global $wpdb;
        $result = $wpdb->get_results('SELECT * FROM wp_woocommerce_order_items where order_id="'.$single_order->ID.'" AND order_item_name ="Joss Paper 2"');
        //var_dump ($result);
        foreach($result as $row) {
            ?>
      <a class="button ced_my_account_reorder" href="javascript:void(0);" data-order_id="<?php echo $row->order_id;?>"> <?php echo esc_attr($settings['buttontext']); ?> </a>            

         <?php      
        }   
            if ($get_oder) {
            break;
         }  
            
     }

    }

  }


Solution 1:[1]

function woo_reorder_scripts() {
    wp_enqueue_script('reorder', get_stylesheet_directory_uri().'/assets/js/reorder.js',array('jquery')); // js file from where we execute ajax request
    wp_localize_script( 'reorder', 'ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) )); // ajax
}
add_action('wp_enqueue_scripts','woo_reorder_scripts');

//Place following button where you want
function reorder_button() {
    if ( ! is_user_logged_in() ) return;
    echo '<a href="#" class="reorder">Reorder</a>';
}

//Our function executed from ajax
add_action( 'wp_ajax_reorder_action', 'reorder_action_callback' );

function reorder_action_callback() {
    $args = array(
        'limit'           => 1, // We want latest order
        'return'          => 'ids', // We want only the order ID
        'status'          => 'completed' // If we look for latest completed order or comment for any order status
    );
    $query = new WC_Order_Query( $args );
    $latest_order = $query->get_orders();
    if($latest_order) {
        foreach( $latest_order as $order_id ) {
            $order = wc_get_order( $order_id );
            if ( $order ) {
                $product_ids = array();
                foreach ( $order->get_items() as $item_id => $item ) {
                    if( has_term( array( 'tshirts' ), 'product_cat', $item->get_product_id() )) { // Change tshirts to category/ies you want
                        // WC()->cart->empty_cart(); // Uncomment if you want to clear cart before adding the items
                        WC()->cart->add_to_cart( $item->get_product_id(), $item->get_quantity()); // Add the order item with same quantity again
                    }
                }
            }
        }
    }
    exit();
}

And the js file will look like this

(function ($) {
    $(document).ready(function($) {

        $('.reorder').click(function(){
            $.ajax({
                type: 'POST'
                ,dataType: 'json'
                ,url: ajax_object.ajax_url
                ,data: {
                    'action': 'reorder_action'
                }
            });
        });
    });
})(jQuery);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1