[英]Woocommerce Change Checkout Total on Checkout Page
我有一個運行 WooCommerce 插件的 Wordpress 站點。 在結帳頁面上,客戶要求提供他們送貨到的村庄的下拉列表,並要求對訂單收取送貨費用。 因此,訂購該物品的人會選擇該物品,去結帳,輸入他們的地址,從下拉菜單中選擇他們的村庄,訂單總額將在此更改,然后支付更改后的訂單總額。
我現在的代碼在下面,只將下拉框添加到訂單中。 所有這些代碼都是從自定義插件文件中添加的。
add_filter( 'woocommerce_checkout_fields' , 'town_dropdown' );
add_action('woocommerce_checkout_process', 'check_if_town_set');
function town_dropdown($fields) {
$fields['billing']['town_dropdown'] = array(
'type' => 'select',
'class' => array('form-row-wide'),
'label' => '<div id="town-drop"><h2>Delivery Charge</h2><p>We deliver to the villages in the dropdown below. Please choose a village to have the correct price applied to your order.</p><br>',
'options' => array(
'0' => 'Please select a delivery charge',
'3.50-lin' => 'Linton - £3.50',
'5.00-abi' => 'Gt & Lt Abington - £5.00',
'6.00-bab' => 'Babraham - £6.00',
'5.00-bal' => 'Balsham - £5.00',
'5.00-bar' => 'Bartlow - £5.00',
'8.00-bri' => 'Brinkley - £8.00',
'8.00-bur' => 'Burrough Green - £8.00',
'8.00-cam' => 'Cambridge - £8.00',
'5.00-card' => 'Cardinals Green - £5.00',
'8.00-carl' => 'Carlton - £8.00',
'6.00-cas' => 'Castle Camps - £6.00',
'8.00-che' => 'Cherry Hinton - £8.00',
'6.00-dux' => 'Duxford - £6.00',
'8.00-ful' => 'Fulbourn - £8.00',
'5.00-had' => 'Hadstock - £5.00',
'8.00-hav' => 'Haverhill - £8.00',
'8.00-hel' => 'Helions Bumpstead - £8.00',
'8.00-hem' => 'Hempstead - £8.00',
'5.00-hil' => 'Hildersham - £5.00',
'8.00-hix' => 'Hinxton - £8.00',
'5.00-hor' => 'Horseheath - £5.00',
'8.00-ick' => 'Ickleton - £8.00',
'8.00-ked' => 'Kedington - £8.00',
'8.00-litb' => 'Littlebury - £8.00',
'6.00-litw' => 'LittleWalden - £6.00',
'6.00-rad' => 'Radwinter - £6.00',
'8.00-saf' => 'Saffron Walden - £8.00',
'8.00-sam' => 'Gt & Lt Sampford - £8.00',
'6.00-saw' => 'Sawston - £6.00',
'8.00-sew' => 'Sewards End - £8.00',
'8.00-she' => 'Shelfords - £8.00',
'8.00-ste' => 'Steeple Bumpstead - £8.00',
'6.00-str' => 'Streetly End - £6.00',
'8.00-thu' => 'Thurlow - £8.00',
'6.00-wwi' => 'West Wickham - £6.00',
'6.00-wwa' => 'West Wratting - £6.00',
'6.00-wco' => 'Weston Colville - £6.00',
'8.00-wgr' => 'Weston Green - £8.00',
'8.00-whi' => 'Whittlesford - £8.00',
'8.00-wil' => 'Gt & Lt Wilbraham - £8.00',
'6.00-wit' => 'Withersfield - £6.00',
'8.00-wra' => 'Gt & Lt Wratting - £8.00',
)
);
return $fields;
}
function check_if_town_set() {
if (!$_POST['town_dropdown'])
if ($_POST['town_dropdown'] == 0)
wc_add_notice( __('Please select a shipping charge'), 'error' );
}
總結一下,我的問題是:
1) 結賬頁面是否有更新結賬總額的功能?
2)我如何調用這個函數? 是否可以調用 select OnChange 事件? 如果沒有,我可以在運費框下方添加一個按鈕來調用該功能嗎?
請多問我問題,我確定我有什么問題,因為這是我第一次使用 Wordpress 插件 API - 感謝所有回復。
已解決 - 方法如下。 添加了一個按鈕和一個下拉菜單,允許客戶將運輸作為產品添加到購物車中,這是代碼。
add_filter( 'woocommerce_after_order_notes' , 'town_dropdown' );
function town_dropdown($checkout) {
echo '<div id="town-drop">
<h2>Delivery Charge</h2>
<p>We deliver to the villages in the dropdown below. Please choose a village to have the correct price applied to your order. <strong>You must have the correct delivery price added, or your order will not be shipped!</strong></p>';
echo '<script>
function AddProductToCart() {
product = document.getElementById("town_dropdown").value;
productid = product.split("-");
window.location = "http://site-url.com/checkout/?add-to-cart="+productid[0];
}
</script>';
woocommerce_form_field( 'town_dropdown', array(
'type' => 'select',
'class' => array('form-row-wide'),
'label' => '',
'options' => array(
'0' => 'Please select a delivery charge',
'2458-lin' => 'Linton - £3.50',
'2468-abi' => 'Gt & Lt Abington - £5.00',
'2470-bab' => 'Babraham - £6.00',
'2468-bal' => 'Balsham - £5.00',
'2468-bar' => 'Bartlow - £5.00',
'2471-bri' => 'Brinkley - £8.00',
'2471-bur' => 'Burrough Green - £8.00',
'2471-cam' => 'Cambridge - £8.00',
'2468-card' => 'Cardinals Green - £5.00',
'2471-carl' => 'Carlton - £8.00',
'2470-cas' => 'Castle Camps - £6.00',
'2471-che' => 'Cherry Hinton - £8.00',
'2470-dux' => 'Duxford - £6.00',
'2471-ful' => 'Fulbourn - £8.00',
'2468-had' => 'Hadstock - £5.00',
'2471-hav' => 'Haverhill - £8.00',
'2471-hel' => 'Helions Bumpstead - £8.00',
'2471-hem' => 'Hempstead - £8.00',
'2468-hil' => 'Hildersham - £5.00',
'2471-hix' => 'Hinxton - £8.00',
'2468-hor' => 'Horseheath - £5.00',
'2471-ick' => 'Ickleton - £8.00',
'2471-ked' => 'Kedington - £8.00',
'2471-litb' => 'Littlebury - £8.00',
'2470-litw' => 'Little Walden - £6.00',
'2470-rad' => 'Radwinter - £6.00',
'2471-saf' => 'Saffron Walden - £8.00',
'2471-sam' => 'Gt & Lt Sampford - £8.00',
'2470-saw' => 'Sawston - £6.00',
'2471-sew' => 'Sewards End - £8.00',
'2471-she' => 'Shelfords - £8.00',
'2471-ste' => 'Steeple Bumpstead - £8.00',
'2470-str' => 'Streetly End - £6.00',
'2471-thu' => 'Thurlow - £8.00',
'2470-wwi' => 'West Wickham - £6.00',
'2470-wwa' => 'West Wratting - £6.00',
'2470-wco' => 'Weston Colville - £6.00',
'2471-wgr' => 'Weston Green - £8.00',
'2471-whi' => 'Whittlesford - £8.00',
'2471-wil' => 'Gt & Lt Wilbraham - £8.00',
'2470-wit' => 'Withersfield - £6.00',
'2471-wra' => 'Gt & Lt Wratting - £8.00',
)
),$checkout->get_value('town_dropdown')
);
echo '<button type="button" OnClick="AddProductToCart()">Add Shipping</button>';
echo '</div>';
}
單擊按鈕時,Javascript 重定向到將產品添加到購物車的頁面,通過使用選擇值的第一部分作為產品 ID(之所以需要這樣做是因為 PHP 數組不能有多個相同的元素)標識符)。 希望這對某人有所幫助 - 如果您有 q,請評論。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.