How to configure and adapt Aimeos based shops as developer
Forum rules: Always add your Aimeos and PHP version as well as your environment (Linux/Mac/Win)
#1855 by Mirco
02 Sep 2015, 18:32
Hi
I configure at the moment the shipping cost and need some help.

I would like to calculate the shipping cost dependent of the amount of items at the basket.
For example:
1 item = 1.45 Euro
2 items = 2.60 Euro
3 items = 3.20 Euro
4 items and more 4.50 Euro
Maybe there is something in the documentation but I'm to dumb to find it :?
Or need I to write a Decorator for this behavior?
#1857 by aimeos
02 Sep 2015, 19:48
Mirco wrote:I would like to calculate the shipping cost dependent of the amount of items at the basket.
For example:
1 item = 1.45 Euro
2 items = 2.60 Euro
3 items = 3.20 Euro
4 items and more 4.50 Euro
Maybe there is something in the documentation but I'm to dumb to find it :?
Or need I to write a Decorator for this behavior?


For linear shipping costs, you could have used "costs per item" for each product but you have a cut after four items so you need to create your own decorator. You can use the "MShop_Service_Provider_Decorator_Costs" as reference.
#1866 by Mirco
04 Sep 2015, 07:28
Thanks!
I have done my one Decorator "Delivery.php". The base was the Costs.php Decorator as suggested.

I changed the function calcPrice( MShop_Order_Item_Base_Interface $basket )
I'm note quite sure if this the right way but it works.

Code: Select all   public function calcPrice( MShop_Order_Item_Base_Interface $basket )
   {
      $config = $this->getServiceItem()->getConfig();

      if( !isset( $config['delivery.amount'] ) ) {
         throw new MShop_Service_Provider_Exception( sprintf( 'Missing configuration "%1$s"', 'delivery.amount' ) );
      }
      
      $count = 0;

      foreach( $basket->getProducts() as $product )
      {
         $count += $product->getQuantity();
      }
      
      $price = $this->_getProvider()->calcPrice( $basket );

      if ($count == 1) {
         $price->setValue( 0.0);
         $price->setCosts( 1.45);
      } else if ($count == 2) {
         $price->setValue( 0.0);
         $price->setCosts(2.40);
      } else if ($count == 3) {
         $price->setValue( 0.0);
         $price->setCosts(5.0);
      } else if ($count > 3) {
         $price->setValue( 0.0);
         $price->setCosts( 6.8);
      }
      return $price;
   }
#1869 by aimeos
04 Sep 2015, 08:41
Mirco wrote:I'm note quite sure if this the right way but it works.


Looks good :-D

Code: Select all$config = $this->getServiceItem()->getConfig();

if( !isset( $config['delivery.amount'] ) ) {
   throw new MShop_Service_Provider_Exception( sprintf( 'Missing configuration "%1$s"', 'delivery.amount' ) );
}

This seems to be unnecessary but you can use the configuration to let the shop owner change the prices himself.

Code: Select all$price->setValue( 0.0);
$price->setCosts( 1.45);


Decorators shouldn't reset the prices but add their costs like this:
Code: Select all$price->setCosts( $price->getCosts() + $amount );

Thus, you can stack several decorators which all calculate their additional costs :-)