Advertisement
wpgenie

automatically add won auctions to cart

Mar 29th, 2019
1,035
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.60 KB | None | 0 0
  1. add_action( 'wp_loaded', 'auto_add_auctions_to_cart', 10 );
  2.  
  3. function auto_add_auctions_to_cart() {
  4.     if ( ! is_admin() && is_user_logged_in() ) {
  5.         $user_id = get_current_user_id();
  6.         $args    = array(
  7.             'post_type'          => 'product',
  8.             'posts_per_page'     => '-1',
  9.             'order'              => 'ASC',
  10.             'orderby'            => 'meta_value',
  11.             'meta_key'           => '_auction_dates_to',
  12.             'meta_query'         => array(
  13.                 array(
  14.                     'key'   => '_auction_closed',
  15.                     'value' => 2,
  16.                 ),
  17.                 array(
  18.                     'key'   => '_auction_current_bider',
  19.                     'value' => $user_id,
  20.                 ),
  21.                 array(
  22.                     'key'     => '_auction_payed',
  23.                     'compare' => 'NOT EXISTS',
  24.                 ),
  25.             ),
  26.             'show_past_auctions' => true,
  27.             'auction_arhive'     => true,
  28.             'fields'                 => 'ids',
  29.             'no_found_rows'          => true,
  30.             'update_post_meta_cache' => false,
  31.             'update_post_term_cache' => false,
  32.         );
  33.  
  34.         $winningloop = new WP_Query( $args );
  35.  
  36.         $posts_ids = $winningloop->posts;
  37.         if ( is_array( $posts_ids ) ) {
  38.  
  39.             foreach ( $posts_ids as $posts_id ) {
  40.                 $found = false;
  41.                 //check if product already in cart
  42.                 if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
  43.                     foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
  44.                         $_product = $values['data'];
  45.                         if ( $_product->get_id() == $posts_id ) {
  46.                             $found = true;
  47.                         }
  48.                     }
  49.                     // if product not found, add it
  50.                     if ( ! $found ) {
  51.                         WC()->cart->add_to_cart( $posts_id );
  52.                     }
  53.                 } else {
  54.                     // if no products in cart, add it
  55.                     WC()->cart->add_to_cart( $posts_id );
  56.                 }
  57.             }
  58.         }
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement