wpgenie icon

add all won auctions to cart

wpgenie | PRO | 05/30/16 06:04:40 AM UTC | 0 ⭐ | 1126 👁️ | Never ⏰ | []
PHP |

1.79 KB

|

None

|

0 👍

/

0 👎

add_action('wp_loaded',  'add_all_auctions_to_cart');
function add_all_auctions_to_cart(){
    if ( ! is_admin() ) {
 
        if ( ! empty( $_GET['add-all-auctions-to-cart'] ) ) {
 
            global $woocommerce;
            $current_user = wp_get_current_user();
 
            if (!is_user_logged_in()) {
                        header('Location: '.wp_login_url( $woocommerce->cart->get_checkout_url() .'?add-all-auctions-to-cart' ));
                        exit;
                    }                                               
 
            $woocommerce->cart->empty_cart();
 
            $query->query_vars['meta_query'] = array(
                                array(
                                    'key' => '_auction_closed',
                                    'value' => '2'
                                ),
                                array(
                                    'key' => '_auction_payed',
                                    'compare' => 'NOT EXISTS'
                                )
                            );
 
            $args = array(
                'post_type' => 'product',
                'post_status' => 'publish',
                'ignore_sticky_posts'   => 1,
                'posts_per_page' => -1,
                'tax_query' => array(array('taxonomy' => 'product_type' , 'field' => 'slug', 'terms' => 'auction')),
                'auction_arhive' => TRUE,
                'meta_query' => array(
                    array(
                        'key' => '_auction_closed',
                        'value' => '2'
                    ),
                    array(
                        'key' => '_auction_payed',
                        'compare' => 'NOT EXISTS'
                    ),
                     array(
                               'key' => '_auction_current_bider',
                               'value' => $current_user->ID,
                           )
                )
            );
 
            $products = new WP_Query( $args );
 
            if ( $products->have_posts() ) :
 
            
 
                while ( $products->have_posts() ) : $products->the_post(); 
 
                    $woocommerce->cart->add_to_cart($products->post->ID );
 
                 endwhile; // end of the loop
 
            
 
            endif;
            
            wp_safe_redirect(  remove_query_arg( array( 'pay-auction', 'quantity', 'product_id' ), $woocommerce->cart->get_checkout_url() )  );
            exit;
            
        }
    }
    
}

Comments