问题
Here is the update of my code and I still can't add link break with the provided solution. Check out code below.
function gift_sender( $cart_item, $cart_item_key ) {
$gsender = isset( $cart_item['gsender'] ) ? $cart_item['gsender'] : '';
$gift_label_string = pll_e('Gift Receiver:');
printf(
'<br><div><label>%s</label> <textarea class="%s" id="cart_notes_%s" data-cart-id="%s">%s</textarea></div>',
'prefix-cart-notes',
$cart_item_key,
$cart_item_key,
$gsender
);
}
add_action( 'woocommerce_after_cart_item_name', 'gift_sender', 10, 2 );
function gift_receiver( $cart_item, $cart_item_key ) {
$greceiver = isset( $cart_item['greceiver'] ) ? $cart_item['greceiver'] : '';
$contact_label_string = pll_e('Contact No:');
printf(
'<br><div><label>%s</label> <textarea class="%s" id="rcart_notes_%s" data-cart-id="%s">%s</textarea></div>',
'prefix-cart-notes',
$cart_item_key,
$cart_item_key,
$greceiver
);
}
add_action( 'woocommerce_after_cart_item_name', 'gift_receiver', 10, 2 );
回答1:
If you inspect your code in the frontend with the browser, you can see the following:
Gift Receiver:<div><label></label> <textarea class="prefix-cart-notes" id="cart_notes_cb48bddd1d90be26114366aaaad95806" data-cart-id="cb48bddd1d90be26114366aaaad95806"></textarea></div>
So you see, the string is not inside the label tags. That is the reason why the break does not take place in the right place.
Please try so save the translated string in a variable before and then put this inside your printf.
function gift_sender( $cart_item, $cart_item_key ) {
$gsender = isset( $cart_item['gsender'] ) ? $cart_item['gsender'] : '';
$cart_label_string = pll_e('Gift Receiver:');
printf(
'<br><div><label>%s</label> <textarea class="%s" id="cart_notes_%s" data-cart-id="%s">%s</textarea></div>',
'prefix-cart-notes',
$cart_label_string,
$cart_item_key,
$cart_item_key,
$gsender);
}
add_action( 'woocommerce_after_cart_item_name', 'gift_sender', 10, 2 );
Hope this helps.
回答2:
I've found the solution for adding (br).
function gift_sender( $cart_item, $cart_item_key ) {
$gsender = isset( $cart_item['gsender'] ) ? $cart_item['gsender'] : '';
echo '<br/><br/><div><label>';
pll_e('Gift Receiver:');
echo '</label>';
printf(
'<textarea class="%s" id="cart_notes_%s" data-cart-id="%s">%s</textarea>',
'prefix-cart-notes',
$cart_item_key,
$cart_item_key,
$gsender
);
echo '</div>';
}
add_action( 'woocommerce_after_cart_item_name', 'gift_sender', 10, 2 );
function gift_receiver( $cart_item, $cart_item_key ) {
$greceiver = isset( $cart_item['greceiver'] ) ? $cart_item['greceiver'] : '';
echo '<div><label>';
pll_e('Contact No:');
echo '</label>';
printf(
'<textarea class="%s" id="rcart_notes_%s" data-cart-id="%s">%s</textarea>',
'prefix-cart-notes',
$cart_item_key,
$cart_item_key,
$greceiver
);
echo '</div>';
}
add_action( 'woocommerce_after_cart_item_name', 'gift_receiver', 10, 2 );
来源:https://stackoverflow.com/questions/61856779/br-link-break-code-not-working-in-function-php-polylang