1

I follow this document to create a custom shipping method, its works fine.

But I need to add Create Shipping Label functionality for this shipping method. I got these reference Link1 and Link2

They suggest Only UPS, USPS, FedEx, DHL these shipping method have functionality. I need to add my custom shipping method.

Suggest Me, how to add Create Shipping Label for our Custom Shipping method.

What I Did:

I have edit Magento\Shipping\Controller\Adminhtml\Order\ShipmentMassPrintShippingLabel to get custom shipping method details.

Then, I try to download shppinglabel for custom shipping method, but setting content not added in this file and also not supported this file.

        <?php
        /**
         *
         * Copyright © 2013-2017 Magento, Inc. All rights reserved.
         * See COPYING.txt for license details.
         */
        namespace Magento\Shipping\Controller\Adminhtml\Order\Shipment;
        use Magento\Backend\App\Action;
        use Magento\Framework\App\ResponseInterface;
        use Magento\Framework\App\Filesystem\DirectoryList;
        use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
        use Magento\Ui\Component\MassAction\Filter;
        use Magento\Backend\App\Action\Context;
        use Magento\Shipping\Model\Shipping\LabelGenerator;
        use Magento\Framework\App\Response\Http\FileFactory;
        use Magento\Sales\Model\ResourceModel\Order\Shipment\CollectionFactory as ShipmentCollectionFactory;
        use Magento\Framework\Controller\ResultInterface;
        use Magento\Sales\Model\ResourceModel\Order\CollectionFactory;
        use Spipu\Html2Pdf\Html2Pdf;

        /**
         * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
         */
        class MassPrintShippingLabel extends \Magento\Sales\Controller\Adminhtml\Order\AbstractMassAction
        {
            /**
             * Authorization level of a basic admin session
             *
             * @see _isAllowed()
             */
            const ADMIN_RESOURCE = 'Magento_Sales::shipment';

            /**
             * @var LabelGenerator
             */
            protected $labelGenerator;

            /**
             * @var FileFactory
             */
            protected $fileFactory;

            /**
             * @var CollectionFactory
             */
            protected $collectionFactory;

            /**
             * @var ShipmentCollectionFactory
             */
            protected $shipmentCollectionFactory;

            protected $logger;
            protected $repositoryAddress;

            /**
             * @param Context $context
             * @param Filter $filter
             * @param CollectionFactory $collectionFactory
             * @param FileFactory $fileFactory
             * @param LabelGenerator $labelGenerator
             * @param ShipmentCollectionFactory $shipmentCollectionFactory
             */
            public function __construct(
                Context $context,
                Filter $filter,
                CollectionFactory $collectionFactory,
                FileFactory $fileFactory,
                LabelGenerator $labelGenerator,
                ShipmentCollectionFactory $shipmentCollectionFactory,
                \Psr\Log\LoggerInterface $logger,
                \Magento\Sales\Model\Order\AddressRepository $repositoryAddress

            ) {
                $this->fileFactory = $fileFactory;
                $this->collectionFactory = $collectionFactory;
                $this->shipmentCollectionFactory = $shipmentCollectionFactory;
                $this->labelGenerator = $labelGenerator;
                $this->logger =$logger;
                $this->repositoryAddress= $repositoryAddress;
                parent::__construct($context, $filter);
            }

            /**
             * Batch print shipping labels for whole shipments.
             * Push pdf document with shipping labels to user browser
             *
             * @param AbstractCollection $collection
             * @return ResponseInterface|ResultInterface
             */
            protected function massAction(AbstractCollection $collection)
            {
                $labelsContent = [];
                $shipments = $this->shipmentCollectionFactory->create()->setOrderFilter(['in' => $collection->getAllIds()]);

                if ($shipments->getSize()) {
                    /** @var \Magento\Sales\Model\Order\Shipment $shipment */
                    foreach ($shipments as $shipment) {
                        $labelContent = $shipment->getShippingLabel();
                        if ($labelContent) {
                            $labelsContent[] = $labelContent;
                        }
                    }
                }

                if (!empty($labelsContent)) {
                    $outputPdf = $this->labelGenerator->combineLabelsPdf($labelsContent);
                    return $this->fileFactory->create(
                        'ShippingLabels.pdf',
                        $outputPdf->render(),
                        DirectoryList::VAR_DIR,
                        'application/pdf'
                    );
                }else{

                    $collection_data = $collection->getData();
                    foreach($collection_data as $collect_data){
                        $shipping_address_id = $collect_data['shipping_address_id'];
                        $shipAddress = $this->repositoryAddress->get($shipping_address_id);
                        $shipping_info_array =[];
                        if($shipAddress->getId())
                        {
                            $shipping_info = $shipAddress->getData();
                            $firstname = $shipping_info['firstname'];
                            echo $lastname = $shipping_info['lastname'];
                            echo $street = $shipping_info['street'];
                            echo $city = $shipping_info['city'];
                            echo $postcode = $shipping_info['postcode'];
                            echo $region = $shipping_info['region'];
                            echo $country = $shipping_info['country_id'];
                            array_push($shipping_info_array, $firstname);
                            $outputPdf = $this->labelGenerator->combineLabelsPdf($labelsContent);
                            return $this->fileFactory->create(
                                'ShippingLabels.pdf',
                                $firstname,
                                DirectoryList::VAR_DIR,
                                'application/pdf'
                                );
                            $this->messageManager->addSuccess(__('Your are Choosed Custom Shipping Method'));
                        }
                    }



        //         $this->messageManager->addError(__('There are no shipping labels related to selected orders.'));
                return $this->resultRedirectFactory->create()->setPath('sales/order/');
            }
            }
        }

My code download file, but its not setting any content to this file.

Suggest me How to Set Custom values to this pdf file and download pdf file.

  • Have you found a solution? I am looking for one thanks. – Juliano Vargas Jun 13 '18 at 13:44
0

First of all, Shipping method should be online and offline if you are creating online shipping method than you need to extend your model to AbstractCarrieronline instead of AbstractCarrier also you need to add some class which support AbstractCarrieronline in your model to support and working with AbstractCarrieronline also you need to get API details for creating shipping label from your shipping provider and make and API call during creating shipping label then and only then you will get PDF or PDF will be blank.

Hope this help here you mentioned you had follow inchoo blog it is for offline shipping and your needs seems for online shipping method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.