how to get total weight of basket products in own custom delivery provider?
Forum rules
Always add your Laravel, Aimeos and PHP version as well as your environment (Linux/Mac/Win)
Spam and unrelated posts will be removed immediately!
Always add your Laravel, Aimeos and PHP version as well as your environment (Linux/Mac/Win)
Spam and unrelated posts will be removed immediately!
how to get total weight of basket products in own custom delivery provider?
hello, i need to get total weight (product physical package weight that we set in admin) of basket products in my delivery provider to calculate delivery price based on total weight, how can i get it?
Re: how to get total weight of basket products in own custom delivery provider?
Depends a bit on your used Aimeos version. For 2019.10 use:
For 2020.04 and later use:
Beginning with the upcoming 2020.07 release and later you can also write:
Code: Select all
$ids = [];
$weight = 0;
foreach( $basket->getProducts() as $orderProduct ) {
$ids[] = $orderProduct->getProductId();
}
$manager = \Aimeos\MShop::create( $this->getContext(), 'product' );
$filter = $manager->createSearch()->setSlice( 0, count( $ids ) );
$filter->setConditions( $filter->compare( '==', 'product.id', $ids ) );
$products = $manager->searchItems( $filter, ['product/property'] );
foreach( $basket->getProducts() as $orderProduct ) {
if( isset( $products[$orderProduct->getProductId()] ) ) {
$weight += current( $products[$orderProduct->getProductId()]->getProperties( 'package-weight' ) ) * $orderProduct->getQuantity();
}
}
Code: Select all
$weight = 0;
$ids = $basket->getProducts()->getProductId();
$manager = \Aimeos\MShop::create( $this->getContext(), 'product' );
$filter = $manager->createSearch()->setSlice( 0, count( $ids ) );
$filter->setConditions( $filter->compare( '==', 'product.id', $ids->toArray() ) );
$products = $manager->searchItems( $filter, ['product/property'] );
$weights = $products->getProperties( 'package-weight' );
foreach( $basket->getProducts() as $orderProduct ) {
$weight += current( $weights->get( $orderProduct->getProductId(), [] ) ) * $orderProduct->getQuantity();
}
Code: Select all
$weight = 0;
$ids = $basket->getProducts()->getProductId();
$manager = \Aimeos\MShop::create( $this->getContext(), 'product' );
$filter = $manager->filter()->add( 'product.id', '==', $ids )->slice( 0, count( $ids ) );
$products = $manager->searchItems( $filter, ['product/property'] );
$weights = $products->getProperties( 'package-weight' );
foreach( $basket->getProducts() as $orderProduct ) {
$weight += current( $weights->get( $orderProduct->getProductId(), [] ) ) * $orderProduct->getQuantity();
}
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, give us a star
If you like Aimeos, give us a star
Re: how to get total weight of basket products in own custom delivery provider?
i use Aimeos version 2020.04, i copy your code and use it in my provider but i get this error
in vendor\aimeos\aimeos-core\lib\mwlib\src\MW\Criteria\Expression\Compare\SQL.php:164
i check it and when i change the following line:
i replace the $ids to custom array of product id, after this change i don't get this error but i get another error that say:
in the line
$weight += current( $weight->get( $orderProduct->getProductId(), [] ) ) * $orderProduct->getQuantity();
it seams the the var $ids in the line "$filter->setConditions( $filter->compare( '==', 'product.id', $ids ) );" must be array of strings but it is not, please help me and give me the fixed code for this error and also give me the way to don't get second error after fix the firs error.
Code: Select all
Object of class Aimeos\Map could not be converted to string
i check it and when i change the following line:
Code: Select all
$filter->setConditions( $filter->compare( '==', 'product.id', ["8"] ) );
Code: Select all
Call to a member function get() on int
$weight += current( $weight->get( $orderProduct->getProductId(), [] ) ) * $orderProduct->getQuantity();
it seams the the var $ids in the line "$filter->setConditions( $filter->compare( '==', 'product.id', $ids ) );" must be array of strings but it is not, please help me and give me the fixed code for this error and also give me the way to don't get second error after fix the firs error.
Re: how to get total weight of basket products in own custom delivery provider?
Fixed the code in my 2020.04 example ($ids => $ids->toArray() and $weight => $weights)
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, give us a star
If you like Aimeos, give us a star
Re: how to get total weight of basket products in own custom delivery provider?
it's not work yet, i get the following error:
Code: Select all
Call to a member function toArray() on array
Re: how to get total weight of basket products in own custom delivery provider?
$ids[] => $ids
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, give us a star
If you like Aimeos, give us a star
Re: how to get total weight of basket products in own custom delivery provider?
thanks
the final fixed code is:
the final fixed code is:
Code: Select all
$weight = 0;
$ids = $basket->getProducts()->getProductId();
$manager = \Aimeos\MShop::create( $this->getContext(), 'product' );
$filter = $manager->createSearch()->setSlice( 0, count( $ids ) );
$filter->setConditions( $filter->compare( '==', 'product.id', $ids->toArray() ) );
$products = $manager->searchItems( $filter, ['product/property'] );
$weights = $products->getProperties( 'package-weight' );
foreach( $basket->getProducts() as $orderProduct ) {
$weight += current( $weights->get( $orderProduct->getProductId(), [] )->toArray() ) * $orderProduct->getQuantity();
}