How to access to products weight

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!
WGSF
Posts: 22
Joined: 04 Apr 2023, 11:12

How to access to products weight

Post by WGSF » 12 Jun 2023, 12:31

Laravel 10, aimeos 2022, php 8.2

Hello,
I created a Delivery provider in order to override calcPrice method.
It works find, I can access to each product in the basket (access to IDs, prices, descriptions...).
But, How can I access to product weight from calcPrice method ?
Aimeos recommendation ?

WGSF
Posts: 22
Joined: 04 Apr 2023, 11:12

Re: How to access to products weight

Post by WGSF » 12 Jun 2023, 15:23

I found that :
first create prodMap array

Code: Select all

$prodMap = [];
// basket can contain a product several times in different basket items
// product IDs are only those of articles, selections and bundles, not of the variants and bundled products
foreach( $basket->getProducts() as $orderProduct )
        {
            $qty = $orderProduct->getQuantity();
            $code = $orderProduct->getProductCode();
            $prodMap[$code] = ( isset( $prodMap[$code] ) ? $prodMap[$code] + $qty : $qty );

            foreach( $orderProduct->getProducts() as $prodItem ) // calculate bundled products
            {
                $qty = $prodItem->getQuantity();
                $code = $prodItem->getProductCode();
                $prodMap[$code] = ( isset( $prodMap[$code] ) ? $prodMap[$code] + $qty : $qty );
            }
        }

Then access to property

Code: Select all

$weight = 0;
$manager = \Aimeos\MShop::create( $this->context(), 'product' );
$search = $manager->filter( true )->slice( 0, count( $prodMap ) );
$expr = array(
$search->compare( '==', 'product.code', array_keys( $prodMap ) ),
            $search->getConditions(),
        );
$search->setConditions( $search->and( $expr ) );
foreach( $manager->search( $search, ['product/property'] ) as $product )
        {
            foreach( $product->getPropertyItems( 'package-weight' ) as $property ) {
                $weight += ( (float) $property->getValue() ) * $prodMap[$product->getCode()];
                $weight +=$vartemp;
            }
        }

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

Re: How to access to products weight

Post by aimeos » 12 Jun 2023, 16:08

There's another option using the PropertyAdd basket plugin to add properties such as weight the the products in the basket automatically:
https://aimeos.org/docs/latest/manual/p ... ropertyadd
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

kdim95
Advanced
Posts: 208
Joined: 26 Aug 2022, 12:17

Re: How to access to products weight

Post by kdim95 » 12 Jun 2023, 18:38

If the attributes on a product change, they will change for every previous order as well.
Look at the solution that aimeos provides with the PropertyAdd plugin if you want the attributes to be saved in the order.
There are two types of product items, ordered and existing, both having their own properties.
WGSF wrote: 12 Jun 2023, 15:23 I found that :
first create prodMap array

Code: Select all

$prodMap = [];
// basket can contain a product several times in different basket items
// product IDs are only those of articles, selections and bundles, not of the variants and bundled products
foreach( $basket->getProducts() as $orderProduct )
        {
            $qty = $orderProduct->getQuantity();
            $code = $orderProduct->getProductCode();
            $prodMap[$code] = ( isset( $prodMap[$code] ) ? $prodMap[$code] + $qty : $qty );

            foreach( $orderProduct->getProducts() as $prodItem ) // calculate bundled products
            {
                $qty = $prodItem->getQuantity();
                $code = $prodItem->getProductCode();
                $prodMap[$code] = ( isset( $prodMap[$code] ) ? $prodMap[$code] + $qty : $qty );
            }
        }

Then access to property

Code: Select all

$weight = 0;
$manager = \Aimeos\MShop::create( $this->context(), 'product' );
$search = $manager->filter( true )->slice( 0, count( $prodMap ) );
$expr = array(
$search->compare( '==', 'product.code', array_keys( $prodMap ) ),
            $search->getConditions(),
        );
$search->setConditions( $search->and( $expr ) );
foreach( $manager->search( $search, ['product/property'] ) as $product )
        {
            foreach( $product->getPropertyItems( 'package-weight' ) as $property ) {
                $weight += ( (float) $property->getValue() ) * $prodMap[$product->getCode()];
                $weight +=$vartemp;
            }
        }

Post Reply