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

时间:2015-09-03 15:40:57

标签: php wordpress-plugin woocommerce wordpress

我有一个Wordpress网站,我正在运行WooCommerce插件。在结帐页面上,客户要求提供他们交付的村庄的下拉列表,并要求将交付费用应用于订单。因此,订购物品的人将选择物品,进入结账,输入他们的地址,从下拉列表中选择他们的村庄,订单总额将被更改,然后支付更改的订单总额。

目前我的代码位于下方,只会在订单中添加下拉框。所有这些代码都是从自定义插件文件中添加的。

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' );
}

此刻此框如下: delivery charge box

总结一下,我的问题是:

1)是否有更新结帐页面结帐总额的功能?

2)我怎么称呼这个功能?是否可以调用select OnChange事件?如果没有,我可以在收货箱下方添加一个按钮来调用该功能吗?

请多问我一下,我确定自己错了,因为这是我第一次使用Wordpress Plugin API - 感谢所有回复。

1 个答案:

答案 0 :(得分:2)

解决了 - 这是怎么回事。添加了一个按钮和一个下拉列表,允许客户将货物作为产品添加到购物车,这是代码。

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重定向到将产品添加到购物车的页面,方法是使用select值的第一部分作为产品ID(必要的原因是PHP数组不能包含多个元素具有相同的标识符)。希望这有助于某人 - 如果你拥有它们,可以对q进行评论。

相关问题