Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retrieving price for shipping method on checkout #24

Open
deanfields opened this issue Nov 15, 2021 · 2 comments
Open

Retrieving price for shipping method on checkout #24

deanfields opened this issue Nov 15, 2021 · 2 comments

Comments

@deanfields
Copy link

I'm using this for retriving and displaying the price for the shipping method at checkout:

method.priceForOrder(cart)|commerceCurrency(cart.paymentCurrency) - but the price being returned isn't the price for the particular currency set. I can't see a way to pull this out?

@williamhibberd
Copy link

I am also having this issue. Is there any update on displaying the correct shipping method price for the particular currency?

@matt-adigital
Copy link

We also came across this issue ourselves on the craft 4 branch, plugin version 3.0.0-beta.4

We've put a fix in place by adding a new twig extension into the core plugin files. For anyone else who is interested, you can make the following change to twigextensions/CurrencyPricesTwigExtension.php

Alter getFilters function by adding the following into the array returned:

new TwigFilter('currencyShippingPrice', [$this, 'currencyShippingPrice']),

Add an entirely new function lower down:

public function currencyShippingPrice($cart, $shippingId, $currency, $format = true, $stripZeros = false): null|string
	{
		$this->_validatePaymentCurrency($currency);

		$shipping = CurrencyPrices::$plugin->shipping->getPricesByShippingRuleIdAndCurrency($shippingId, $currency);
		if (!$shipping) {
			return null;
		}

		$total = $shipping['baseRate'];

		$percentageRate = $shipping['percentageRate'];
		$perItemRate = $shipping['perItemRate'];
		$weightRate = $shipping['weightRate'];

		/** @var $cart Order */
		foreach ($cart->getLineItems() as $cartItem) {
			$percentageAmount = $cartItem->getSubtotal() * $percentageRate;
			$perItemAmount = $cartItem->qty * $perItemRate;
			$weightAmount = ($cartItem->weight * $cartItem->qty) * $weightRate;
			$total += Currency::round($percentageAmount + $perItemAmount + $weightAmount);
		}

		if ($shipping['minRate'] != 0 && $total < Currency::round($shipping['minRate'])) {
			$total = Currency::round($shipping['minRate']);
		}

		if ($shipping['maxRate'] != 0 && $total > Currency::round($shipping['maxRate'])) {
			$total = Currency::round($shipping['maxRate']);
		}

		if ($format) {
			$total = Craft::$app->getFormatter()->asCurrency($total, $currency, [], [], $stripZeros);
		}

		return $total;
	}

Once the above changes are done, all you need to do in your templates is the following:

{{ cart|currencyShippingPrice(shippingRule.id, cart.paymentCurrency) }}

This allows a preview of the shipping price for each method without needing to be selected and fully calculated.

It would be nice to see this plugin leave beta and potentially have the above fix included. Thanks all.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants