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

Posted

技术标签:

【中文标题】结帐页面上的 Woocommerce 更改结帐总数【英文标题】:Woocommerce Change Checkout Total on Checkout Page 【发布时间】:2015-11-29 13:10:42 【问题描述】:

我有一个运行 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) 结账页面有更新结账总额的功能吗?

2) 我该如何调用这个函数?是否可以调用 select OnChange 事件?如果没有,我可以在运费框下方添加一个按钮来调用该功能吗?

请多问我一下,我确定我有什么问题,因为这是我第一次使用 Wordpress 插件 API - 感谢所有回复。

【问题讨论】:

你需要做两件事。 1) 创建一个 AJAX 函数来发送用户选择并处理更改购物车。 2)在JS中的body元素上触发'update_checkout',以便检索新的购物车成本 【参考方案1】:

已解决 - 方法如下。添加了一个按钮和一个下拉菜单,允许客户将运输作为产品添加到购物车,代码如下。

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,请在评论中留言。

【讨论】:

以上是关于结帐页面上的 Woocommerce 更改结帐总数的主要内容,如果未能解决你的问题,请参考以下文章

Woocommerce 结帐页面上的额外贝宝费用

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

更改 Woocommerce 中的结帐订单审查部分

Woocommerce 结帐页面先显示送货地址,然后显示帐单地址?

如何在结帐时更改 WooCommerce 文本运输

在 Woocommerce 中更改结帐帐单字段占位符