我想更改此訊息:您無法將另一個“%s”添加到您的購物車。
如果產品有變數,則輸出 %s 如下:產品名稱 | 變數1,變數2
我希望輸出是這樣的:產品名稱 | 變數1,變數2
','我想改成' , '
我找到了文本掛鉤,但我不知道是否應該使用它來更改更改','為' , '. 有什么建議嗎?
路徑:plugins/woocommerce/includes/class-wc-cart.php
// Force quantity to 1 if sold individually and check for existing item in cart.
if ( $product_data->is_sold_individually() ) {
$quantity = apply_filters( 'woocommerce_add_to_cart_sold_individually_quantity', 1, $quantity, $product_id, $variation_id, $cart_item_data );
$found_in_cart = apply_filters( 'woocommerce_add_to_cart_sold_individually_found_in_cart', $cart_item_key && $this->cart_contents[ $cart_item_key ]['quantity'] > 0, $product_id, $variation_id, $cart_item_data, $cart_id );
if ( $found_in_cart ) {
/* translators: %s: product name */
$message = sprintf( __( 'You cannot add another "%s" to your cart.', 'woocommerce' ), $product_data->get_name() );
/**
* Filters message about more than 1 product being added to cart.
*
* @since 4.5.0
* @param string $message Message.
* @param WC_Product $product_data Product data.
*/
$message = apply_filters( 'woocommerce_cart_product_cannot_add_another_message', $message, $product_data );
throw new Exception( sprintf( '<a href="%s" >%s</a> %s', wc_get_cart_url(), __( 'View cart', 'woocommerce' ), $message ) );
}
}
uj5u.com熱心網友回復:
如您所見,您的代碼包含woocommerce_cart_product_cannot_add_another_message過濾器掛鉤,它允許您編輯$message. 然后你可以使用str_replace()
所以你得到:
/**
* Filters message about more than 1 product being added to cart.
*
* @since 4.5.0
* @param string $message Message.
* @param WC_Product $product_data Product data.
*/
function filter_woocommerce_cart_product_cannot_add_another_message( $message, $product_data ) {
// Replace all occurrences of the search string with the replacement string
$product_data_name = str_replace( ',', ' , ', $product_data->get_name() );
// New text
$message = sprintf( __( 'You cannot add another "%s" to your cart.', 'woocommerce' ), $product_data_name );
return $message;
}
add_filter( 'woocommerce_cart_product_cannot_add_another_message', 'filter_woocommerce_cart_product_cannot_add_another_message', 10, 2 );
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/476053.html
標籤:php WordPress woocommerce 产品 大车
