Advertisement
wpgenie

add all won auctions to cart

May 30th, 2016
615
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.79 KB | None | 0 0
  1. add_action('wp_loaded',  'add_all_auctions_to_cart');
  2. function add_all_auctions_to_cart(){
  3.     if ( ! is_admin() ) {
  4.  
  5.         if ( ! empty( $_GET['add-all-auctions-to-cart'] ) ) {
  6.  
  7.             global $woocommerce;
  8.             $current_user = wp_get_current_user();
  9.  
  10.             if (!is_user_logged_in()) {
  11.                         header('Location: '.wp_login_url( $woocommerce->cart->get_checkout_url() .'?add-all-auctions-to-cart' ));
  12.                         exit;
  13.                     }                                              
  14.  
  15.             $woocommerce->cart->empty_cart();
  16.  
  17.             $query->query_vars['meta_query'] = array(
  18.                                 array(
  19.                                     'key' => '_auction_closed',
  20.                                     'value' => '2'
  21.                                 ),
  22.                                 array(
  23.                                     'key' => '_auction_payed',
  24.                                     'compare' => 'NOT EXISTS'
  25.                                 )
  26.                             );
  27.  
  28.             $args = array(
  29.                 'post_type' => 'product',
  30.                 'post_status' => 'publish',
  31.                 'ignore_sticky_posts'   => 1,
  32.                 'posts_per_page' => -1,
  33.                 'tax_query' => array(array('taxonomy' => 'product_type' , 'field' => 'slug', 'terms' => 'auction')),
  34.                 'auction_arhive' => TRUE,
  35.                 'meta_query' => array(
  36.                     array(
  37.                         'key' => '_auction_closed',
  38.                         'value' => '2'
  39.                     ),
  40.                     array(
  41.                         'key' => '_auction_payed',
  42.                         'compare' => 'NOT EXISTS'
  43.                     ),
  44.                      array(
  45.                                'key' => '_auction_current_bider',
  46.                                'value' => $current_user->ID,
  47.                            )
  48.                 )
  49.             );
  50.  
  51.             $products = new WP_Query( $args );
  52.  
  53.             if ( $products->have_posts() ) :
  54.  
  55.            
  56.  
  57.                 while ( $products->have_posts() ) : $products->the_post();
  58.  
  59.                     $woocommerce->cart->add_to_cart($products->post->ID );
  60.  
  61.                  endwhile; // end of the loop
  62.  
  63.            
  64.  
  65.             endif;
  66.            
  67.             wp_safe_redirect(  remove_query_arg( array( 'pay-auction', 'quantity', 'product_id' ), $woocommerce->cart->get_checkout_url() )  );
  68.             exit;
  69.            
  70.         }
  71.     }
  72.    
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement