我创建了一个自定义复选框,询问“订单是否是礼物?”在购物车页面中,但无法弄清楚当用户单击“继续结账”时如何保存其价值。价格不会受到影响。我为此创建了一个单独的 PHP 文件。该复选框是可见的,但我不知道该怎么做。此外,该值(如果选中)将显示在订单的管理端。
该复选框在购物车页面中可见。这是我的代码:
add_action('woocommerce_cart_totals_after_order_total', 'add_gift_checkbox_to_cart');
function add_gift_checkbox_to_cart() {
echo '<tr class="gift-checkbox"><th>' . __('Is this order a gift?', 'your-theme-textdomain') . '</th><td>';
woocommerce_form_field('is_gift', array(
'type' => 'checkbox',
'class' => array('input-checkbox'),
'label' => __('Yes, this order is a gift', 'your-theme-textdomain'),
), WC()->checkout->get_value('is_gift'));
echo '</td></tr>';
}
我是 WordPress 编码新手,我只知道核心 PHP。
以下代码将在购物车和结帐页面中显示您的自定义复选框。所选选项将通过 Ajax 设置为会话变量,并在下订单时保存。它将显示在管理订单、客户订单和电子邮件通知中。
// Display a custom checkbox on cart and checkout
add_action('woocommerce_cart_totals_after_order_total', 'display_gift_order_checkbox');
add_action('woocommerce_review_order_after_order_total', 'display_gift_order_checkbox');
function display_gift_order_checkbox() {
echo '<tr class="gift_order">
<th>' . __('Is this order a gift?', 'woocommerce') . '</th>
<td>';
woocommerce_form_field('gift_order', array(
'type' => 'checkbox',
'class' => array('input-checkbox'),
'label' => __('Yes, this order is a gift', 'woocommerce'),
), WC()->session->get('gift_order') === 'Yes' );
echo '</td></tr>';
}
// Remove "(optional)" text from custom fields
add_filter( 'woocommerce_form_field' , 'remove_optional_txt_from_checkbox_field', 10, 2 );
function remove_optional_txt_from_checkbox_field( $field, $key ) {
if( $key === 'gift_order' ) {
$optional = ' <span class="optional">(' . esc_html__( 'optional', 'woocommerce' ) . ')</span>';
$field = str_replace( $optional, '', $field );
}
return $field;
}
// AJAX REQUEST SENDER
add_action( 'wp_footer', 'gift_order_checkbox_script_js' );
function gift_order_checkbox_script_js() {
if ( is_cart() || ( is_checkout() && ! is_wc_endpoint_url() ) ) :
?>
<script>
jQuery(function($){
const eventToTrigger = '<?php echo is_cart() ? "added_to_cart" : "update_checkout"; ?>';
$(document.body).on('change', 'input[name=gift_order]', function(){
$.ajax({
type: 'POST',
url: woocommerce_params.ajax_url,
data: {
'action': 'gift_order_checkbox',
'gift_order': $(this).prop('checked') ? 'Yes' : 'No',
},
success: function (response) {
$(document.body).trigger(eventToTrigger);
},
});
});
});
</script>
<?php
endif;
}
// Get Ajax request and saving checkbox value to a WC Session variable
add_action( 'wp_ajax_gift_order_checkbox', 'gift_order_checkbox_to_wc_session' );
add_action( 'wp_ajax_nopriv_gift_order_checkbox', 'gift_order_checkbox_to_wc_session' );
function gift_order_checkbox_to_wc_session() {
if ( isset($_POST['gift_order']) && isset($_POST['gift_order']) ) {
WC()->session->set('gift_order', esc_attr($_POST['gift_order']));
wp_die(esc_attr($_POST['gift_order']));
}
wp_die('Something went wrong');
}
// Save checkbox selected value as order meta data
add_action( 'woocommerce_checkout_create_order', 'save_gift_order_session_variable_as_meta_data' );
function save_gift_order_session_variable_as_meta_data( $order ) {
$order->add_meta_data( 'gift_order', WC()->session->get('gift_order') === 'Yes' ? 'Yes' : 'No', true );
}
// Remove WC Session variable related to the checkbox
add_action( 'woocommerce_checkout_order_created', 'remove_gift_order_session_variable' );
function remove_gift_order_session_variable() {
WC()->session->__unset('gift_order');
}
// Display Gift order selected value in admin orders
add_action('woocommerce_admin_order_data_after_billing_address', 'admin_order_displays_gift_order_value');
function admin_order_displays_gift_order_value($order) {
$value = $order->get_meta('gift_order') ;
echo '<p><strong>' . __("Is a gift order", "woocommerce") . '</strong>: ' . $value . '</p>';
}
// Display on customer orders and email notifications
add_filter( 'woocommerce_get_order_item_totals', 'display_delivery_on_order_item_totals', 10, 3 );
function display_delivery_on_order_item_totals( $total_rows, $order, $tax_display ){
if( $gift_order = $order->get_meta( 'gift_order' ) ) {
$new_total_rows = [];
// Loop through order total rows
foreach( $total_rows as $key => $values ) {
$new_total_rows[$key] = $values;
// Inserting before payment method
if( $key === 'payment_method' ) {
$new_total_rows['gift_order'] = array(
'label' => __("Is a gift order", "woocommerce") . ':',
'value' => $gift_order,
);
}
}
return $new_total_rows;
}
return $total_rows;
}
代码位于子主题的functions.php 文件中(或插件中)。已测试并有效。
结账页面截图:
客户订单截图: