Page 1 of 2

issues in Implementing Delivery Provider

Posted: 02 Apr 2021, 09:46
by khizar
Hi aimeos,
i have integrated aimeos with laravel.
versions:
"aimeos/aimeos-laravel": "~2020.10",
"laravel/framework": "^8.12",

i am implementing delivery service provider.I have created my provider and implemented api.That delivery service api needs data about order which i have provided but now i am stuck at a point at which i need to retrieve the lengtth,width,height of products and i don't know how to do that .i am posting my code below please guide me how can i do that

Code: Select all

	<?php

namespace Aimeos\MShop\Service\Provider\Delivery;

use Illuminate\Support\Facades\Http;

class Myprovider
extends \Aimeos\MShop\Service\Provider\Delivery\Base
implements \Aimeos\MShop\Service\Provider\Delivery\Iface
{
    /**
     * Sends the order details to the ERP system for further processing.
     *
     * @param \Aimeos\MShop\Order\Item\Iface $order Order invoice object to process
     */
    public function process(\Aimeos\MShop\Order\Item\Iface $order): \Aimeos\MShop\Order\Item\Iface
    {

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

        $payment = [];
        $delivery = [];
        // addresses can also be picked through following method
        if ($addr = current($basket->getAddress('payment'))) {
            $payment = [
                "name" => "{$addr->getFirstname()} {$addr->getLastname()}",
                "phone" => $addr->getTelephone(),
                // "alternate_phone" => "9876543210",
                "address_line_1" => $addr->getAddress1(),
                "address_line_2" => $addr->getAddress2(),
                "city" => $addr->getCity()
                // "state" => "DUBAI"
            ];
            // // ...
            // // Alternative:
            // $map = $addr->toArray();
        }


        if ($addr = current($basket->getAddress('delivery'))) {
            $delivery = [
                "name" => "{$addr->getFirstname()} {$addr->getLastname()}",
                "phone" => $addr->getTelephone(),
                // "alternate_phone" => "9876543210",
                "address_line_1" => $addr->getAddress1(),
                "address_line_2" => $addr->getAddress2(),
                "city" => $addr->getCity()
                // "state" => "DUBAI"
                // "latitude" => "24.6948",
                // "longitude" => "46.8396"
            ];
            // // ...
            // // Alternative:
            // $map = $addr->toArray();
        }
         
        if(sizeOf($delivery) == 0){
            $delivery = $payment;
        }
        $products = $basket->getProducts();

        $piecesDetail = array();
        
        foreach ($products as $pd) {
            
            $p = current($p);
           // here i retrieved name of product successfully but i don't know how can i retrieve length width and height
            $spd = [
                "description" => $p->getName(),
                "declared_value" => $p->getPrice(),
                "weight" => "",
                "height" => "",
                "length" => "",
                "width" => ""
            ];
            array_push($piecesDetail, $spd);
        }

       




        $data = [
            "consignments" => [
                [
                    "customer_code" => "MODHAH 01",
                    "reference_number" => "",
                    "service_type_id" => "1",
                    "load_type" => "NON-DOCUMENT",
                    "description" => "Items",
                    // "cod_favor_of" => "",
                    // "dimension_unit" => "",
                    // "length" => "",
                    // "width" => "",
                    // "height" => "",
                    // "weight" => "",
                    // "weight_unit" => "",
                    "declared_value" => "1300",
                    // "declared_price" => "",
                    // "cod_amount" => "",
                    // "cod_collection_mode" => "",
                    // "prepaid_amount" => "",
                    // "num_pieces" => "",
                    // "customer_reference_number" => "",
                    // "s_risk_surcharge_applicable" => "true",
                    "origin_details" => $payment,
                    "destination_details" => $delivery,
                    "pieces_detail" => 


                ]
            ]
        ];





        // $apiurl = 'https://demodashboardapi.shipsy.in/api/customer/integration/consignment/softdata';
        // $response =  Http::withHeaders([
        //     "api-key" => "38d270602719af5c78c112be2c7ee4",
        //     "Content-Type" => "application/json"
        // ])->post($apiurl,$data);

        // info($response);
        // $t = json_decode($response);
        // info($t->error->message);
        return $order;
    }
}



Re: issues in Implementing Delivery Provider

Posted: 06 Apr 2021, 08:03
by aimeos
The product properties are not added to the ordered product (or at least not by default). To get the properties, you have to load the original product, e.g.:

Code: Select all

$prodIds = [];
$manager = \Aimeos\MShop::create( $this->getContext(), 'product' );

foreach( $basket->getProducts() as $orderProduct ) {
	$prodIds[] = $orderProduct->getProductId();
}

$filter = $manager->filter()->add( ['product.id' => $prodIds] )->slice( 0, 10000 );
$products = $manager->search( $filter, ['product/property'] );

foreach( $basket->getProducts() as $orderProduct )
{
	if( $product = $products[$orderProduct->getProductId()] ?? null ) )
	{
		$piecesDetail[] = [
			'width' => $product->getProperties( 'package-width' )->first(),
			'height' => $product->getProperties( 'package-height' )->first(),
			'length' => $product->getProperties( 'package-length' )->first(),
			'weight' => $product->getProperties( 'package-weight' )->first(),
		];
	}
}

Re: issues in Implementing Delivery Provider

Posted: 07 Apr 2021, 09:46
by khizar
aimeos wrote: 06 Apr 2021, 08:03 The product properties are not added to the ordered product (or at least not by default). To get the properties, you have to load the original product, e.g.:

Code: Select all

$prodIds = [];
$manager = \Aimeos\MShop::create( $this->getContext(), 'product' );

foreach( $basket->getProducts() as $orderProduct ) {
	$prodIds[] = $orderProduct->getProductId();
}

$filter = $manager->filter()->add( ['product.id' => $prodIds] )->slice( 0, 10000 );
$products = $manager->search( $filter, ['product/property'] );

foreach( $basket->getProducts() as $orderProduct )
{
	if( $product = $products[$orderProduct->getProductId()] ?? null ) )
	{
		$piecesDetail[] = [
			'width' => $product->getProperties( 'package-width' )->first(),
			'height' => $product->getProperties( 'package-height' )->first(),
			'length' => $product->getProperties( 'package-length' )->first(),
			'weight' => $product->getProperties( 'package-weight' )->first(),
		];
	}
}
Thanks for reply.Your solution worked perfectly.Now there is another problem.The code which i have posted above you can see that there is a key in my api request(origin_details) in which i have inserted an array($payment).Now that array($payment) contains the payment address which the user inputs as billing address in checkout process but i want to insert the origin address in origin_details which will be diferent for every site.Now i don't know where to add that address and then how can i access that address in my provider which i have posted above.please guide me on that.

Re: issues in Implementing Delivery Provider

Posted: 07 Apr 2021, 10:19
by aimeos
There's no "global address" if you mean that. You could add the address details as configuration to your service provider and use

Code: Select all

$this->getConfigValue( '...' )
to get the data you've entered in the service panel of the admin backend.

See here for more details:
https://aimeos.org/docs/latest/provider ... figuration

Re: issues in Implementing Delivery Provider

Posted: 07 Apr 2021, 10:22
by khizar
aimeos wrote: 07 Apr 2021, 10:19 There's no "global address" if you mean that. You could add the address details as configuration to your service provider and use

Code: Select all

$this->getConfigValue( '...' )
to get the data you've entered in the service panel of the admin backend.

See here for more details:
https://aimeos.org/docs/latest/provider ... figuration
i know that but then it will be same for every site. i want different address for every site so for that do i have to add the address as cofiguration value to sites. and if it is correct then how can i access the site configuaration in my provider .please guide me on that

Re: issues in Implementing Delivery Provider

Posted: 07 Apr 2021, 10:28
by aimeos
The site object doesn't currently contains any address information but you can use the config property for that. Then, you can retrieve the site configuration using:

Code: Select all

$this->getContext()->getLocale()->getSiteItem()->getConfigValue( '...' )

Re: issues in Implementing Delivery Provider

Posted: 07 Apr 2021, 10:31
by khizar
aimeos wrote: 07 Apr 2021, 10:28 The site object doesn't currently contains any address information but you can use the config property for that. Then, you can retrieve the site configuration using:

Code: Select all

$this->getContext()->getLocale()->getSiteItem()->getConfigValue( '...' )
Thanks for the reply.I misunderstood your previous reply.adding configuaration to services does the job for me as you know that for every site we have to add the services so i can give diferent address for different sites.Again Thanks for the reply

Re: issues in Implementing Delivery Provider

Posted: 07 Apr 2021, 11:39
by khizar
aimeos wrote: 07 Apr 2021, 10:28 The site object doesn't currently contains any address information but you can use the config property for that. Then, you can retrieve the site configuration using:

Code: Select all

$this->getContext()->getLocale()->getSiteItem()->getConfigValue( '...' )
hey aimeos.I got another problem .i am probably at the end of implementing delivery provider i have done every thing right and i am stuck at last point.which is that i want to store the reference number returned by my delivery company api and i have stored that you can see above . i am posting below the specific code taken from myprovider class which i have posted above.please guide me that do i have used correct code and if that is correct can you please tell me how can i access that reference no later in my orders list.

Code: Select all

<?php

namespace Aimeos\MShop\Service\Provider\Delivery;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;

class Myprovider
extends \Aimeos\MShop\Service\Provider\Delivery\Base
implements \Aimeos\MShop\Service\Provider\Delivery\Iface
{
    /**
     * Sends the order details to the ERP system for further processing.
     *
     * @param \Aimeos\MShop\Order\Item\Iface $order Order invoice object to process
     */
    public function process(\Aimeos\MShop\Order\Item\Iface $order): \Aimeos\MShop\Order\Item\Iface
    {
        $parts = \Aimeos\MShop\Order\Item\Base\Base::PARTS_ALL;
        // $parts = \Aimeos\MShop\Order\Item\Base\Base::PARTS_ADDRESS;
        $basket = $this->getOrderBase($order->getBaseId(), $parts);

      	$data = []; // data array is present in above code .i have made it short because that part is succfully done.
	$apiurl = 'http://demodashboardapi.shipsy.in/api/customer/integration/consignment/softdata';
        $response =  Http::withHeaders([
            "api-key" => "38d270602719af5c78c112be2c7ee4",
            "Content-Type" => "application/json"
        ])->post($apiurl,$data);

       
      
        
        $t = json_decode($response);

        if($t->data[0]->success){
        // here is the code for saving attributes
            $status = \Aimeos\MShop\Order\Item\Base::STAT_PROGRESS;
            $order->setDeliveryStatus( $status );
            // $this->saveOrder( $order );

            $attributes = ['reference_number' => $t->data[0]->reference_number];
            $serviceType = \Aimeos\MShop\Order\Item\Base\Service\Base::TYPE_DELIVERY;
            $orderServiceItem = $order->getService( $serviceType );
    
            $this->setAttributes( $orderServiceItem, $attributes, 'myprovider' );
            $this->saveOrderBase( $basket );
        }

        return $order;
    }
}

Now if above code is right can you please tell me how can i access that reference no in orders list

Code: Select all

	<?php foreach( $this->get( 'items', [] ) as $id => $item ) : ?>
	
	<?php endforeach; ?>
please note that the above foreach loop is from the orders list and it contains code but for the sake of simplicity i picked up only start and end of the loop can you please tell me here how can i access that reference number

Re: issues in Implementing Delivery Provider

Posted: 07 Apr 2021, 11:41
by khizar
khizar wrote: 07 Apr 2021, 11:39
aimeos wrote: 07 Apr 2021, 10:28 The site object doesn't currently contains any address information but you can use the config property for that. Then, you can retrieve the site configuration using:

Code: Select all

$this->getContext()->getLocale()->getSiteItem()->getConfigValue( '...' )
hey aimeos.I got another problem .i am probably at the end of implementing delivery provider i have done every thing right and i am stuck at last point.which is that i want to store the reference number returned by my delivery company api and i have stored that you can see above . i am posting below the specific code taken from myprovider class which i have posted above.please guide me that do i have used correct code and if that is correct can you please tell me how can i access that reference no later in my orders list.

Code: Select all

<?php

namespace Aimeos\MShop\Service\Provider\Delivery;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;

class Myprovider
extends \Aimeos\MShop\Service\Provider\Delivery\Base
implements \Aimeos\MShop\Service\Provider\Delivery\Iface
{
    /**
     * Sends the order details to the ERP system for further processing.
     *
     * @param \Aimeos\MShop\Order\Item\Iface $order Order invoice object to process
     */
    public function process(\Aimeos\MShop\Order\Item\Iface $order): \Aimeos\MShop\Order\Item\Iface
    {
        $parts = \Aimeos\MShop\Order\Item\Base\Base::PARTS_ALL;
        // $parts = \Aimeos\MShop\Order\Item\Base\Base::PARTS_ADDRESS;
        $basket = $this->getOrderBase($order->getBaseId(), $parts);

      	$data = []; // data array is present in above code .i have made it short because that part is succfully done.
	$apiurl = 'http://demodashboardapi.shipsy.in/api/customer/integration/consignment/softdata';
        $response =  Http::withHeaders([
            "api-key" => "xxxxxxxxxxxxxxxxxx",
            "Content-Type" => "application/json"
        ])->post($apiurl,$data);

       
      
        
        $t = json_decode($response);

        if($t->data[0]->success){
        // here is the code for saving attributes
            $status = \Aimeos\MShop\Order\Item\Base::STAT_PROGRESS;
            $order->setDeliveryStatus( $status );
            // $this->saveOrder( $order );

            $attributes = ['reference_number' => $t->data[0]->reference_number];
            $serviceType = \Aimeos\MShop\Order\Item\Base\Service\Base::TYPE_DELIVERY;
            $orderServiceItem = $order->getService( $serviceType );
    
            $this->setAttributes( $orderServiceItem, $attributes, 'myprovider' );
            $this->saveOrderBase( $basket );
        }

        return $order;
    }
}

Now if above code is right can you please tell me how can i access that reference no in orders list

Code: Select all

	<?php foreach( $this->get( 'items', [] ) as $id => $item ) : ?>
	
	<?php endforeach; ?>
please note that the above foreach loop is from the orders list and it contains code but for the sake of simplicity i picked up only start and end of the loop can you please tell me here how can i access that reference number

Re: issues in Implementing Delivery Provider

Posted: 07 Apr 2021, 11:46
by khizar
An immediate help will be great :D :D