<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Dynamic Shipping Pricing by Weight and Country]]></title><description><![CDATA[<p dir="auto">I'm working on an online pharmacy project for Pills4Cure, where we ship healthcare products to customers in the USA, UK, Netherlands, and Australia. I need to calculate a custom payment or shipping fee based on two factors: the total weight of the items in the cart and the customer's destination country.</p>
<p dir="auto">I already have access to the product weight through a custom attribute, but I'm not sure of the best way to retrieve the destination country selected during checkout or from the shopping cart. I'd also like to know the recommended approach for combining the cart weight with the shipping country to calculate a dynamic fee.</p>
<p dir="auto">Has anyone implemented a similar solution for international shipping? Any suggestions, best practices, or code examples would be greatly appreciated.</p>
]]></description><link>https://forums.bagisto.com/topic/4280/dynamic-shipping-pricing-by-weight-and-country</link><generator>RSS for Node</generator><lastBuildDate>Fri, 14 Aug 2026 20:42:23 GMT</lastBuildDate><atom:link href="https://forums.bagisto.com/topic/4280.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 06 Aug 2026 06:25:00 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Dynamic Shipping Pricing by Weight and Country on Fri, 14 Aug 2026 10:02:53 GMT]]></title><description><![CDATA[<p dir="auto">Hello <a class="plugin-mentions-user plugin-mentions-a" href="https://forums.bagisto.com/uid/29556">@barnaddy06</a></p>
<p dir="auto">The best approach is to implement this as a custom shipping carrier.</p>
<p dir="auto">You should not read the destination country directly from the request or session. Get the current cart and retrieve the country from the cart's shipping address:</p>
<pre><code class="language-php">use Webkul\Checkout\Facades\Cart;

$cart = Cart::getCart();

$country = $cart-&gt;shipping_address?-&gt;country
    ?? $cart-&gt;billing_address?-&gt;country;
</code></pre>
<p dir="auto">The country value will be the ISO country code, for example:</p>
<pre><code class="language-text">US
GB
NL
AU
</code></pre>
<p dir="auto">For weight, if you are using Bagisto's native <code>weight</code> attribute, you should use the cart item <code>total_weight</code> instead of recalculating the product weight manually:</p>
<pre><code class="language-php">$totalWeight = 0;

foreach ($cart-&gt;items as $item) {
    if (! $item-&gt;getTypeInstance()-&gt;isStockable()) {
        continue;
    }

    $totalWeight += (float) $item-&gt;total_weight;
}
</code></pre>
<p dir="auto">Using <code>$cart-&gt;items</code> is important because <code>all_items</code> may include child items and can result in double-counting for configurable or bundle products.</p>
<p dir="auto">Then create a custom carrier extending <code>AbstractShipping</code> and perform the country + weight calculation inside the <code>calculate()</code> method:</p>
<pre><code class="language-php">class InternationalRate extends AbstractShipping
{
    protected $code = 'international';

    public function calculate()
    {
        if (! $this-&gt;isAvailable()) {
            return false;
        }

        $cart = Cart::getCart();

        if (! $cart || ! $cart-&gt;haveStockableItems()) {
            return false;
        }

        $country = $cart-&gt;shipping_address?-&gt;country
            ?? $cart-&gt;billing_address?-&gt;country;

        $weight = 0;

        foreach ($cart-&gt;items as $item) {
            if (! $item-&gt;getTypeInstance()-&gt;isStockable()) {
                continue;
            }

            $weight += (float) $item-&gt;total_weight;
        }

        $rates = [
            'US' =&gt; ['handling' =&gt; 8, 'per_kg' =&gt; 2.5],
            'GB' =&gt; ['handling' =&gt; 6, 'per_kg' =&gt; 2],
            'NL' =&gt; ['handling' =&gt; 6.5, 'per_kg' =&gt; 2.2],
            'AU' =&gt; ['handling' =&gt; 12, 'per_kg' =&gt; 4],
        ];

        if (! isset($rates[$country])) {
            return false;
        }

        $basePrice = $rates[$country]['handling']
            + ($weight * $rates[$country]['per_kg']);

        return $this-&gt;getRate($basePrice);
    }
}
</code></pre>
<p dir="auto">The main point is:</p>
<p dir="auto"><strong>Cart → Shipping Address Country + Cart Item Weight → Custom Carrier → Calculated Shipping Rate</strong></p>
<p dir="auto">Also, make sure to set both <code>base_price</code> and <code>price</code> on the <code>CartShippingRate</code>. The base price should be calculated in the store's base currency, while <code>price</code> should be converted using:</p>
<pre><code class="language-php">$rate-&gt;base_price = $basePrice;
$rate-&gt;price = core()-&gt;convertPrice($basePrice);
</code></pre>
<p dir="auto">If the shipping rules become more complex, such as multiple weight slabs per country, I would recommend storing the rules in a database table rather than hardcoding them.</p>
<p dir="auto">One important note: if your product weight is stored in a completely custom attribute instead of Bagisto's native <code>weight</code> attribute, <code>total_weight</code> may not be populated. In that case, either map your value to Bagisto's native weight field or calculate it from your custom attribute manually.</p>
<p dir="auto">For this requirement, a custom shipping carrier is the cleanest and most maintainable Bagisto solution.</p>
<p dir="auto">Best regards,<br />
Shivendra Gupta<br />
Team Bagisto</p>
]]></description><link>https://forums.bagisto.com/post/14976</link><guid isPermaLink="true">https://forums.bagisto.com/post/14976</guid><dc:creator><![CDATA[shivendra-webkul]]></dc:creator><pubDate>Fri, 14 Aug 2026 10:02:53 GMT</pubDate></item></channel></rss>