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

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

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[&#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
if ( ! empty( $items ) ) {
$quantity_set = false;
foreach ( $items as $item =&gt; $quantity ) {
$quantity = wc_stock_amount( $quantity );
if ( $quantity &lt;= 0 ) {
continue;
}
$quantity_set = true;
// Add to cart validation.
$passed_validation = apply_filters( &#39;woocommerce_add_to_cart_validation&#39;, true, $item, $quantity );
// Suppress total recalculation until finished.
remove_action( &#39;woocommerce_add_to_cart&#39;, array( WC()-&gt;cart, &#39;calculate_totals&#39; ), 20, 0 );
if ( $passed_validation &amp;&amp; false !== WC()-&gt;cart-&gt;add_to_cart( $item, $quantity ) ) {
$was_added_to_cart      = true;
$added_to_cart[ $item ] = $quantity;
}
add_action( &#39;woocommerce_add_to_cart&#39;, array( WC()-&gt;cart, &#39;calculate_totals&#39; ), 20, 0 );
}
if ( ! $was_added_to_cart &amp;&amp; ! $quantity_set ) {
wc_add_notice( __( &#39;Please choose the quantity of items you wish to add to your cart…&#39;, &#39;woocommerce&#39; ), &#39;error&#39; );
} elseif ( $was_added_to_cart ) {
wc_add_to_cart_message( $added_to_cart );
WC()-&gt;cart-&gt;calculate_totals();
return true;
}
} elseif ( $product_id ) {
/* Link on product archives */
wc_add_notice( __( &#39;Please choose a product to add to your cart…&#39;, &#39;woocommerce&#39; ), &#39;error&#39; );
}
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&hellip;' === $string) {
        $string = esc_html__('Please choose a product.', 'woocommerce');
    }
    return $string;
}
英文:
add_filter(&#39;gettext_woocommerce&#39;, &#39;translate_woocommerce_strings_cart&#39;);
function translate_woocommerce_strings_cart($string) {
if (&#39;Please choose a product to add to your cart&amp;hellip;&#39; === $string) {
$string = esc_html__(&#39;Please choose a product.&#39;, &#39;woocommerce&#39;);
}
return $string;
}

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:

确定