英文:
Change WooCommerce validation text when adding grouped products
问题
// 更改 WooCommerce 文本:“Please choose a product to add to your cart…” 当向购物车中添加分组产品时。
/**
* 处理向购物车中添加分组产品。
*
* @since 2.4.6 从 add_to_cart_action 中分离出来。
* @param int $product_id 要添加到购物车的产品 ID。
* @return bool 是否成功
*/
private static function add_to_cart_handler_grouped( $product_id ) {
$was_added_to_cart = false;
$added_to_cart = array();
$items = isset( $_REQUEST['quantity'] ) && is_array( $_REQUEST['quantity'] ) ? wp_unslash( $_REQUEST['quantity'] ) : array(); // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
if ( ! empty( $items ) ) {
$quantity_set = false;
foreach ( $items as $item => $quantity ) {
$quantity = wc_stock_amount( $quantity );
if ( $quantity <= 0 ) {
continue;
}
$quantity_set = true;
// 添加到购物车的验证。
$passed_validation = apply_filters( 'woocommerce_add_to_cart_validation', true, $item, $quantity );
// 在完成之前取消总计重新计算。
remove_action( 'woocommerce_add_to_cart', array( WC()->cart, 'calculate_totals' ), 20, 0 );
if ( $passed_validation && false !== WC()->cart->add_to_cart( $item, $quantity ) ) {
$was_added_to_cart = true;
$added_to_cart[ $item ] = $quantity;
}
add_action( 'woocommerce_add_to_cart', array( WC()->cart, 'calculate_totals' ), 20, 0 );
}
if ( ! $was_added_to_cart && ! $quantity_set ) {
wc_add_notice( __( '请选择您想要添加到购物车的商品数量…', 'woocommerce' ), 'error' );
} elseif ( $was_added_to_cart ) {
wc_add_to_cart_message( $added_to_cart );
WC()->cart->calculate_totals();
return true;
}
} elseif ( $product_id ) {
/* 在产品目录上的链接 */
wc_add_notice( __( '请选择要添加到购物车的产品…', 'woocommerce' ), 'error' );
}
return false;
}
我不擅长编写自定义代码,但我想添加自己的自定义消息。
英文:
Want to change the WooCommerce text Please choose a product to add to your cart… for when adding grouped products to the cart.
I see it here - see below - but I would like to create a snippet code that I can add to my custom functions instead of touching the code.
/**
* Handle adding grouped products to the cart.
*
* @since 2.4.6 Split from add_to_cart_action.
* @param int $product_id Product ID to add to the cart.
* @return bool success or not
*/
private static function add_to_cart_handler_grouped( $product_id ) {
$was_added_to_cart = false;
$added_to_cart = array();
$items = isset( $_REQUEST['quantity'] ) && is_array( $_REQUEST['quantity'] ) ? wp_unslash( $_REQUEST['quantity'] ) : array(); // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
if ( ! empty( $items ) ) {
$quantity_set = false;
foreach ( $items as $item => $quantity ) {
$quantity = wc_stock_amount( $quantity );
if ( $quantity <= 0 ) {
continue;
}
$quantity_set = true;
// Add to cart validation.
$passed_validation = apply_filters( 'woocommerce_add_to_cart_validation', true, $item, $quantity );
// Suppress total recalculation until finished.
remove_action( 'woocommerce_add_to_cart', array( WC()->cart, 'calculate_totals' ), 20, 0 );
if ( $passed_validation && false !== WC()->cart->add_to_cart( $item, $quantity ) ) {
$was_added_to_cart = true;
$added_to_cart[ $item ] = $quantity;
}
add_action( 'woocommerce_add_to_cart', array( WC()->cart, 'calculate_totals' ), 20, 0 );
}
if ( ! $was_added_to_cart && ! $quantity_set ) {
wc_add_notice( __( 'Please choose the quantity of items you wish to add to your cart…', 'woocommerce' ), 'error' );
} elseif ( $was_added_to_cart ) {
wc_add_to_cart_message( $added_to_cart );
WC()->cart->calculate_totals();
return true;
}
} elseif ( $product_id ) {
/* Link on product archives */
wc_add_notice( __( 'Please choose a product to add to your cart…', 'woocommerce' ), 'error' );
}
return false;
}
I am not good at this custom code, but I would like to add my own custom message.
答案1
得分: 0
add_filter('gettext_woocommerce', 'translate_woocommerce_strings_cart');
function translate_woocommerce_strings_cart($string) {
if ('Please choose a product to add to your cart…' === $string) {
$string = esc_html__('Please choose a product.', 'woocommerce');
}
return $string;
}
英文:
add_filter('gettext_woocommerce', 'translate_woocommerce_strings_cart');
function translate_woocommerce_strings_cart($string) {
if ('Please choose a product to add to your cart&hellip;' === $string) {
$string = esc_html__('Please choose a product.', 'woocommerce');
}
return $string;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论