上一个订单将在wooCommerce中重新订购.这怎么可能?



我正在开发一个woocommerce网站,有3个类别,如蔬菜,快餐和;鱼。蔬菜是他的电子商务网站的主要类别。我的客户要求是用户从蔬菜类订购3种产品,从鱼类订购2种产品,总共5种产品。用户通过付款完成他们的订单。几天后,用户再次来到网站,他想购买一些食物,当他进入蔬菜类别时,他看到了复制订单按钮。用户单击复制订单按钮重新订购之前的订单,但没有鱼类中的2个产品。从蔬菜类别中选择3个产品加入购物车页面,完成订单。我编写了以下脚本,但我无法将Fish类产品与单个订单ID分开。如何解决?

$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;
}  

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

js文件是这样的

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

最新更新