Skip to content

Add different shipping price on the basis of the product attributes and Country

I have this qty_more attribute showing on the Grouped Product PDP.

I have a table of the shipping costs of the different countries that will only apply if the product having attribute is add to cart and then place the order.

As you see in the grouped.phtml it is displaying with the qty box button, it is same like that but bulk of quantities

getData(‘qty_box’)) : ?>

                                        <input type="hidden"
                                               name="qty_incdec_<?= $_item->getId() ?>"
                                               id="qty_incdec<?= $_item->getId() ?>"
                                               value="<?= $_item->getData('qty_box') ?>"
                                        />
                                    <?php endif; ?>
                                    <?php if ($_item->getData('qty_more')) : ?>
                                        <input type="hidden"
                                               name="qty_more_incdec_<?= $_item->getId() ?>"
                                               id="qty_more_incdec<?= $_item->getId() ?>"
                                               value="<?= $_item->getData('qty_more') ?>"
                                        />

And this is JS that works on the quantity

// app/code/YourCompany/YourModule/view/frontend/web/js/quantity-handler.js

define([‘jquery’], function($) {
‘use strict’;

return function() {
    $(document).ready(function() {
        $(".qty-down-fixed-onclick").click(function() {
            var qty_incdec = $(this).closest('tr').find('input[name^="qty_incdec"]').val();
            var number_click = parseInt(qty_incdec);
            var qty_more_incdec = $(this).closest('tr').find('input[name^="qty_more_incdec"]').val();
            var number_click_pallet = parseInt(qty_more_incdec);

            var val_input = $(this).siblings(':input.qty[type="number"]').val();
            let optionValue = $(this).closest('tr').find('.quantity-dropdown').val();
            val_input = parseInt(val_input);

            console.log('qty_index: ' + qty_incdec);
            console.log('qty_more_index: ' + qty_more_incdec);
            console.log('optionValue: ' + optionValue);

            if (optionValue === 'single') {
                number_click = 1;
            } else if (optionValue === 'pallet') {
                number_click = number_click_pallet;
            }
            if(val_input < 0 ){
                val_input = number_click;
            }
            else{
                val_input = val_input - number_click;
            }
            if(val_input < 0) val_input = 0;
            $(this).siblings(':input.qty[type="number"]').val(val_input);
            return false;
        });

        $(".qty-up-fixed-onclick").click(function() {
            var qty_incdec = $(this).closest('tr').find('input[name^="qty_incdec"]').val();
            var number_click = parseInt(qty_incdec);
            var qty_more_incdec = $(this).closest('tr').find('input[name^="qty_more_incdec"]').val();
            var number_click_pallet = parseInt(qty_more_incdec);

            var val_input = $(this).siblings(':input.qty[type="number"]').val();
            val_input = parseInt(val_input);
            let optionValue = $(this).closest('tr').find('.quantity-dropdown').val();

            console.log('qty_index: ' + qty_incdec);
            console.log('qty_more_index: ' + qty_more_incdec);
            console.log('optionValue: ' + optionValue);

            if (optionValue === 'single') {
                number_click = 1;
                val_input = val_input + number_click;
            } else if (optionValue === 'box') {
                val_input = val_input + number_click;
            } else if (optionValue === 'pallet') {
                val_input = val_input + number_click_pallet;
            }

            $(this).siblings(':input.qty[type="number"]').val(val_input);
            return false;
        });
    });
};

});

I want to apply that shipping price on the basis of the address country if matching the attribute.

Actually it is like a product quantity attribute that is coming from the admin, WHEN select this it will add product add to cart with that more quantity, I want to add shipping price based on this attribute having the product based on the address country.