在Woocommerce中,我想基于自定义交付选项的自定义结帐选择字段来更新购物车总额。我在结帐页面上添加了一个新的选择字段。现在,当选择选项“доставка”时,我想将300加到购物车总计。
将选择字段添加到结帐页面:
add_action('woocommerce_before_order_notes', 'wps_add_select_checkout_field');
function wps_add_select_checkout_field( $checkout ) {
woocommerce_form_field( 'daypart', array(
'type' => 'select',
'class' => array( 'wps-drop', 'update_totals_on_change' ),
'label' => __( 'Способ доставки' ),
'options' => array(
'blank' => __( 'Выбирите из списка', 'wps' ),
'delivery' => __( 'доставка', 'wps' ),
'samovivoz' => __( 'самовывоз', 'wps' ),
),
), $checkout->get_value( 'daypart' ));
}
购物车总更新过滤器:
add_filter( 'woocommerce_calculated_total', 'change_calculated_total', 10, 2 );
function change_calculated_total( $total, $cart ) {
if ($_POST['daypart'] == "delivery") {
$total = $total + 300;
return $total;
} else {
return $total;
}
}
但是我的代码无法正常工作。我在做什么错了?