Woocommerce 更改结帐页面上的结帐总数
Woocommerce Change Checkout Total on Checkout Page
我有一个 Wordpress 网站,我在上面 运行 WooCommerce 插件。在结帐页面上,客户要求提供他们送货到的村庄的下拉列表,并要求将运费应用于订单。因此,订购该商品的人会选择该商品,去结账,输入他们的地址,select 他们的村庄从下拉菜单中更改订单总额,然后支付更改后的订单总额。
我目前的代码如下,只将下拉框添加到订单中。所有这些代码都是从自定义插件文件中添加的。
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 重定向到将产品添加到购物车的页面,方法是使用 select 值的第一部分作为产品 ID(这是为什么必要的是 PHP 数组不能有多个具有相同标识符的元素)。希望这对某人有帮助 - 如果你有的话,请评论 q。
我有一个 Wordpress 网站,我在上面 运行 WooCommerce 插件。在结帐页面上,客户要求提供他们送货到的村庄的下拉列表,并要求将运费应用于订单。因此,订购该商品的人会选择该商品,去结账,输入他们的地址,select 他们的村庄从下拉菜单中更改订单总额,然后支付更改后的订单总额。
我目前的代码如下,只将下拉框添加到订单中。所有这些代码都是从自定义插件文件中添加的。
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 重定向到将产品添加到购物车的页面,方法是使用 select 值的第一部分作为产品 ID(这是为什么必要的是 PHP 数组不能有多个具有相同标识符的元素)。希望这对某人有帮助 - 如果你有的话,请评论 q。