問題描述
遇到這個問題,感謝您的幫助!
Having trouble with this issue, any help is appreciated!
我希望我的每個 woocommerce 單一產品頁面都有一個立即預訂"按鈕,該按鈕鏈接到我在 WordPress 上創建的名為預訂約會"的頁面.
I want each of my woocommerce single product pages to have a button "Book Now" that links to a page that I created on WordPress titled "Booking Appointments".
那個預訂約會"頁面包含一個我安裝的簡單約會"插件表單(客戶將填寫),我也想讓它自動填充產品的 SKU 或值到表單輸入之一(從上一頁),這樣我就不必每次都輸入它.
That "Booking Appointments" page contains a "Easy Appointment" Plug in Form (that clients will fill out) that I installed which I also want to have it auto populate the product's SKU or values into one of the form inputs (from the pervious page) so that I do not have to enter it each time.
我在網上嘗試了各種方法,但根本不起作用.
I tried a variety of methods online but its not working at all.
推薦答案
更新
這將在單個產品頁面中添加一個立即預訂"按鈕鏈接到您的預訂約會"頁面,通過 URL 傳遞產品 ID、產品 sku...
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…
所以代碼將是:
// 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 中獲取值以填充必要的字段 帶有您將在其中使用的附加腳本:
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'];
在 WooCommerce 3+ 中測試和工作.
Tested and works in WooCommerce 3+.
這篇關于添加自定義“預訂"按鈕到 WooCommerce 產品單頁的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!