我有一个正在运行 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 Plugin 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 进行评论。
关于php - Woocommerce 更改结帐页面上的结帐总额,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32380046/