【问题标题】:Add custom "Booking" button to WooCommerce Product single pages将自定义“预订”按钮添加到 WooCommerce 产品单页
【发布时间】:2018-02-18 14:31:51
【问题描述】:

遇到此问题,感谢任何帮助!

我希望我的每个 woocommerce 单一产品页面都有一个“立即预订”按钮,该按钮链接到我在 WordPress 上创建的标题为“预订约会”的页面。

“预订预约”页面包含我安装的“简单预约”插件表单(客户将填写),我还希望它自动将产品的 SKU 或值填充到表单输入之一(来自上一页),这样我就不必每次都输入它。

我在网上尝试了各种方法,但根本不起作用。

【问题讨论】:

    标签: php wordpress button woocommerce product


    【解决方案1】:

    更新

    这将在单个产品页面中添加一个“立即预订”按钮链接到您的“预订预约”页面通过 URL 传递产品 ID、产品 sku...

    所以代码是:

    // 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>';
    
    }
    

    代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件中。

    在您的“预订约会”页面表单中,您将能够使用 $_GET 从 URL 获取值以填充必要的字段,其中包含额外的脚本您将使用的:

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

    在 WooCommerce 3+ 中测试并运行。

    【讨论】:

    • 我在自动填充自定义输入表单时遇到问题,因为我不熟悉这个“轻松预约”插件。你知道吗?
    • 个人信息不需要自动填充。我刚刚添加了一个 ProductID 字段(刷新并查看),我只需要获取 URL 参数以在页面加载时自动填写它。在网上找不到任何东西.. @LoicTheAztec
    【解决方案2】:

    content-single-product.php的最后可以添加

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

    然后使用 .button-link 设置链接样式,使其看起来像一个按钮。

    【讨论】:

    • 在默认的 content-single-product.php 上尝试过,但它仍然重定向到当前页面(刷新页面)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-01
    • 2018-12-16
    • 1970-01-01
    • 2019-07-16
    • 2020-11-16
    • 1970-01-01
    相关资源
    最近更新 更多