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)
#1934 by boettner
24 Sep 2015, 12:43
Hi,

I´m trying to qery the basket weight property but can´t manage to setup a query taht is selecting the property value directly. As you can see I am currently looping over all properties to get the one in question:

Code: Select all/**
    * Returns the  weight dependend price
    *
    * @param MShop_Order_Item_Base_Interface $basket Basket object
    *
    * @return MShop_Price_Item_Interface Price item containing the price, shipping, rebate
    */
   public function calcPrice(MShop_Order_Item_Base_Interface $basket) {
      $config       = $this->getServiceItem()->getConfig();
      $context      = $this->_getContext();
      $basketWeight = 0;
      $basketItems  = $basket->getProducts();

      foreach ($basketItems as $basketItem) {
         $propertyManager = MShop_Factory::createManager($context, 'Product/Property');

         $search = $propertyManager->createSearch(true);
         //         $expr   = array(
         //            $search->compare('==', 'product.property.type', 'package-weight'),
         //            $search->getConditions(),
         //         );
         //         $search->setConditions($search->combine('&&', $expr));
         $properties = $propertyManager->searchItems($search);

         foreach ($properties as $property) {
            if ($property->getType() == 'package-weight') {
               if ($property->getValue() > 0) {
                  $basketWeight += $property->getValue() * $basketItem->getQuantity();
               }
            }
         }
      }

      foreach ($config as $key => $costConf) {
         if ($basketWeight > (float) $costConf['min'] && $basketWeight <= (float) $costConf['max']) {
            $cost = $costConf['cost'];
         }
      }
      $price = $this->_getProvider()->calcPrice($basket);
      $price->setCosts($cost);

      return $price;
   }


What am I missing here?

Best
Robert.
#1942 by aimeos
24 Sep 2015, 20:32
This will do the job efficiently:
Code: Select all/**
 * Returns the  weight dependend price
 *
 * @param MShop_Order_Item_Base_Interface $basket Basket object
 * @return MShop_Price_Item_Interface Price item containing the price, shipping, rebate
 */
public function calcPrice(MShop_Order_Item_Base_Interface $basket) {
   $config       = $this->getServiceItem()->getConfig();
   $context      = $this->_getContext();
   $basketWeight = 0;
   $prodMap = array();

   foreach ($basket->getProducts() as $basketItem) {
      $prodIds[$basketItem->getProductId()] = $basketItem->getQuantity();
   }

   $propertyManager = MShop_Factory::createManager($context, 'product/property');

   $search = $propertyManager->createSearch(true);
   $expr   = array(
      $search->compare('==', 'product.property.parentid', array_keys($prodMap)),
      $search->compare('==', 'product.property.type', 'package-weight'),
      $search->getConditions(),
   );
   $search->setConditions($search->combine('&&', $expr));

   foreach ($propertyManager->searchItems($search) as $property) {
      if (isset($prodMap[$property->getParentId()])) {
         $basketWeight += $property->getValue() * $prodMap[$property->getParentId()];
      }
   }

   if (isset($config['costs']) {
      foreach ((array) $config['costs'] as $key => $costConf) {
         if ($basketWeight > (float) $costConf['min'] && $basketWeight <= (float) $costConf['max']) {
            $cost = $costConf['cost'];
            break;
         }
      }
   }

   $price = $this->_getProvider()->calcPrice($basket);
   $price->setCosts($price->getCosts() + $cost);

   return $price;
}
#2006 by boettner
26 Oct 2015, 17:22
I changed the decorator for more clearness in backend. Every price is now one single service with the configuration options:

Code: Select allweight.min
weight.max

This way it is possible to combine the default country.billing decorator in one service object and it´s better readable for non tech admins. Before I had a JSON object combining the whole weight scale in a single delivery service.

Code is here:
https://gist.github.com/boettner-it/2d0dbd1c84457ba24a7b
#2008 by aimeos
27 Oct 2015, 12:22
boettner wrote:I changed the decorator for more clearness in backend. Every price is now one single service with the configuration options

Code is here:
https://gist.github.com/boettner-it/2d0dbd1c84457ba24a7b


We would love to add this to the Aimeos Core. Could you create a pull request?
Thanks