Querying package-weight property on basket
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!
Always add your Aimeos and PHP version as well as your environment (Linux/Mac/Win)
Spam and unrelated posts will be removed immediately!
Querying package-weight property on basket
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:
What am I missing here?
Best
Robert.
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;
}
Best
Robert.
Re: Querying package-weight property on basket
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;
}
Re: Querying package-weight property on basket
I changed the decorator for more clearness in backend. Every price is now one single service with the configuration options:
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
Code: Select all
weight.min
weight.max
Code is here:
https://gist.github.com/boettner-it/2d0 ... 457ba24a7b
Re: Querying package-weight property on basket
We would love to add this to the Aimeos Core. Could you create a pull request?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
Thanks
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos,
give us a star
If you like Aimeos,

Re: Querying package-weight property on basket
I just created the pull request, thanks for approving .aimeos wrote: We would love to add this to the Aimeos Core. Could you create a pull request?
