How to create auction product type with bidding in woocommerce?

LearningWordpress
Creating a WooCommerce auction product with bidding functionality typically involves using a plugin, as WooCommerce doesn’t have native auction capabilities. Below are general steps to guide you through the process using the “WooCommerce Simple Auctions” plugin as an example. Please note that plugins and their interfaces might change, so it’s essential to refer to the specific plugin documentation for the most accurate instructions.
Below is the custom code to create an auction product with bidding functionality without using a plugin.
The below function will add a custom product type, add it to the function file
function custom_product_type() {
    class WC_Product_Auction extends WC_Product {
        public function __construct( $product ) {
            $this->product_type = ‘auction’;
            parent::__construct( $product );
        }
    }
}
add_action( ‘init’, ‘custom_product_type’ );
Add custom product type to product type list, add the below function in function file
function add_auction_product_type( $types ) {
    $types[‘auction’] = __( ‘Auction’ );
    return $types;
}
add_filter( ‘product_type_selector’, ‘add_auction_product_type’ );
The below function will display custom fields in product general options, add it in function file
function auction_custom_product_options() {
    global $post;
    echo ‘<div class=”options_group”>’;
Add auction Start Date
    woocommerce_wp_text_input(
        array(
            ‘id’          => ‘_auction_start_date’,
            ‘label’       => __( ‘Auction Start Date’, ‘woocommerce’ ),
            ‘desc_tip’    => ‘true’,
            ‘description’ => __( ‘Enter the start date for the auction.’, ‘woocommerce’ ),
            ‘type’        => ‘datetime-local’,
        )
    );
 Add auction End Date
    woocommerce_wp_text_input(
        array(
            ‘id’          => ‘_auction_end_date’,
            ‘label’       => __( ‘Auction End Date’, ‘woocommerce’ ),
            ‘desc_tip’    => ‘true’,
            ‘description’ => __( ‘Enter the end date for the auction.’, ‘woocommerce’ ),
            ‘type’        => ‘datetime-local’,
        )
    );
Add auction Reserve Price for auction product
    woocommerce_wp_text_input(
        array(
            ‘id’          => ‘_auction_reserve_price’,
            ‘label’       => __( ‘Reserve Price’, ‘woocommerce’ ),
            ‘desc_tip’    => ‘true’,
            ‘description’ => __( ‘Enter the reserve price for the auction.’, ‘woocommerce’ ),
            ‘type’        => ‘number’,
            ‘custom_attributes’ => array(
                ‘step’ => ‘0.01’,
            ),
        )
    );
    echo ‘</div>’;
}
add_action( ‘woocommerce_product_options_general_product_data’, ‘auction_custom_product_options’ );
Save custom fields when the product is saved
function save_auction_custom_fields( $post_id ) {
    // Auction Start Date
    $auction_start_date = isset( $_POST[‘_auction_start_date’] ) ? sanitize_text_field( $_POST[‘_auction_start_date’] ) : ”;
    update_post_meta( $post_id, ‘_auction_start_date’, $auction_start_date );
    // Auction End Date
    $auction_end_date = isset( $_POST[‘_auction_end_date’] ) ? sanitize_text_field( $_POST[‘_auction_end_date’] ) : ”;
    update_post_meta( $post_id, ‘_auction_end_date’, $auction_end_date );
    // Auction Reserve Price
    $auction_reserve_price = isset( $_POST[‘_auction_reserve_price’] ) ? sanitize_text_field( $_POST[‘_auction_reserve_price’] ) : ”;
    update_post_meta( $post_id, ‘_auction_reserve_price’, $auction_reserve_price );
}
add_action( ‘woocommerce_process_product_meta’, ‘save_auction_custom_fields’, 30 );
Display auction details on the single product page
function display_auction_details() {
    global $product;
   if ( $product->get_type() === ‘auction’ ) {
        $reserve_price = get_post_meta( $product->get_id(), ‘_auction_reserve_price’, true );
        $auction_end_date = get_post_meta($product->get_id(), ‘_auction_end_date’, true);
        $current_time = current_time(‘timestamp’);
        $auction_end_timestamp = strtotime($auction_end_date);
        $remaining_time_seconds = max(0, $auction_end_timestamp – $current_time);
        $remaining_hours = floor($remaining_time_seconds / 3600); // 1 hour = 3600 seconds
        $remaining_minutes = floor(($remaining_time_seconds % 3600) / 60); // 1 minute = 60 seconds
        echo “<p class=’biddetail’>Auction Remaining time: &nbsp $remaining_hours hours and $remaining_minutes minutes</p>”;
        // echo ‘<p><strong>Auction Start Date:</strong> ‘ . esc_html( $start_date ) . ‘</p>’;
        // echo ‘<p><strong>Auction End Date:</strong> ‘ . esc_html( $end_date ) . ‘</p>’;
        $current_bid = get_post_meta($product->get_id(), ‘_current_bid’, true);
         // Display the current bid
         echo ‘<p class=”biddetail”>Current Bid: &nbsp $’ . esc_html($current_bid) . ‘</p>’;
        echo ‘<p class=”biddetail”><strong>Reserve Price: &nbsp </strong> $’ . esc_html( $reserve_price ) . ‘</p>’;
    }
}
add_action( ‘woocommerce_single_product_summary’, ‘display_auction_details’, 30 );
Display the bidding form in product page
function display_bid_form() {
    global $product;
    if ($product->get_type() === ‘auction’) {
        echo ‘<div id=”bid-form”>’;
        echo ‘<h3>Place Your Bid  &nbsp </h3>’;
        echo ‘<form method=”post” action=””>’;
        echo ‘<label for=”bid_amount” class=”biddetail”>Bid Amount: &nbsp  &nbsp </label>’;
        echo ‘<input type=”number” step=”0.01″ name=”bid_amount” class=”bidtextinput” id=”bid_amount” required />’;
        echo ‘<input type=”hidden” name=”product_id” value=”‘ . $product->get_id() . ‘” />’;
        echo ‘<input type=”submit” name=”submit_bid” value=”Place Bid” class=”bidbutton” />’;
        echo ‘</form>’;
        echo ‘</div>’;
    }
}
add_action(‘woocommerce_single_product_summary’, ‘display_bid_form’, 30);
By the below function you can handle the submission of bid with different conditions you can modify it as per your requirement
function handle_bid_submission() {
    if(isset($_POST[‘submit_bid’])){
        if (is_user_logged_in()) {
                $product_id = sanitize_text_field($_POST[‘product_id’]);
                $bid_amount = sanitize_text_field($_POST[‘bid_amount’]);
                // Check if the auction end date has passed
                $auction_end_date = get_post_meta($product_id, ‘_auction_end_date’, true);
                $current_time = current_time(‘timestamp’);
                if ($auction_end_date && strtotime($auction_end_date) <= $current_time) {
                    echo ‘Auction is over. Bidding is disabled.’;
                    exit; // Exit to prevent further processing
                }
                // Validate bid amount if needed Update or save bid information in the database
                $current_bid = get_post_meta($product_id, ‘_current_bid’, true);
                if ($bid_amount > $current_bid) {
                    update_post_meta($product_id, ‘_current_bid’, $bid_amount);
                    $bidder_email = get_post_meta($product_id, ‘_bidder_email’, true);
                    update_post_meta($product_id, ‘_bidder_email’, get_the_author_meta(‘user_email’, get_current_user_id()));
                     // Update or save bid information in the database
                    $current_bid = get_post_meta($product_id, ‘_current_bid’, true);
                    echo”<script>alert(‘Your bid was placed successfully!’)</script>”;
                        if ($bid_amount > $current_bid) {
                            // Get the current highest bidder’s email
                            $current_bidder_email = get_post_meta($product_id, ‘_bidder_email’, true);
                            // Update bid information
                            update_post_meta($product_id, ‘_current_bid’, $bid_amount);
                            update_post_meta($product_id, ‘_bidder_email’, get_the_author_meta(‘user_email’, get_current_user_id()));
                                // Notify the new highest bidder if the auction is over
                                if (strtotime($auction_end_date) <= $current_time) {
                                    $new_bidder_email = get_the_author_meta(‘user_email’, get_current_user_id());
                                    $subject = ‘Congratulations! You Won the Auction!’;
                                    $message = ‘Congratulations! You are the highest bidder for this product with a winning bid of $’ . esc_html($bid_amount) . ‘.’;
                                    $message .= ‘ To complete your purchase, please proceed to the payment link: ‘ . get_permalink($product_id);
                                    $headers = array(‘Content-Type: text/html; charset=UTF-8’);
                                    wp_mail($new_bidder_email, $subject, $message, $headers);
                                }
                        }
                }
            else{
                echo”<script>alert(‘Your bid must be higher than the current bid!’)</script>”;
            }
        }
        else{
            echo”<script>alert(‘You must be logged in to place a bid!’)</script>”;
        }
    }
}
add_action(‘init’, ‘handle_bid_submission’);
You can also create your plugin for the auction or bidding in WooCommerce, you can modify it as per your requirement.

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Must Read