Basket plugin, add rebate product -> out of stock?

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!
DmS
Posts: 56
Joined: 09 Aug 2021, 08:59

Basket plugin, add rebate product -> out of stock?

Post by DmS » 24 Feb 2022, 16:03

Hi.
I'm building a basket plugin that will give a rebate based on how many of certain products is bought.
Example, buy 10 of prod A and pay for 8, each product costs 30.
In this case the total rebate would be 30*2 = 60

So I've got the logic to match the conditions and calculate the rebate amount. It triggers just fine on changes in the basket so that part works.
Then I understood that the principle would be to add a rebate product to the basket with
I've created a rebate product with unlimited stock, no category, no price which I will give the rebate to.

Then I've found old posts here, that I should do like this to add the rebate-product to the basket:

Code: Select all

//$product is the rebate product created earlier.
$rebateProduct = \Aimeos\MShop::create( $this->getContext(), 'order/base/product' )->create();
$rebateProduct->copyFrom( $product )->getPrice()->setValue( '-'.$discount )->setRebate( $discount );
$order->addProduct( $rebateProduct );
But I'm seeing "products out of stock" on the $order->addProduct( $rebateProduct );

I've dumped the product I created and want to add to the basket and they hold the values I would expect.

Code: Select all

[2022-02-24 15:48:57] local.DEBUG: array (
  'order.base.product.id' => NULL,
  'order.base.product.price' => '-60.00',
  'order.base.product.costs' => '0.00',
  'order.base.product.rebate' => '60.00',
  'order.base.product.taxrate' => '0.00',
  'order.base.product.taxrates' => 
  array (
  ),
  'order.base.product.type' => 'default',
  'order.base.product.stocktype' => '',
  'order.base.product.prodcode' => 'bulk-discount',
  'order.base.product.productid' => '300',
  'order.base.product.supplierid' => '',
  'order.base.product.suppliername' => '',
  'order.base.product.qtyopen' => 1.0,
  'order.base.product.quantity' => 1.0,
  'order.base.product.currencyid' => 'SEK',
  'order.base.product.taxvalue' => '0.0000',
  'order.base.product.taxflag' => true,
  'order.base.product.name' => 'Mängdrabatt',
  'order.base.product.description' => '',
  'order.base.product.mediaurl' => '',
  'order.base.product.timeframe' => '',
  'order.base.product.position' => NULL,
  'order.base.product.notes' => '',
  'order.base.product.statuspayment' => NULL,
  'order.base.product.statusdelivery' => 1,
  'order.base.product.status' => -1,
) 
What am I missing here?

Thanx/Dan
_____________
Laravel 8 with Aimeos 2021.10.7 + Marketplace. Setup via composer. Mac with Valet and MySql 8.0.25

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

Re: Basket plugin, add rebate product -> out of stock?

Post by aimeos » 26 Feb 2022, 07:50

The item in $product you fetch from the database needs to have a stock item associated (can be unlimited too).
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

DmS
Posts: 56
Joined: 09 Aug 2021, 08:59

Re: Basket plugin, add rebate product -> out of stock?

Post by DmS » 28 Feb 2022, 12:25

aimeos wrote: 26 Feb 2022, 07:50 The item in $product you fetch from the database needs to have a stock item associated (can be unlimited too).
As can be seen in the attached screens, the rebate product I created has unlimited stock.
I don't know why I'm not seeing anything stockrelated though when I do $product->toArray()
This is how I retrieve the product, create a new one for the basket, give it values:

Code: Select all

$prodCodes = array($this->getConfigValue('xfory2.rebatecode'));
        $productManager = \Aimeos\MShop::create( $this->getContext(), 'product' );
        $search = $productManager->filter( true )->slice( 0, count( $prodCodes ) );
        $search->setConditions( $search->and( [
            $search->compare( '==', 'product.code', $prodCodes ),
            $search->getConditions(),
        ] ) );
        $result = $productManager->search( $search, ['price', 'attribute' => ['custom']] )->col( null, 'product.code' );
        $product = $productManager->get($result->first());
        
        $rebateProduct = \Aimeos\MShop::create( $this->getContext(), 'order/base/product' )->create();
        $rebateProduct->copyFrom( $product )->getPrice()->setValue( '-'.$discount )->setRebate( $discount );
        $rebateProduct->setStatus('1');
        $order->addProduct( $rebateProduct );
this is what I see for the product I grab from the db and log $product->toArray():

Code: Select all

[2022-02-28 11:23:22] local.DEBUG: array (
  'product.id' => '808',
  'product.url' => 'mangd-rabatt',
  'product.type' => 'default',
  'product.code' => 'mangd-rabatt',
  'product.label' => 'Mängdrabatt',
  'product.status' => 1,
  'product.dataset' => '',
  'product.datestart' => NULL,
  'product.dateend' => NULL,
  'product.config' => 
  array (
  ),
  'product.scale' => 1.0,
  'product.target' => '',
  'product.ctime' => '2021-08-12 09:22:00',
  'product.rating' => '0.00',
  'product.ratings' => 0,
)  
When I create the rebate product that I will place in the basket, I have this when I have set the prices etc and want to put it in the basket:

Code: Select all

[2022-02-28 12:29:45] local.DEBUG: rebateProd:   
[2022-02-28 12:29:45] local.DEBUG: array (
  'order.base.product.id' => NULL,
  'order.base.product.price' => '-60.00',
  'order.base.product.costs' => '0.00',
  'order.base.product.rebate' => '60.00',
  'order.base.product.taxrate' => '0.00',
  'order.base.product.taxrates' => 
  array (
  ),
  'order.base.product.type' => 'default',
  'order.base.product.stocktype' => '',
  'order.base.product.prodcode' => 'mangd-rabatt',
  'order.base.product.productid' => '808',
  'order.base.product.supplierid' => '',
  'order.base.product.suppliername' => '',
  'order.base.product.qtyopen' => 1.0,
  'order.base.product.quantity' => 1.0,
  'order.base.product.name' => 'Mängdrabatt',
  'order.base.product.description' => '',
  'order.base.product.mediaurl' => '',
  'order.base.product.timeframe' => '',
  'order.base.product.position' => NULL,
  'order.base.product.status' => 1,
  'order.base.product.notes' => '',
)  
So how do I ensure that the product has stock? the one I've created in the db has it?
/Dan
Attachments
Screenshot 2022-02-28 at 11.57.46.png
Screenshot 2022-02-28 at 11.57.46.png (218.62 KiB) Viewed 4397 times
Screenshot 2022-02-28 at 11.58.11.png
Screenshot 2022-02-28 at 11.58.11.png (214.3 KiB) Viewed 4397 times
_____________
Laravel 8 with Aimeos 2021.10.7 + Marketplace. Setup via composer. Mac with Valet and MySql 8.0.25

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

Re: Basket plugin, add rebate product -> out of stock?

Post by aimeos » 01 Mar 2022, 14:31

Most likely because you don't set the stock type in the order base product item.
Use the "createRebateProducts()" from the Base coupon provider class so you don't have to care about the details:
https://github.com/aimeos/aimeos-core/b ... te.php#L96
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

DmS
Posts: 56
Joined: 09 Aug 2021, 08:59

Re: Basket plugin, add rebate product -> out of stock?

Post by DmS » 01 Mar 2022, 14:48

aimeos wrote: 01 Mar 2022, 14:31 Use the "createRebateProducts()" from the Base coupon provider class so you don't have to care about the details:
https://github.com/aimeos/aimeos-core/b ... te.php#L96
Thank you, been searching for examples, did not find this one :)
Will try this.
_____________
Laravel 8 with Aimeos 2021.10.7 + Marketplace. Setup via composer. Mac with Valet and MySql 8.0.25

DmS
Posts: 56
Joined: 09 Aug 2021, 08:59

Re: Basket plugin, add rebate product -> out of stock?

Post by DmS » 01 Mar 2022, 16:00

That did indeed work a lot better, now I'm going to dig around on how to remove rebate prod from the basket when needed.
Thank you.
/Dan
_____________
Laravel 8 with Aimeos 2021.10.7 + Marketplace. Setup via composer. Mac with Valet and MySql 8.0.25

Post Reply