wpgenie icon

auctions Buy now button in the loop

wpgenie | PRO | 12/19/22 07:51:21 AM UTC (Edited) | 0 ⭐ | 4282 👁️ | Never ⏰ | []
PHP |

1.56 KB

|

None

|

0 👍

/

0 👎

/* visit us - https://wpgenie.org */
 
add_action( 'woocommerce_after_shop_loop_item', 'custom_loop_buy_nowbutton', 11 );
 
function custom_loop_buy_nowbutton( $args = array() ) {
    global $product;
    if ( ! $product->is_purchasable() OR ! $product->is_sold_individually() OR ! $product->is_in_stock() OR $product->is_closed() ) return;
    if ( $product && $product->get_type() == 'auction'  ) {
        $defaults = array(
            'quantity' => 1,
            'class'    => implode( ' ', array_filter( array(
                    'button',
                    'product_type_' . $product->get_type(),
                    $product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button' : '',
                    $product->supports( 'ajax_add_to_cart' ) ? 'ajax_add_to_cart' : '',
            ) ) ),
        );
 
        $args = apply_filters( 'woocommerce_loop_add_to_cart_args', wp_parse_args( $args, $defaults ), $product );
 
        $url = $product->is_purchasable() && $product->is_in_stock() ? remove_query_arg( 'added-to-cart', add_query_arg( 'add-to-cart', $product->id ) ) : get_permalink( $product->id );
 
        $url =  apply_filters( 'woocommerce_product_add_to_cart_url', $url, $product );
 
        echo apply_filters( 'woocommerce_loop_add_to_cart_link',
            sprintf( '<a rel="nofollow" href="%s" data-quantity="%s" data-product_id="%s" data-product_sku="%s" class="%s">%s</a>',
                esc_url( $url ),
                esc_attr( 1 ),
                esc_attr( $product->get_id() ),
                esc_attr( $product->get_sku() ),
                esc_attr( isset( $class ) ? $class : 'button' ),
                sprintf(__( 'Buy now for %s', 'wc_simple_auctions' ),wc_price($product->get_regular_price()) )
            ),
        $product );
    }
}

Comments