1

We are adding shipping price of quote object to myTestData which is pulled onto the page using window.checkoutConfig.myTestData:

use Magento\Checkout\Model\ConfigProviderInterface;

class Config implements ConfigProviderInterface
{
  protected $checkoutSession;

  public function __construct(
    \Magento\Checkout\Model\Session $checkoutSession
  ) {
    $this->checkoutSession = $checkoutSession;
  }

  /* Get Quote */
  public function getQuote()
  {
    return $this->checkoutSession->getQuote();
  }

  /* Return Config Details */
  public function getConfig()
  {
    $config = [];
    $config['myTestData'] = $this->getQuote()->getShippingAddress()->getShippingAmount();

    return $config;
  }

This returns the proper shipping amount on first load of the cart, but if we change shipping method, myTestData does not get updated to reflect the new value. How can we make sure window.checkoutConfig.myTestData gets refreshed if / when cart is updated?

EDIT, if not using checkoutConfig, is there another way to pass dynamic block variables to javascript object any time the cart updates?

1
  • 1
    if my answer helped kindly accept that
    – Asad Ullah
    Commented Jul 28, 2020 at 15:20

1 Answer 1

1

PHP functions run when page is reload so you can do this in you custom controller what ever you want

steps

  1. create a ajax call on shipping select to a controller

like on shipping select radio button add

onlick="myfunction()"

and in js file use like this

   function myfunction() {
    jQuery.ajax({
        url: '/routename/controller_folder/controller_file',
        type: 'POST',
        dataType: 'json',
        data: {
            yourvariable: your_variable_value
        },
        complete: function(response) {

        }
    });
}
  1. in your controller get that value in execute function like

    $yourvariable = $this->getRequest()->getParam('yourvariable');

now do what ever you like with this variable

By this when ever the shipping method changes it will call that ajax with updated JS variable

hope you get my point if need more details feel free to comment if helps then up-vote please

8
  • Thanks @Asad, using this would it be possible to update checkoutConfig.myTestData with updated value? I any case, this does give me a bit of a better idea on some things to try.
    – AJK
    Commented Jul 28, 2020 at 15:21
  • i think if you send that param with ajax call it will update on every click so you will get that updated value in controller
    – Asad Ullah
    Commented Jul 28, 2020 at 15:22
  • The checkout is built with UI Components so I recommend using those not jQuery, else you may end up in a situation where jQuery and Knockout are conflicting.
    – Ben Crook
    Commented Jul 28, 2020 at 15:58
  • Thanks @AsadUllah this ended up working by creating a controller and calling that via ajax and binding that to checkout button.
    – AJK
    Commented Jul 28, 2020 at 21:45
  • "binding that to checkout button." how did you do this?
    – Jack
    Commented Mar 12, 2021 at 10:04

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.