add_action( 'wp_loaded', 'auto_add_auctions_to_cart', 10 );
function auto_add_auctions_to_cart() {
if ( ! is_admin() && is_user_logged_in() ) {
$user_id = get_current_user_id();
$args = array(
'post_type' => 'product',
'posts_per_page' => '-1',
'order' => 'ASC',
'orderby' => 'meta_value',
'meta_key' => '_auction_dates_to',
'meta_query' => array(
array(
'key' => '_auction_closed',
'value' => 2,
),
array(
'key' => '_auction_current_bider',
'value' => $user_id,
),
array(
'key' => '_auction_payed',
'compare' => 'NOT EXISTS',
),
),
'show_past_auctions' => true,
'auction_arhive' => true,
'fields' => 'ids',
'no_found_rows' => true,
'update_post_meta_cache' => false,
'update_post_term_cache' => false,
);
$winningloop = new WP_Query( $args );
$posts_ids = $winningloop->posts;
if ( is_array( $posts_ids ) ) {
foreach ( $posts_ids as $posts_id ) {
$found = false;
//check if product already in cart
if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
if ( $_product->get_id() == $posts_id ) {
$found = true;
}
}
// if product not found, add it
if ( ! $found ) {
WC()->cart->add_to_cart( $posts_id );
}
} else {
// if no products in cart, add it
WC()->cart->add_to_cart( $posts_id );
}
}
}
}
}
Comments