Add custom “Booking” button to WooCommerce Product single pages

前端 未结 2 983
眼角桃花
眼角桃花 2020-12-20 02:19

Having trouble with this issue, any help is appreciated!

I want each of my woocommerce single product pages to have a button \"Book Now\" that links to a page that I

相关标签:
2条回答
  • 2020-12-20 03:01

    Updated

    This will add in single product pages a "Book Now" button linked to your "Booking Appointments" page passing through the URL the product ID, the product sku…

    So the code will be:

    // Add a custom button in Single product pages
    add_action( 'woocommerce_single_product_summary', 'replacing_template_single_add_to_cart', 31, 0 );
    function replacing_template_single_add_to_cart() {
        global $product;
    
        $product_id = $product->get_id();
        $sku = $product->get_sku();
    
        // Set below the page ID, your button text and link parameters to pass in url
        $page_id = 124;
        $button_text = __('Book Now', 'woocommerce');
        $link = get_permalink( $page_id ) . '?id=' . $product_id . '&sku=' . $sku;
    
        // Output your custom text
        echo '<a href="'.$link.'" class="book-now button">'.$button_text.'</a>';
    
    }
    

    Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

    In your your "Booking Appointments" page form you will be able to get the values from the URL with $_GET to populate necessary fields with an additional scripts in which you will use:

    $product_id = $_GET['id'];
    $sku = $_GET['sku'];
    

    Tested and works in WooCommerce 3+.

    0 讨论(0)
  • 2020-12-20 03:11

    At the very end of content-single-product.php you can add

    <a class="button-link" href="<?php 
    get_page_by_path(booking-appointments); 
    ?>">Booking Appointments</a>
    

    then use .button-link to style the link to look like a button.

    0 讨论(0)
提交回复
热议问题