how to get total weight of basket products in own custom delivery provider?

Help for integrating the Laravel package
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!
Ahmad
Posts: 72
Joined: 05 Jul 2017, 15:19

how to get total weight of basket products in own custom delivery provider?

Post by Ahmad » 01 Jul 2020, 13:59

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?

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

Re: how to get total weight of basket products in own custom delivery provider?

Post by aimeos » 03 Jul 2020, 11:59

Depends a bit on your used Aimeos version. For 2019.10 use:

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();
   }
 }
For 2020.04 and later use:

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();
 }
Beginning with the upcoming 2020.07 release and later you can also write:

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, Image give us a star

Ahmad
Posts: 72
Joined: 05 Jul 2017, 15:19

Re: how to get total weight of basket products in own custom delivery provider?

Post by Ahmad » 03 Jul 2020, 16:27

i use Aimeos version 2020.04, i copy your code and use it in my provider but i get this error

Code: Select all

Object of class Aimeos\Map could not be converted to string
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:

Code: Select all

$filter->setConditions( $filter->compare( '==', 'product.id', ["8"] ) );
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:

Code: Select all

Call to a member function get() on int
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.

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

Re: how to get total weight of basket products in own custom delivery provider?

Post by aimeos » 03 Jul 2020, 16:57

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, Image give us a star

Ahmad
Posts: 72
Joined: 05 Jul 2017, 15:19

Re: how to get total weight of basket products in own custom delivery provider?

Post by Ahmad » 03 Jul 2020, 19:21

it's not work yet, i get the following error:

Code: Select all

Call to a member function toArray() on array

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

Re: how to get total weight of basket products in own custom delivery provider?

Post by aimeos » 03 Jul 2020, 20:02

$ids[] => $ids
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

Ahmad
Posts: 72
Joined: 05 Jul 2017, 15:19

Re: how to get total weight of basket products in own custom delivery provider?

Post by Ahmad » 04 Jul 2020, 08:21

thanks
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();
}

Post Reply