在 Woocommerce 商店中,我尝试在结账时为“手写卡”(或任何人所说的任何名称)创建一个复选框。我创建该字段,在结账时保存它并在管理页面上适当地显示它。这里没有问题。
但是,我想为此添加费用。我对该字段进行了检查,但我不知道如何传递数据以节省费用。我尝试了以下操作,但费用出现在最终购物车表中,但未计算或显示在感谢页面或管理>订单上。
我尝试过的:
<?php
//add card fee if checkbox field is checked
add_action( 'woocommerce_cart_calculate_fees', 'add_custom_fee' );
function add_custom_fee( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) ){return;}
//if on post data there is gift_card_checkbox that is 'checked' then add fee
parse_str($_POST['post_data'], $post_data);
if ( isset( $post_data['gift_card_checkbox'] ) && $post_data['gift_card_checkbox'] == 1 ) {
$fee = 1;
WC()->cart->add_fee('Gift card', $fee, true, '');
}
}
add_action( 'woocommerce_checkout_create_order', 'add_custom_fee_checkout', 20, 2 );
function add_custom_fee_checkout( $order, $data ) {
$gift_card = get_post_meta($order->get_id(), 'gift_card_checkbox', true);
if ($gift_card && $gift_card == 1) {
//if (isset($_POST['gift_card_checkbox']) && $_POST['gift_card_checkbox'] == 1) {
//test1
$order->add_fee('Gift card 1', 1);
//test2
$order->add_item(
new WC_Order_Item_Fee(
array(
'name' => "Gift card 2",
'amount' => 2,
'tax_status' => 'taxable',
'total' => 2
)
)
);
//test3
WC()->cart->add_fee('Gift card 3', 3, true, '');
}
}
?>
将自定义复选框字段添加到 WooCommerce 结账页面的订单备注部分之后。它使用
woocommerce_after_order_notes
操作挂钩来添加字段。 add_custom_checkbox_to_checkout
函数负责渲染复选框字段。它使用 woocommerce_form_field
函数生成复选框字段 HTML。使用 WC()->session->get('handwritten_card')
从 WooCommerce 会话中检索复选框的值。
// Add the custom checkbox field to the checkout page
add_action( 'woocommerce_after_order_notes', 'add_custom_checkbox_to_checkout' );
function add_custom_checkbox_to_checkout( $checkout ) {
echo '<div id="custom_checkbox_field">';
// Generate the checkbox field with the label "Handwritten Card"
woocommerce_form_field( 'handwritten_card', array(
'type' => 'checkbox',
'class' => array( 'input-checkbox' ),
'label' => 'Handwritten Card',
), WC()->session->get( 'handwritten_card' ));
echo '</div>';
}
使用
wp_footer
操作挂钩将 JavaScript 脚本添加到页面的页脚。该脚本使用 jQuery 来检测复选框的更改事件,并在复选框更改时触发 update_checkout
事件。此事件用于更新结帐页面并重新计算总计。
// Add JavaScript script to handle checkbox change event
add_action( 'wp_footer', 'custom_checkbox_script' );
function custom_checkbox_script() {
?>
<script type="text/javascript">
jQuery(document).ready(function($) {
// Detect checkbox change event
$(document).on('change', '#handwritten_card', function() {
$(document.body).trigger('update_checkout');
});
});
</script>
<?php
}
下面的代码定义了
woo_add_cart_fee
函数,该函数与 woocommerce_cart_calculate_fees
操作挂钩。此函数负责在选中复选框时向购物车添加费用,并在取消选中复选框时删除费用。它首先检查请求是否是有效的提交,然后解析 POST 数据以检索复选框值。会话中的复选框值重置为空。如果选中该复选框,则会使用 $woocommerce->cart->add_fee()
将价值为 2 的名为“礼品卡”的费用添加到购物车。它还使用 WC()->session->set('handwritten_card', sanitize_text_field($data['handwritten_card']))
在 WooCommerce 会话中设置复选框值。如果未选中该复选框,则会使用 $woocommerce->cart->remove_fee()
从购物车中删除“礼品卡”费用。
// Add or remove a fee based on the checkbox value
function woo_add_cart_fee() {
global $woocommerce;
// Check if the request is a valid submission
if ( ! $_POST || ( is_admin() && ! is_ajax() ) ) {
return;
}
// Parse the POST data to retrieve the checkbox value
parse_str($_POST['post_data'], $data);
// Reset the checkbox value in the session
WC()->session->set( 'handwritten_card', null );
// Check if the checkbox is checked
if ( isset( $data['handwritten_card'] ) && $data['handwritten_card'] == 1 ) {
// Add a fee to the cart named "Gift card" with a value of 2
$woocommerce->cart->add_fee( 'Gift card', 2, true, '' );
// Set the checkbox value in the session
WC()->session->set( 'handwritten_card', sanitize_text_field( $data['handwritten_card'] ) );
} else {
// Remove the "Gift card" fee from the cart if the checkbox is unchecked
foreach ( $woocommerce->cart->get_fees() as $fee_key => $fee ) {
if ( $fee->name === 'Gift card' ) {
$woocommerce->cart->remove_fee( $fee_key );
break;
}
}
}
}
add_action( 'woocommerce_cart_calculate_fees', 'woo_add_cart_fee' );
输出
如果有人遇到这个问题,想知道为什么费用显示在结帐页面上,而不是显示在感谢页面或订单中......我发生这种情况的原因是我在使用
$_POST['post_data']
结账页面获取表单字段。
我在另一个 stackoverflow 问题上发布了我的解决方案:https://stackoverflow.com/a/77656386/4484799