Querying package-weight property on basket

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)
Spam and unrelated posts will be removed immediately!
boettner
Advanced
Posts: 136
Joined: 09 Feb 2015, 17:49

Querying package-weight property on basket

Post 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.

User avatar
aimeos
Administrator
Posts: 7836
Joined: 01 Jan 1970, 00:00

Re: Querying package-weight property on basket

Post 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;
}

boettner
Advanced
Posts: 136
Joined: 09 Feb 2015, 17:49

Re: Querying package-weight property on basket

Post 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 all

weight.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/2d0 ... 457ba24a7b

User avatar
aimeos
Administrator
Posts: 7836
Joined: 01 Jan 1970, 00:00

Re: Querying package-weight property on basket

Post 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/2d0 ... 457ba24a7b
We would love to add this to the Aimeos Core. Could you create a pull request?
Thanks
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

boettner
Advanced
Posts: 136
Joined: 09 Feb 2015, 17:49

Re: Querying package-weight property on basket

Post by boettner » 28 Oct 2015, 10:11

aimeos wrote: We would love to add this to the Aimeos Core. Could you create a pull request?
I just created the pull request, thanks for approving . :-)

Post Reply