Create manual order

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!
jramirezgranada
Posts: 40
Joined: 12 Jul 2018, 19:57

Create manual order

Post by jramirezgranada » 28 Jan 2019, 20:52

Hi there;

Given a product code (SKU) how can I create a manual order with that ?

Thanks
--
Jorge A Ramirez
System Engineer
PHP Developer

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

Re: Create manual order

Post by aimeos » 29 Jan 2019, 06:54

Use something like this:

Code: Select all

$prodcntl = \Aimeos\Controller\Frontend\Product\Factory::createController( $context );
$prodId = $prodcntl->findItem( '<sku>' )->getId();
$basketcntl = \Aimeos\Controller\Frontend\Basket\Factory::createController( $context );
$basketcntl->addProduct( $prodId );
You also have to add a billing address and a delivery and payment option to the basket. Use the "customer" and "service" controller for that. Then, you can use "$basketcntl->store()" to create an order. Using the "order" controller, you can create an order item with a payment status of "authorized" or "received" so your order gets processed normally in the further steps (send e-mails, push to ERP system, etc.)

For more information about the controllers, please have a look at the API docs:
https://aimeos.org/api/latest/namespace ... ntend.html

In 2019.x versions, the interfaces of the controllers changes a bit to simplify things.
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

jramirezgranada
Posts: 40
Joined: 12 Jul 2018, 19:57

Re: Create manual order

Post by jramirezgranada » 29 Jan 2019, 21:39

findItem doesn't exists, I used the manager instead of factory
$prodcntl = \Aimeos\Controller\Frontend\Product\Factory::createController( $context );
$prodId = $prodcntl->findItem( '<sku>' )->getId();
When I sotre the basket with $basketcntl->store(), it creates a record in order_base, but it doesn't create records in order and order_base_products
$basketcntl = \Aimeos\Controller\Frontend\Basket\Factory::createController( $context );
$basketcntl->addProduct( $prodId );
What is my case:
1. I extended the coupon manager to add an sku,
2. That code will be redeemed.
3. As that code has associated a sku I need to create a complete order, set price in 0, and put it as authorized, and generate the billing.

Thks
--
Jorge A Ramirez
System Engineer
PHP Developer

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

Re: Create manual order

Post by aimeos » 31 Jan 2019, 13:06

If you are implementing something in the mshoplib, you must not use the controllers but the managers directly instead.

Then it will be something like this:

Code: Select all

$product = \Aimeos\MShop\Factory::createManager( $context, 'product' )->findItem( '<sku>', ['text', 'media', 'price'] );
$orderProduct = \Aimeos\MShop\Factory::createManager( $context, 'order/base/product' )->createItem()->copyFrom( $product );
$orderProduct->getPrice()->setValue( '0.00' );

$baseManager = \Aimeos\MShop\Factory::createManager( $context, 'order/base' );
$basket = $baseManager->createItem()->addProduct( $orderProduct );
// add address (if authenticated customer), delivery (e.g. 'Manual') and payment option (e.g. 'Postpay')
$basket = $baseManager->store( $basket );

$orderManager = \Aimeos\MShop\Factory::createManager( $context, 'order' );
$orderItem = $orderManager->createItem()->setBaseId( $basket->getId() )->setPaymentStatus( 5 )->setType( 'coupon' );
$orderManager->saveItem( $orderItem );
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

jramirezgranada
Posts: 40
Joined: 12 Jul 2018, 19:57

Re: Create manual order

Post by jramirezgranada » 04 Feb 2019, 19:45

Thanks, I have a good progress with that.

When I wnat to add a product to order/base with $baseManager->createItem()->addProduct( $orderProduct ); the product isn't added to order base neither order base product table, always the product should be a variant of a parent product,

I've tried pass the parent product id and the variant id, also pass the product id, but in those cases i can't get it in the order base information.

Thanks
--
Jorge A Ramirez
System Engineer
PHP Developer

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

Re: Create manual order

Post by aimeos » 06 Feb 2019, 10:41

jramirezgranada wrote: When I wnat to add a product to order/base with $baseManager->createItem()->addProduct( $orderProduct ); the product isn't added to order base neither order base product table, always the product should be a variant of a parent product,
Only after storing the order/base object (basket), the content is persisted in the database:

Code: Select all

$baseManager->store( $basket );
jramirezgranada wrote:I've tried pass the parent product id and the variant id, also pass the product id, but in those cases i can't get it in the order base information.
For selection products, you might find it helpful to investigate how the basket frontend controller handles them:
https://github.com/aimeos/ai-controller ... Select.php
Most important: The product ID in the order/base/product item is the ID of the selection product but the code is the SKU of the variant article.
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

milos_shomsy
Posts: 7
Joined: 03 Aug 2020, 13:30

Re: Create manual order

Post by milos_shomsy » 14 Aug 2020, 15:27

Hello,

One question, please.

How can I add multiple products in single order? At the end, I need sum of price and quantites for an order like:

Code: Select all

$order = [
  0 => [
    "product_id" => "17"
    "quantity" => "3"
  ]
  1 =>  [
    "product_id" => "27"
    "quantity" => "4"
  ]
]
And I want to use managers like you described before:

Code: Select all

 $product = \Aimeos\MShop\Factory::createManager( $context, 'product' )->findItem( '<sku>', ['text', 'media', 'price'] );
$orderProduct = \Aimeos\MShop\Factory::createManager( $context, 'order/base/product' )->createItem()->copyFrom( $product );
$orderProduct->getPrice()->setValue( '0.00' );

$baseManager = \Aimeos\MShop\Factory::createManager( $context, 'order/base' );
$basket = $baseManager->createItem()->addProduct( $orderProduct );
// add address (if authenticated customer), delivery (e.g. 'Manual') and payment option (e.g. 'Postpay')
$basket = $baseManager->store( $basket );

$orderManager = \Aimeos\MShop\Factory::createManager( $context, 'order' );
$orderItem = $orderManager->createItem()->setBaseId( $basket->getId() )->setPaymentStatus( 5 )->setType( 'coupon' );
$orderManager->saveItem( $orderItem );

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

Re: Create manual order

Post by aimeos » 17 Aug 2020, 06:43

You can use:

Code: Select all

$prodcntl = \Aimeos\Controller\Frontend::create( $this->getContext(), 'product' );
$basketcntl = \Aimeos\Controller\Frontend::create( $this->getContext(), 'basket' );

foreach( ['17' => 3, '27' => 4] as $prodid => $qty )
{
	$product = $prodcntl->get( $prodid, ['price', 'text', 'media'] );
	$basketcntl->addProduct( $product, $qty );
}

$price = $basketcntl->get()->getPrice()->getValue();
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

milos_shomsy
Posts: 7
Joined: 03 Aug 2020, 13:30

Re: Create manual order

Post by milos_shomsy » 17 Aug 2020, 09:37

Hello,

Thanks for help.

I have one more queston about this.

We want to have simple shop like: products and orders.
So we don't have catalogs, categories, etc.

And now I have bug usning frontend controllers you sent me in code snippet:
Aimeos\Controller\Frontend\Basket\Exception
Adding product with ID "17" is not allowed
Is there a way to skip those and add multiple products to one order ID?

Thanks in advance,
Milos Stankovic

milos_shomsy
Posts: 7
Joined: 03 Aug 2020, 13:30

Re: Create manual order

Post by milos_shomsy » 19 Aug 2020, 08:51

Hello,

Just to let you know, I ended up with something like this:

Code: Select all

$context = app()->make('aimeos.context')->get();
$productManager = \Aimeos\MShop::create($context, 'product');
// ORDER BASE PRODUCT
$orderProduct = \Aimeos\MShop::create($context, 'order/base/product');
$orderedProducts = [];
foreach (['17' => 3, '27' => 4] as $prodid => $qty) {
    $product = $productManager->getItem($prodid, ['text', 'media', 'price']);
    $price = $product->getRefItems('price')->first();
    $orderedProducts[] = $orderProduct
        ->createItem()
        ->setQuantity($qty)
        ->setPrice($price)
        ->copyFrom($product);
}
// ORDER BASE
$baseManager = \Aimeos\MShop::create($context, 'order/base');
$baseItem = $baseManager->createItem();
$basketProducts = $baseItem->setProducts($orderedProducts);
$basket = $baseManager->store($basketProducts);
// ORDER
$orderManager = \Aimeos\MShop::create($context, 'order');
$orderItem = $orderManager->createItem();
$orderItem->setBaseId($basket);
$orderManager->saveItem($orderItem);

Post Reply