Woocommerce更改结帐页面上的结帐总额

詹姆斯·帕特森

我有一个正在运行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 &amp; 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 &amp; Lt Wilbraham - £8.00', 
        '6.00-wit' => 'Withersfield - £6.00', 
        '8.00-wra' => 'Gt &amp; 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) Is there a function to update the checkout total on the checkout page?

2) How would I call this function? Is it possible to call on the select OnChange event? If not could I add a button below the delivery charge box to call the function?

Please question me more, I'm sure I've got something wrong as it's my first time with the Wordpress Plugin API - And thanks for any and all replies.

James Paterson

Solved - Here's how. Added a button and a dropdown allowing customers to add the shipping as a product to the cart, here's the code.

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 &amp; 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 &amp; Lt Wilbraham - £8.00', 
            '2470-wit' => 'Withersfield - £6.00', 
            '2471-wra' => 'Gt &amp; Lt Wratting - £8.00', 
            )
        ),$checkout->get_value('town_dropdown')
    );
    echo '<button type="button" OnClick="AddProductToCart()">Add Shipping</button>';
    echo '</div>';
}

单击按钮后,Javascript重定向到将选择值的第一部分用作产品ID的产品添加到购物车的页面(之所以需要这样做,是因为PHP数组不能具有相同的多个元素标识符)。希望这对某人有帮助-如果有q,请用q进行评论。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

更改 Woocommerce 结帐页面上的措辞

仅在结帐页面上更改WooCommerce送货方式标签

Woocommerce结帐页面上的额外Paypal费用

Woocommerce在结帐页面上删除运费

在某些情况下,如何在WooCommerce的结帐页面上更改“您的订单”文本

在结帐页面上更改WooCommerce优惠券的“删除”链接文本

如何使用钩子更改 WooCommerce 结帐页面上的“帐单详细信息”标题

简单的woocommerce结帐页面

在WooCommerce结帐页面上显示产品类别

在WooCommerce结帐页面上获取实时的客户送货位置

如何在WordPress WooCommerce结帐页面上使用jQuery

取消Woocommerce结帐页面上的优惠券表单

WooCommerce:在结帐页面上停止滚动到错误消息

在WooCommerce结帐页面上的输入字段下显示描述

WooCommerce:我想更新结帐页面上的标签

如何从购物车和结帐页面woocommerce删除订单总额

在Prestashop的COD结帐页面上更改默认的“免费”文本

在 WooCommerce 购物车和结帐页面上的购物车中的特定产品时更改“总计”文本

woocommerce在结帐和购物车页面中更改价格

条纹结帐-结帐页面上是否可以显示多个图像?

Woocommerce 更改结帐标题格式

如何更改WooCommerce结帐网址?

在Stripe结帐页面上显示计划的描述

删除结帐页面上的搜索字段(Magento)

wordpress 结帐页面上的奇怪文本 (CSS)

快递结帐订单总额缺失

在 WooCommerce 结帐页面上显示自定义字段产品信息

WooCommerce PayPal Checkout Gateway插件的结帐页面上的替换图标

在结帐WooCommerce页面上获取购物车产品ID,以显示产品图片