Merx

Shipping Costs and Discounts

With Merx 1.1 there is a new hook called ww.merx.cart which gives you the capability to manipulate the cart. You can add new products (e.g. Shipping). You could even change the price of products in the cart with $cart->updateItem().

Find the source code of a full working example on Github:
Source Code

Create Products

Shipping Costs or Discounts are handled as own products. It’s up to you how you will output and style these things on the frontend.

content/shipping/product.txt

Title: Shippping

----

Price: 7.99

----

Tax: 7

content/discount/product.txt

Title: Discount

----

Price: -10

----

Tax: 0

You probably noted that the price of Discount is negative. This is possible since Merx 1.1. Merx will still check if the whole cart has a negative sum, if so an exception will be thrown.

Create cart hook

Use the ww.merx.cart hook to manipulate the user’s cart.

In this example Shipping is added if the sum of the cart is lower than 50.
When you have five or more Knitted Socks you will get a discount of 10.

site/config.php

return [
  'hooks' => [
    'ww.merx.cart' => function ($cart) {
      if ($cart->count() > 0) {
        $cart->remove('shipping');
        $cart->remove('discount');
        if ($cart->getSum() < 50) {
          $cart->add([
            'id' => 'shipping',
          ]);
        }
        if ($cart->get('knitted-socks')['quantity'] >= 5) {
          $cart->add([
            'id' => 'discount',
          ]);
        }
      }
    }
  ],
];