How To Programmatically Change Shipping Method?

Can someone please tell me how I can let the user change the shipping method by allowing him to press a custom button?

(edited previous question for clarity)


Please describe an example

There are two buttons: "No shipping required" and "Shipping required"

If I click on Shipping required it opens the shipping estimation form.

If I click on "No shipping required" I want it to reset the chosen shipping method if already selected in the shipping estimation form.

I've made a controller called "change_shipping.php"

use Tygh\Registry;

if (!defined(‘BOOTSTRAP’)) { die(‘Access denied’); }

if ($mode == “update_to_free”) {
$cart = &Tygh::$app[‘session’][‘cart’];

and in the frontend I made a form like this


but with no success.

It is required to make some tests. Unfortunately, I cannot provide you with the quick solution

The guys from the Help Desk were able to help me with this one. So in case anyone needs this functionality you don't need to create an additional controller, you can just use the existing checkout.update_shipping controller.

I've used this code in the file which has the checkout_totals hook included. Here are the steps:

1. Create the design/themes/[ACTIVE_THEME]/templates/addons/my_changes/hooks/checkout directory and the file in it with the following code:

{include file="buttons/button.tpl" but_text=__("no_shipping_required") but_name="dispatch[checkout.update_shipping]" but_meta="ty-btn__secondary" but_role="submit"}

Replace the SHIPPING_ID with the ID of your free shipping method.

2. Activate the My changes add-on and clear the store cache.

The No shipping required button will appear on the cart page and your free shipping will be selected if you click on it.