2

I am creating a magento2 module and I want to add an html block to selected shipping method: I am using jQuery to achieve this but I will accept other workarounds all I want is to make this work.

I tried this :

https://magento.stackexchange.com/a/162967/78274

But the problem is the block is added after all shipping methods which isn't what I am looking for. However I tried to use it.

I used jQuery and managed to add my block under selected shipping method but my problem is this works only the first time I load the page when mychippingmethod is selected by default, but when I reload the page even though mychippingmethod is selected by default nothing is added. But if I click on my shipping method my block is added.

Then I tried to switch between after and append but nothing changed I had the same problem.

What I tried so far :

jQuery("tr:contains('mychippingmethod')").append("#my-carrier-custom-block-wrapper");

I also tried

var myelement = "example";
jQuery("tr:contains('mychippingmethod')").after(myelement);

Even trying to append a simple text doesn't work.

I wouldn't say that magento2 isn't allowing me to edit the shipping block since it does work the first time and this is what's making me frustrated why would it work the first time and then stops.

I would accept a solution to uncheck all the shipping methods by default too.

EDIT :

If i append :

<tr class=\"row\"><td>aaaaaaaaa</td></tr>

My after function doesn't work even in the first time but If i select my shippingmethode my text gets appended ! so maybe I am adding it wrong

Please help.

Mukesh
1,4844 gold badges29 silver badges59 bronze badges
asked Apr 5, 2019 at 21:02
4
  • have you tried using checkout LayoutProcessor? Commented Apr 6, 2019 at 10:45
  • No I didn't and I don't know how to use it. With some googling it looks very complex to deal with I am new to magento (>2 weeks) so I am trying to do everything as simple as possible. I will look at it later if I find no other workaround. Thanks for your help ! Commented Apr 6, 2019 at 11:30
  • what is your expected output here? Maybe I can help you. Do you want to add a label below of your custom shipping method when selected? Is that right? Commented Apr 6, 2019 at 11:32
  • I want to add an <iframe> of a cart under myshippingmethod when selected. Commented Apr 6, 2019 at 11:55

1 Answer 1

0

I was able to find a workaround with setInterval ... the problem is that when I execute my script the table containing

jQuery("tr:contains('mychippingmethod')")

is still used by Magento so he doesn't allow me to change it until he finishes loading the whole table. So what I did is looking for my appended html tag with id as long as it's not added my code get executed again:

if (selectedMethod == 'myshippingmethod') {
 var checkExist = setInterval(function() {
 if ($("#myid").length == 0) {
 jQuery("tr:contains('myshippingmethod')").after(myelement);
 clearInterval(checkExist);
 }
 }, 100);
 }

Hope this helps

answered Apr 7, 2019 at 19:24

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.