I need to save new fields on new Delivery Service

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!
monkyja
Posts: 3
Joined: 06 Jul 2020, 10:11

I need to save new fields on new Delivery Service

Post by monkyja » 06 Jul 2020, 10:32

Hello,
I need to add new fields to a service (deliveries).

I have managed to show two fields with the $ feConfig configuration and access the Validation function. But I have stayed there.

Where could I find an example that saves those fields in the database, and then when sending the emails I will attach those new fields?

What I need to do is create a new service that sends the order as a gift. Save the mail and a message and after payment. Send two emails, one of normal order to the purchasing user and another to the gifted user.

Thank you very much for the help. And excuse me for needing to use the translator

My actual code is:

Code: Select all

<?php
namespace Aimeos\MShop\Service\Provider\Delivery;
 
class MailRegalo
	extends \Aimeos\MShop\Service\Provider\Delivery\Base
	implements \Aimeos\MShop\Service\Provider\Delivery\Iface
{
	
	private $feConfig = array(
		'mailregalo.destinatario' => array(
			'code' => 'mailregalo.destinatario',
			'internalcode'=> 'mailregalo.destinatario',
			'label'=> 'Mail Regalo',
			'type'=> 'string',
			'internaltype'=> 'string',
			'default'=> '',
			'required'=> true,
		),
        'mailregalo.remitente' => array(
            'code' => 'mailregalo.remitente',
            'internalcode'=> 'mailregalo.remitente',
            'label'=> 'PostPac Priority',
            'type'=> 'string',
            'internaltype'=> 'string',
            'default'=> '',
            'required'=> true,
        ),
    );
    
    public function getConfigFE( \Aimeos\MShop\Order\Item\Base\Iface $basket )
    {
        $list = array();
    
        foreach( $this->feConfig as $key => $config ) {
            $list[$key] = new \Aimeos\MW\Criteria\Attribute\Standard( $config );
        }
        return $list;
    }

    public function setConfigFE( \Aimeos\MShop\Order\Item\Base\Service\Iface $orderServiceItem, array $attributes )
    {
        $this->setAttributes( $orderServiceItem, $attributes, 'delivery/mailregalo' );
    }

    public function checkConfigFE( array $attributes )
	{
		return $this->checkConfig( $this->feConfig, $attributes );
    }

    


	public function process( \Aimeos\MShop\Order\Item\Iface $order, array $params = [] )
    {

		dd($order);

        $parts = \Aimeos\MShop\Order\Manager\Base\Base::PARTS_ALL;
        $orderBaseItem = $this->getOrderBase( $order->getBaseId(), $parts );

        $type = \Aimeos\MShop\Order\Item\Base\Service\Base::TYPE_DELIVERY;
        $code = $this->getServiceItem()->getCode(); // code of the service payment 

        foreach( $orderBaseItem->getService( $type, $code )->getAttributes() as $attr ) {
            // $attr->getCode() . ': ' . $attr->getValue();
        }




		$attr = array();
		foreach( $this->payconfig as $key => $config ) {
			$attr[$key] = new \Aimeos\MW\Criteria\Attribute\Standard( $config );
		}
	
		$url = $this->getConfigValue( array( 'payment.url-self' ) );
		return new \Aimeos\MShop\Common\Item\Helper\Form\Standard( $url, 'POST', $attr, false );
    }


}

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

Re: I need to save new fields on new Delivery Service

Post by aimeos » 07 Jul 2020, 17:27

Remove the process() method, it's wrong for what you want.
The setConfigFE() method already adds the data entered by the customer as order service attributes and they will be stored in the mshop_order_base_service_attr table when the customer clicks on "Buy now".

To send your additional e-mail, you need to create a new job controller that scans for those orders (best to use the service code to select them within a limited time frame to avoid performance problems), sends the e-mail and adds a order status entry to mark the order as done.

Job controller documentation:
https://aimeos.org/docs/Developers/Cont ... controller

Voucher controller as example:
https://github.com/aimeos/ai-client-htm ... andard.php

Get the orders:
https://github.com/aimeos/ai-client-htm ... #L105-L130

Add the order status entry:
https://github.com/aimeos/ai-client-htm ... #L160-L168
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

monkyja
Posts: 3
Joined: 06 Jul 2020, 10:11

Re: I need to save new fields on new Delivery Service

Post by monkyja » 08 Jul 2020, 06:56

Thanks for the reply.

I have tried removing the "process" function but, I have an error "Class Aimeos\MShop\Service\Provider\Delivery\MailRegalo contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (Aimeos\MShop\Service\Provider\Delivery\Iface::process) " and it won't let me pass the payment.

I am doing the class as Provider.
Do I have to do it as a Decorator?

monkyja
Posts: 3
Joined: 06 Jul 2020, 10:11

Re: I need to save new fields on new Delivery Service

Post by monkyja » 08 Jul 2020, 07:28

If I set the porocess function to empty, as it says in the documentation

https://aimeos.org/docs/Developers/Libr ... y_provider

I get an error like this "Argument 1 passed to Aimeos\MShop\Order\Item\Base\Base::addService() must implement interface Aimeos\MShop\Order\Item\Base\Service\Iface, null given, called in C:\laragon\www\CentroNature-Web\ext\ai-controller-frontend\controller\frontend\src\Controller\Frontend\Basket\Standard.php on line 430"

Thanks for the help and patience

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

Re: I need to save new fields on new Delivery Service

Post by aimeos » 10 Jul 2020, 17:32

You can do it as a delivery service provider by implementing the process() method but then you need to comply to the interface and implement them as

Code: Select all

public function processBatch( array $orders )
{
    foreach( $orders as $order ) {
        $this->getObject()->process( $order );
    }
}

public function process( \Aimeos\MShop\Order\Item\Iface $order )
{
    $status = \Aimeos\MShop\Order\Item\Base::STAT_PROGRESS;
    $order->setDeliveryStatus( $status );
    $this->saveOrder( $order );
}
The better way is to implment your additional fields as service decorator because then, you can combine it with any existing service provider.
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

Post Reply