How to access to products weight
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 access to products weight
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 ?
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 ?
Re: How to access to products weight
I found that :
first create prodMap array
Then access to property
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;
}
}
Re: How to access to products weight
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
https://aimeos.org/docs/latest/manual/p ... ropertyadd
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 access to products weight
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.
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 arrayCode: 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 propertyCode: 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; } }