2

I am using the table rate (price vs destination) shipping method. Along with these data I also need individual shipping rates per product.So the shipping rate from the table-rate should only be used if there is no individual shipping rate is specified for the product. Can anyone help me here

Many thanks in advance.

2
  • 1
    And how you will calculate a final price for the cart with both product with individual rate price and without that price? The price must be taken from the individual shipping price of the product if any product have it, is it right? Jan 22 '20 at 14:22
  • @SiarheyUchukhlebau ya exactly
    – skm
    Jan 22 '20 at 14:27
2

You can use slightly modified plugin from this answer How to add shipping price for all individual product

app/code/MageWorx/ShippingPricePerProduct/etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\OfflineShipping\Model\Carrier\Tablerate">
        <plugin name="mageworx_individual_product_shipping_price"
                type="MageWorx\ShippingPricePerProduct\Plugin\ChangeShippingMethodPrice"
                sortOrder="10"
                disabled="false"/>
    </type>
</config>

app/code/MageWorx/ShippingPricePerProduct/Plugin/ChangeShippingMethodPrice.php

<?php
/**
 * Copyright © MageWorx. All rights reserved.
 * See LICENSE.txt for license details.
 */

namespace MageWorx\ShippingPricePerProduct\Plugin;

use Magento\Quote\Model\Quote\Address\RateRequest;
use Magento\Shipping\Model\Carrier\AbstractCarrierInterface;

/**
 * Class ChangeShippingMethodPrice
 */
class ChangeShippingMethodPrice
{
    /**
     * Set individual shipping price per product to each shipping rate
     *
     * @param AbstractCarrierInterface $subject
     * @param $result
     * @param RateRequest $request
     * @return mixed
     */
    public function afterCollectRates(
        AbstractCarrierInterface $subject,
        $result,
        RateRequest $request
    ) {
        if (!$result instanceof \Magento\Shipping\Model\Rate\Result) {
            return $result;
        }

        $priceSurcharge = 0;
        $items = $request->getAllItems();
        foreach ($items as $item) {
            $product = $item->getProduct();
            if (!$product) {
                continue;
            }
            $priceSurcharge += (float)$product->getData('base_individual_shipping_price');
        }

        foreach ($result->getAllRates() as $rate) {
            if (!$rate->getData('individual_price_applied')) {
                if ($priceSurcharge > 0) {
                    // Set new price only in case individual price was found
                    $rate->setPrice($priceSurcharge);
                }

                $rate->setData('individual_price_applied', true);
            }
        }

        return $result;
    }
}

In this variant we set new price for the table rate only in case when the individual price was found for products in current cart.

PS: If this is not your case, please, leave me a comment.

11
  • In my case if individual shipping rate is set for a product then that value should be considered as shipping rate otherwise default value from table rate should be considered.
    – skm
    Jan 23 '20 at 19:18
  • @skm this plugin works in same way: if individual shipping price is not found, we using a regular rate; if individual price exists - we use considered individual price. But in case when two products in cart, with individual price and without it, we are using only individual price of the first product and 0 for the second. Jan 24 '20 at 8:10
  • I installed the plugin but the shipping rate always return 0 in the checkout page.
    – skm
    Jan 24 '20 at 10:34
  • @skm did you set an individual price for your products in the edit product form? Jan 24 '20 at 10:40
  • Ya I already set the shipping price for the product but it is not appearing in the frontend.
    – skm
    Jan 24 '20 at 10:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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