WooCommerce:避免结帐如果未填写所有购物车自定义字段 [英] WooCommerce: Avoid checkout If all cart item custom fields are not filled in
问题描述
我编写了一个功能,当每个产品中的其他字段(在购物车中显示)还没有完成时(仅当购物车中有两个以上项目时,这些字段才会出现),才能禁用订购按钮.
I wrote a function that disables the ordering button when additional fields (with display in cart) in each of the products have not been completed (fields appear only if there are more than 2 items in the cart).
function disable_checkout_button_no_shipping() {
global $woocommerce;
$quantity = $woocommerce->cart->cart_contents_count;
if( $quantity > 1 ){
foreach ( WC()->cart->get_cart() as $cart_item ) {
$item_name = $cart_item['data']->get_title();
$notes = $cart_item['notes'];
printf("[%s]\n", $notes);
if ( empty( $notes ) ) {
remove_action( 'woocommerce_proceed_to_checkout', 'woocommerce_button_proceed_to_checkout', 20 );
printf(
'<div class="wc-cart-note-warning"><span>Przed złożeniem zamówienia dodaj informację dla kogo jest kartka!</span></div>',
);
}
}
}
}
add_action( 'woocommerce_proceed_to_checkout', 'disable_checkout_button_no_shipping', 1 );
但是它仅在浏览器中刷新页面时有效.
But it only works when the page is refreshed in the browser.
每当在分配给产品的文本字段中输入某些内容时,如何使函数刷新,带有订购"按钮的部分将用ajax刷新?
How can I make the function refresh every time something is entered in the text field assigned to the product and the section with the order button will be refreshed with ajax?
下面是负责添加注释的代码:
Below is the code that is responsible for adding notes:
Javascrit代码(保存在 update-cart-item-ajax.js
文件名(自定义文件) 下):
Javascrit code saved under update-cart-item-ajax.js
file name (custom file):
(function($) {
// $(document).ready(function(){
$('.prefix-cart-notes').on('change keyup paste', function() {
$('.cart_totals').block({
message: null,
overlayCSS: {
background: '#fff',
opacity: 0.6
}
});
var cart_id = $(this).data('cart-id');
$.ajax({
type: 'POST',
url: prefix_vars.ajaxurl,
data: {
action: 'prefix_update_cart_notes',
security: $('#woocommerce-cart-nonce').val(),
notes: $('#cart_notes_' + cart_id).val(),
cart_id: cart_id
},
success: function(response) {
$('.cart_totals').unblock();
}
})
});
});
PHP代码保存在活动主题 functions.php
文件中:
PHP code saved in active theme functions.php
file:
/**
* Add a text field to each cart item
*/
function prefix_after_cart_item_name( $cart_item, $cart_item_key ) {
$notes = isset( $cart_item['notes'] ) ? $cart_item['notes'] : '';
global $woocommerce;
$quantity = $woocommerce->cart->cart_contents_count;
if( $quantity > 1 ){
printf(
'<div><textarea rows="1" placeholder="Dla kogo jest kartka?" maxlength="30" class="%s" id="cart_notes_%s" data-cart-id="%s">%s</textarea></div>',
'prefix-cart-notes',
$cart_item_key,
$cart_item_key,
$notes
);
}
}
add_action( 'woocommerce_after_cart_item_name', 'prefix_after_cart_item_name', 10, 2 );
/**
* Enqueue our JS file
*/
function prefix_enqueue_scripts() {
//trailingslashit( plugin_dir_url( __FILE__ ) )
wp_register_script( 'prefix-script', trailingslashit( get_home_url() ) . '/wp-content/themes/boomb_theme/assets/update-cart-item-ajax.js', array( 'jquery-blockui' ), time(), true );
wp_localize_script(
'prefix-script',
'prefix_vars',
array(
'ajaxurl' => admin_url( 'admin-ajax.php' )
)
);
wp_enqueue_script( 'prefix-script' );
}
add_action( 'wp_enqueue_scripts', 'prefix_enqueue_scripts' );
/**
* Update cart item notes
*/
function prefix_update_cart_notes() {
// Do a nonce check
if( ! isset( $_POST['security'] ) || ! wp_verify_nonce( $_POST['security'], 'woocommerce-cart' ) ) {
wp_send_json( array( 'nonce_fail' => 1 ) );
exit;
}
// Save the notes to the cart meta
$cart = WC()->cart->cart_contents;
$cart_id = $_POST['cart_id'];
$notes = $_POST['notes'];
$cart_item = $cart[$cart_id];
$cart_item['notes'] = $notes;
WC()->cart->cart_contents[$cart_id] = $cart_item;
WC()->cart->set_session();
wp_send_json( array( 'success' => 1 ) );
exit;
}
add_action( 'wp_ajax_prefix_update_cart_notes', 'prefix_update_cart_notes' );
add_action( 'wp_ajax_nopriv_prefix_update_cart_notes', 'prefix_update_cart_notes' );
function prefix_checkout_create_order_line_item( $item, $cart_item_key, $values, $order ) {
if( isset( $values['notes'] ) ) {
$item->add_meta_data( 'notes', $values['notes'], true );
}
}
add_action( 'woocommerce_checkout_create_order_line_item', 'prefix_checkout_create_order_line_item', 10, 4 );
推荐答案
在您的情况下,为避免结帐,请改用 woocommerce_checkout_process
专用钩子,如:
In your case, to avoid checkout, use instead woocommerce_checkout_process
dedicated hook like:
add_action( 'woocommerce_checkout_process', 'check_cart_notes_to_disable_checkout' );
function check_cart_notes_to_disable_checkout() {
foreach ( WC()->cart->get_cart() as $cart_item ) {
if ( ! isset($cart_item['notes']) || empty($cart_item['notes']) ) {
$button = '<a href="'. wc_get_cart_url() .'" tabindex="1" class="button wc-forward">'. __("Back to cart", "woocommerce") .'</a>';
wc_add_notice( $button . __("Przed złożeniem zamówienia dodaj informację dla kogo jest kartka!", "woocommerce"), 'error' );
}
}
}
代码进入活动子主题(或活动主题)的functions.php文件中.经过测试,可以正常工作.
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
这篇关于WooCommerce:避免结帐如果未填写所有购物车自定义字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!