更改添加分组产品时的WooCommerce验证文本

huangapple go评论86阅读模式
英文:

Change WooCommerce validation text when adding grouped products

问题

  1. // 更改 WooCommerce 文本:“Please choose a product to add to your cart…” 当向购物车中添加分组产品时。
  2. /**
  3. * 处理向购物车中添加分组产品。
  4. *
  5. * @since 2.4.6 从 add_to_cart_action 中分离出来。
  6. * @param int $product_id 要添加到购物车的产品 ID。
  7. * @return bool 是否成功
  8. */
  9. private static function add_to_cart_handler_grouped( $product_id ) {
  10. $was_added_to_cart = false;
  11. $added_to_cart = array();
  12. $items = isset( $_REQUEST['quantity'] ) && is_array( $_REQUEST['quantity'] ) ? wp_unslash( $_REQUEST['quantity'] ) : array(); // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
  13. if ( ! empty( $items ) ) {
  14. $quantity_set = false;
  15. foreach ( $items as $item => $quantity ) {
  16. $quantity = wc_stock_amount( $quantity );
  17. if ( $quantity <= 0 ) {
  18. continue;
  19. }
  20. $quantity_set = true;
  21. // 添加到购物车的验证。
  22. $passed_validation = apply_filters( 'woocommerce_add_to_cart_validation', true, $item, $quantity );
  23. // 在完成之前取消总计重新计算。
  24. remove_action( 'woocommerce_add_to_cart', array( WC()->cart, 'calculate_totals' ), 20, 0 );
  25. if ( $passed_validation && false !== WC()->cart->add_to_cart( $item, $quantity ) ) {
  26. $was_added_to_cart = true;
  27. $added_to_cart[ $item ] = $quantity;
  28. }
  29. add_action( 'woocommerce_add_to_cart', array( WC()->cart, 'calculate_totals' ), 20, 0 );
  30. }
  31. if ( ! $was_added_to_cart && ! $quantity_set ) {
  32. wc_add_notice( __( '请选择您想要添加到购物车的商品数量…', 'woocommerce' ), 'error' );
  33. } elseif ( $was_added_to_cart ) {
  34. wc_add_to_cart_message( $added_to_cart );
  35. WC()->cart->calculate_totals();
  36. return true;
  37. }
  38. } elseif ( $product_id ) {
  39. /* 在产品目录上的链接 */
  40. wc_add_notice( __( '请选择要添加到购物车的产品…', 'woocommerce' ), 'error' );
  41. }
  42. return false;
  43. }
  44. 我不擅长编写自定义代码,但我想添加自己的自定义消息。
英文:

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.

  1. /**
  2. * Handle adding grouped products to the cart.
  3. *
  4. * @since 2.4.6 Split from add_to_cart_action.
  5. * @param int $product_id Product ID to add to the cart.
  6. * @return bool success or not
  7. */
  8. private static function add_to_cart_handler_grouped( $product_id ) {
  9. $was_added_to_cart = false;
  10. $added_to_cart = array();
  11. $items = isset( $_REQUEST[&#39;quantity&#39;] ) &amp;&amp; is_array( $_REQUEST[&#39;quantity&#39;] ) ? wp_unslash( $_REQUEST[&#39;quantity&#39;] ) : array(); // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
  12. if ( ! empty( $items ) ) {
  13. $quantity_set = false;
  14. foreach ( $items as $item =&gt; $quantity ) {
  15. $quantity = wc_stock_amount( $quantity );
  16. if ( $quantity &lt;= 0 ) {
  17. continue;
  18. }
  19. $quantity_set = true;
  20. // Add to cart validation.
  21. $passed_validation = apply_filters( &#39;woocommerce_add_to_cart_validation&#39;, true, $item, $quantity );
  22. // Suppress total recalculation until finished.
  23. remove_action( &#39;woocommerce_add_to_cart&#39;, array( WC()-&gt;cart, &#39;calculate_totals&#39; ), 20, 0 );
  24. if ( $passed_validation &amp;&amp; false !== WC()-&gt;cart-&gt;add_to_cart( $item, $quantity ) ) {
  25. $was_added_to_cart = true;
  26. $added_to_cart[ $item ] = $quantity;
  27. }
  28. add_action( &#39;woocommerce_add_to_cart&#39;, array( WC()-&gt;cart, &#39;calculate_totals&#39; ), 20, 0 );
  29. }
  30. if ( ! $was_added_to_cart &amp;&amp; ! $quantity_set ) {
  31. wc_add_notice( __( &#39;Please choose the quantity of items you wish to add to your cart…&#39;, &#39;woocommerce&#39; ), &#39;error&#39; );
  32. } elseif ( $was_added_to_cart ) {
  33. wc_add_to_cart_message( $added_to_cart );
  34. WC()-&gt;cart-&gt;calculate_totals();
  35. return true;
  36. }
  37. } elseif ( $product_id ) {
  38. /* Link on product archives */
  39. wc_add_notice( __( &#39;Please choose a product to add to your cart…&#39;, &#39;woocommerce&#39; ), &#39;error&#39; );
  40. }
  41. return false;
  42. }

I am not good at this custom code, but I would like to add my own custom message.

答案1

得分: 0

  1. add_filter('gettext_woocommerce', 'translate_woocommerce_strings_cart');
  2. function translate_woocommerce_strings_cart($string) {
  3. if ('Please choose a product to add to your cart&hellip;' === $string) {
  4. $string = esc_html__('Please choose a product.', 'woocommerce');
  5. }
  6. return $string;
  7. }
英文:
  1. add_filter(&#39;gettext_woocommerce&#39;, &#39;translate_woocommerce_strings_cart&#39;);
  2. function translate_woocommerce_strings_cart($string) {
  3. if (&#39;Please choose a product to add to your cart&amp;hellip;&#39; === $string) {
  4. $string = esc_html__(&#39;Please choose a product.&#39;, &#39;woocommerce&#39;);
  5. }
  6. return $string;
  7. }

huangapple
  • 本文由 发表于 2023年2月18日 12:04:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/75491105.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定