0

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 );

2 Answers2

0

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.

rank
  • 2,361
  • 2
  • 10
  • 20
  • I saved the string variable but is not working too. –  May 17 '20 at 19:33
  • I checked your link, everything is working fine and the string is inside the label. Did you solve it some other way? If my solution did it, would be glad if you accept the answer. If not, please share your solution, I would like to know, thank you. – rank May 17 '20 at 19:41
  • No, I haven't resolved it. The solution are not working. https://jinyeye.com.my/staging1/cart/, you can add a product to the cart and give it a try. –  May 17 '20 at 19:59
  • When I took a look 20 minutes ago, it was displaying fine, but with the textareas. Where has the textarea gone? seems like a different markup now. You will have to change the code then. Can you update your code ? Edit your question and put a hint "EDIT: Changed the code... " so others know there was some progress. – rank May 17 '20 at 20:06
  • Please take a look at your question, something went wrong when you inserted the code. It would be great if you show the complete two functions how they look now. In your shopping cart there are no more any textareas, so this markup has to have been changed. Also you have overseen that I add the variable inside the printf ` 'prefix-cart-notes', $cart_label_string, ` But I think you should show us the complete functions how they look now. – rank May 17 '20 at 21:04
  • I've updated my code correctly. I can see it on my side. –  May 17 '20 at 21:08
  • But I cannot see the php code on your side ;) . If you want me to be able to help you, edit your question and paste the code (both functions) you are currently using. Otherwise I can not find out what's wrong. – rank May 17 '20 at 21:11
  • I can't edit and update my current code. So what should I do? –  May 17 '20 at 21:30
  • If you can provide your email I can send it to you. –  May 17 '20 at 21:31
  • Isn't there the "edit" link available for you beneath your question? I cannot give you my mail. But you can just make a jsfiddle (https://jsfiddle.net/) or similar to show me your code, if you have problems with SO editing – rank May 17 '20 at 21:36
  • When I want to save as draft it showing: Your post appears to contain code that is not properly formatted as code. Please indent all code by 4 spaces using the code toolbar button or the CTRL+K keyboard shortcut. For more editing help, click the [?] toolbar icon. –  May 17 '20 at 21:42
  • I think I just show you the screenshot of the current code: https://ibb.co/nm6M6yG –  May 17 '20 at 21:43
  • You forgot the variable as I said. In the printf you use %s. This is the placeholder for the variable you add at the end. $cart_item_key is used there twice, if you know the place I mean. Check my anser, there is one more variable. - But I'm not sure if it solves your issue. Can you try just putting a static text like "Gift receiver" between the label tags, instead of the %s. Just to see if this is being showed. I'm still wondering why there is no textarea, you have this in the code... – rank May 17 '20 at 22:07
  • I've added a static text to the label tags. Now the string got duplicated and on the Polylang strings translation tab, I was not able to find the string already. https://ibb.co/MBgYH8K https://ibb.co/hyFthyr –  May 17 '20 at 22:45
  • Ok looks better. So it works but the pll_e makes problems. Please take this ` $cart_label_string = pll_e('Gift Receiver:'); ` outside of your function and paste before the function. Than inside your function at the line where you just putted the code outside, you paste ` global $cart_label_string; ` Then insert the variable name as I did in my answer using %s and adding the variable name to the printf. We will get this going ;). your text is positioned the right way. – rank May 17 '20 at 23:20
  • to explain: I think the pll_e outputs directly. So you get the duplicate because its the first pll_e where you save it to value. Thats because we put it in variable outside the function and then get the value with global inside the function. – rank May 17 '20 at 23:22
  • I already told you this, but you seem not to notice ;) . You do not give the string in your printf. Take again a close look at my answer. You just add, $cart_item_key, $cart_item_key, $greceiver, but you have to put the variable $gift_label_string before.... please read my answer again with caution. – rank May 20 '20 at 10:55
0

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 );