Magento 2 : How to create shipment label programmatically












0















In magento 2, I am currently building a functionality to create order,invoice and shipment via script. I can create shipment with tracking ID programmatically but could not create shipment label via script. I am using FedEx shipping method. please help how to create shipment label for FedEx programmatically.










share|improve this question









New contributor




Debditta Nandi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

























    0















    In magento 2, I am currently building a functionality to create order,invoice and shipment via script. I can create shipment with tracking ID programmatically but could not create shipment label via script. I am using FedEx shipping method. please help how to create shipment label for FedEx programmatically.










    share|improve this question









    New contributor




    Debditta Nandi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.























      0












      0








      0








      In magento 2, I am currently building a functionality to create order,invoice and shipment via script. I can create shipment with tracking ID programmatically but could not create shipment label via script. I am using FedEx shipping method. please help how to create shipment label for FedEx programmatically.










      share|improve this question









      New contributor




      Debditta Nandi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.












      In magento 2, I am currently building a functionality to create order,invoice and shipment via script. I can create shipment with tracking ID programmatically but could not create shipment label via script. I am using FedEx shipping method. please help how to create shipment label for FedEx programmatically.







      magento2.2.3 fedex






      share|improve this question









      New contributor




      Debditta Nandi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      Debditta Nandi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited 12 mins ago









      Amit Naraniwal

      40027




      40027






      New contributor




      Debditta Nandi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 20 hours ago









      Debditta NandiDebditta Nandi

      12




      12




      New contributor




      Debditta Nandi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Debditta Nandi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Debditta Nandi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          1 Answer
          1






          active

          oldest

          votes


















          0















          Please use this code for shipment creation in your script. I use the
          example order number




          $orderNumber = '00000001';
          $order = $this->orderFactory->loadByIncrementId($orderNumber);

          if($order->hasInvoices()){

          if($order->canShip()){

          $shipment = $this->orderModel->toShipment($order);

          foreach ($order->getAllItems() AS $orderItem) {

          if (!$orderItem->getQtyToShip() || $orderItem->getIsVirtual()) {
          continue;
          }
          $qtyShipped = $orderItem->getQtyToShip();

          $shipmentItem = $this->orderModel->itemToShipmentItem($orderItem)->setQty($qtyShipped);

          $shipment->addItem($shipmentItem);

          }

          $shipment->register();
          $shipment->getOrder()->setIsInProcess(true);

          try {

          $trackingIds = array(
          '0'=>array('carrier_code'=>'fedex','title' => 'Federal Express','number'=>'3131331230')
          );

          /*You can Add Multiple tracking information*/
          foreach ($trackingIds as $trackingId) {
          $data = array(
          'carrier_code' => $trackingId['carrier_code'],
          'title' => $trackingId['title'],
          'number' => $trackingId['number'],
          );
          $track = $this->trackFactory->create()->addData($data);
          $shipment->addTrack($track)->save();
          }

          $shipment->save();
          $shipment->getOrder()->save();

          // Send email
          $this->shipmentFactory->notify($shipment);
          $shipment->save();

          //********** Newly Added Part *********************//
          $labelContent = $shipment->getShippingLabel();
          if (!empty($labelContent)) {
          $outputPdf = MagentoFrameworkAppObjectManager::getInstance()->create(MagentoShippingModelShippingLabelGenerator::class)->combineLabelsPdf($labelsContent);


          echo MagentoFrameworkAppObjectManager::getInstance()->create(MagentoFrameworkAppResponseHttpFileFactory::class)->create(
          'ShippingLabels.pdf',
          $outputPdf->render(),
          MagentoFrameworkAppFilesystemDirectoryList::VAR_DIR,
          'application/pdf'
          );
          }


          } catch (Exception $e) {
          $this->logger->info($e->getMessage());
          }
          }else{
          $this->logger->info('You can not create an shipment:' . $orderNumber);
          }

          }else {
          $this->logger->info('Invoice is not created for order:' . $orderNumber);
          }


          I hope this will help






          share|improve this answer


























          • I tried with this code but I just can create shipment with tracking number with it. I am trying to create shipment label accordingly. Could you please help ?

            – Debditta Nandi
            17 hours ago











          • @DebdittaNandi Answer edited. I added more detail though I am not able to test the newly added part.

            – Muhammad Hasham
            21 mins ago











          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "479"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });






          Debditta Nandi is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f262629%2fmagento-2-how-to-create-shipment-label-programmatically%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0















          Please use this code for shipment creation in your script. I use the
          example order number




          $orderNumber = '00000001';
          $order = $this->orderFactory->loadByIncrementId($orderNumber);

          if($order->hasInvoices()){

          if($order->canShip()){

          $shipment = $this->orderModel->toShipment($order);

          foreach ($order->getAllItems() AS $orderItem) {

          if (!$orderItem->getQtyToShip() || $orderItem->getIsVirtual()) {
          continue;
          }
          $qtyShipped = $orderItem->getQtyToShip();

          $shipmentItem = $this->orderModel->itemToShipmentItem($orderItem)->setQty($qtyShipped);

          $shipment->addItem($shipmentItem);

          }

          $shipment->register();
          $shipment->getOrder()->setIsInProcess(true);

          try {

          $trackingIds = array(
          '0'=>array('carrier_code'=>'fedex','title' => 'Federal Express','number'=>'3131331230')
          );

          /*You can Add Multiple tracking information*/
          foreach ($trackingIds as $trackingId) {
          $data = array(
          'carrier_code' => $trackingId['carrier_code'],
          'title' => $trackingId['title'],
          'number' => $trackingId['number'],
          );
          $track = $this->trackFactory->create()->addData($data);
          $shipment->addTrack($track)->save();
          }

          $shipment->save();
          $shipment->getOrder()->save();

          // Send email
          $this->shipmentFactory->notify($shipment);
          $shipment->save();

          //********** Newly Added Part *********************//
          $labelContent = $shipment->getShippingLabel();
          if (!empty($labelContent)) {
          $outputPdf = MagentoFrameworkAppObjectManager::getInstance()->create(MagentoShippingModelShippingLabelGenerator::class)->combineLabelsPdf($labelsContent);


          echo MagentoFrameworkAppObjectManager::getInstance()->create(MagentoFrameworkAppResponseHttpFileFactory::class)->create(
          'ShippingLabels.pdf',
          $outputPdf->render(),
          MagentoFrameworkAppFilesystemDirectoryList::VAR_DIR,
          'application/pdf'
          );
          }


          } catch (Exception $e) {
          $this->logger->info($e->getMessage());
          }
          }else{
          $this->logger->info('You can not create an shipment:' . $orderNumber);
          }

          }else {
          $this->logger->info('Invoice is not created for order:' . $orderNumber);
          }


          I hope this will help






          share|improve this answer


























          • I tried with this code but I just can create shipment with tracking number with it. I am trying to create shipment label accordingly. Could you please help ?

            – Debditta Nandi
            17 hours ago











          • @DebdittaNandi Answer edited. I added more detail though I am not able to test the newly added part.

            – Muhammad Hasham
            21 mins ago
















          0















          Please use this code for shipment creation in your script. I use the
          example order number




          $orderNumber = '00000001';
          $order = $this->orderFactory->loadByIncrementId($orderNumber);

          if($order->hasInvoices()){

          if($order->canShip()){

          $shipment = $this->orderModel->toShipment($order);

          foreach ($order->getAllItems() AS $orderItem) {

          if (!$orderItem->getQtyToShip() || $orderItem->getIsVirtual()) {
          continue;
          }
          $qtyShipped = $orderItem->getQtyToShip();

          $shipmentItem = $this->orderModel->itemToShipmentItem($orderItem)->setQty($qtyShipped);

          $shipment->addItem($shipmentItem);

          }

          $shipment->register();
          $shipment->getOrder()->setIsInProcess(true);

          try {

          $trackingIds = array(
          '0'=>array('carrier_code'=>'fedex','title' => 'Federal Express','number'=>'3131331230')
          );

          /*You can Add Multiple tracking information*/
          foreach ($trackingIds as $trackingId) {
          $data = array(
          'carrier_code' => $trackingId['carrier_code'],
          'title' => $trackingId['title'],
          'number' => $trackingId['number'],
          );
          $track = $this->trackFactory->create()->addData($data);
          $shipment->addTrack($track)->save();
          }

          $shipment->save();
          $shipment->getOrder()->save();

          // Send email
          $this->shipmentFactory->notify($shipment);
          $shipment->save();

          //********** Newly Added Part *********************//
          $labelContent = $shipment->getShippingLabel();
          if (!empty($labelContent)) {
          $outputPdf = MagentoFrameworkAppObjectManager::getInstance()->create(MagentoShippingModelShippingLabelGenerator::class)->combineLabelsPdf($labelsContent);


          echo MagentoFrameworkAppObjectManager::getInstance()->create(MagentoFrameworkAppResponseHttpFileFactory::class)->create(
          'ShippingLabels.pdf',
          $outputPdf->render(),
          MagentoFrameworkAppFilesystemDirectoryList::VAR_DIR,
          'application/pdf'
          );
          }


          } catch (Exception $e) {
          $this->logger->info($e->getMessage());
          }
          }else{
          $this->logger->info('You can not create an shipment:' . $orderNumber);
          }

          }else {
          $this->logger->info('Invoice is not created for order:' . $orderNumber);
          }


          I hope this will help






          share|improve this answer


























          • I tried with this code but I just can create shipment with tracking number with it. I am trying to create shipment label accordingly. Could you please help ?

            – Debditta Nandi
            17 hours ago











          • @DebdittaNandi Answer edited. I added more detail though I am not able to test the newly added part.

            – Muhammad Hasham
            21 mins ago














          0












          0








          0








          Please use this code for shipment creation in your script. I use the
          example order number




          $orderNumber = '00000001';
          $order = $this->orderFactory->loadByIncrementId($orderNumber);

          if($order->hasInvoices()){

          if($order->canShip()){

          $shipment = $this->orderModel->toShipment($order);

          foreach ($order->getAllItems() AS $orderItem) {

          if (!$orderItem->getQtyToShip() || $orderItem->getIsVirtual()) {
          continue;
          }
          $qtyShipped = $orderItem->getQtyToShip();

          $shipmentItem = $this->orderModel->itemToShipmentItem($orderItem)->setQty($qtyShipped);

          $shipment->addItem($shipmentItem);

          }

          $shipment->register();
          $shipment->getOrder()->setIsInProcess(true);

          try {

          $trackingIds = array(
          '0'=>array('carrier_code'=>'fedex','title' => 'Federal Express','number'=>'3131331230')
          );

          /*You can Add Multiple tracking information*/
          foreach ($trackingIds as $trackingId) {
          $data = array(
          'carrier_code' => $trackingId['carrier_code'],
          'title' => $trackingId['title'],
          'number' => $trackingId['number'],
          );
          $track = $this->trackFactory->create()->addData($data);
          $shipment->addTrack($track)->save();
          }

          $shipment->save();
          $shipment->getOrder()->save();

          // Send email
          $this->shipmentFactory->notify($shipment);
          $shipment->save();

          //********** Newly Added Part *********************//
          $labelContent = $shipment->getShippingLabel();
          if (!empty($labelContent)) {
          $outputPdf = MagentoFrameworkAppObjectManager::getInstance()->create(MagentoShippingModelShippingLabelGenerator::class)->combineLabelsPdf($labelsContent);


          echo MagentoFrameworkAppObjectManager::getInstance()->create(MagentoFrameworkAppResponseHttpFileFactory::class)->create(
          'ShippingLabels.pdf',
          $outputPdf->render(),
          MagentoFrameworkAppFilesystemDirectoryList::VAR_DIR,
          'application/pdf'
          );
          }


          } catch (Exception $e) {
          $this->logger->info($e->getMessage());
          }
          }else{
          $this->logger->info('You can not create an shipment:' . $orderNumber);
          }

          }else {
          $this->logger->info('Invoice is not created for order:' . $orderNumber);
          }


          I hope this will help






          share|improve this answer
















          Please use this code for shipment creation in your script. I use the
          example order number




          $orderNumber = '00000001';
          $order = $this->orderFactory->loadByIncrementId($orderNumber);

          if($order->hasInvoices()){

          if($order->canShip()){

          $shipment = $this->orderModel->toShipment($order);

          foreach ($order->getAllItems() AS $orderItem) {

          if (!$orderItem->getQtyToShip() || $orderItem->getIsVirtual()) {
          continue;
          }
          $qtyShipped = $orderItem->getQtyToShip();

          $shipmentItem = $this->orderModel->itemToShipmentItem($orderItem)->setQty($qtyShipped);

          $shipment->addItem($shipmentItem);

          }

          $shipment->register();
          $shipment->getOrder()->setIsInProcess(true);

          try {

          $trackingIds = array(
          '0'=>array('carrier_code'=>'fedex','title' => 'Federal Express','number'=>'3131331230')
          );

          /*You can Add Multiple tracking information*/
          foreach ($trackingIds as $trackingId) {
          $data = array(
          'carrier_code' => $trackingId['carrier_code'],
          'title' => $trackingId['title'],
          'number' => $trackingId['number'],
          );
          $track = $this->trackFactory->create()->addData($data);
          $shipment->addTrack($track)->save();
          }

          $shipment->save();
          $shipment->getOrder()->save();

          // Send email
          $this->shipmentFactory->notify($shipment);
          $shipment->save();

          //********** Newly Added Part *********************//
          $labelContent = $shipment->getShippingLabel();
          if (!empty($labelContent)) {
          $outputPdf = MagentoFrameworkAppObjectManager::getInstance()->create(MagentoShippingModelShippingLabelGenerator::class)->combineLabelsPdf($labelsContent);


          echo MagentoFrameworkAppObjectManager::getInstance()->create(MagentoFrameworkAppResponseHttpFileFactory::class)->create(
          'ShippingLabels.pdf',
          $outputPdf->render(),
          MagentoFrameworkAppFilesystemDirectoryList::VAR_DIR,
          'application/pdf'
          );
          }


          } catch (Exception $e) {
          $this->logger->info($e->getMessage());
          }
          }else{
          $this->logger->info('You can not create an shipment:' . $orderNumber);
          }

          }else {
          $this->logger->info('Invoice is not created for order:' . $orderNumber);
          }


          I hope this will help







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 22 mins ago

























          answered 20 hours ago









          Muhammad HashamMuhammad Hasham

          1,725423




          1,725423













          • I tried with this code but I just can create shipment with tracking number with it. I am trying to create shipment label accordingly. Could you please help ?

            – Debditta Nandi
            17 hours ago











          • @DebdittaNandi Answer edited. I added more detail though I am not able to test the newly added part.

            – Muhammad Hasham
            21 mins ago



















          • I tried with this code but I just can create shipment with tracking number with it. I am trying to create shipment label accordingly. Could you please help ?

            – Debditta Nandi
            17 hours ago











          • @DebdittaNandi Answer edited. I added more detail though I am not able to test the newly added part.

            – Muhammad Hasham
            21 mins ago

















          I tried with this code but I just can create shipment with tracking number with it. I am trying to create shipment label accordingly. Could you please help ?

          – Debditta Nandi
          17 hours ago





          I tried with this code but I just can create shipment with tracking number with it. I am trying to create shipment label accordingly. Could you please help ?

          – Debditta Nandi
          17 hours ago













          @DebdittaNandi Answer edited. I added more detail though I am not able to test the newly added part.

          – Muhammad Hasham
          21 mins ago





          @DebdittaNandi Answer edited. I added more detail though I am not able to test the newly added part.

          – Muhammad Hasham
          21 mins ago










          Debditta Nandi is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          Debditta Nandi is a new contributor. Be nice, and check out our Code of Conduct.













          Debditta Nandi is a new contributor. Be nice, and check out our Code of Conduct.












          Debditta Nandi is a new contributor. Be nice, and check out our Code of Conduct.
















          Thanks for contributing an answer to Magento Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f262629%2fmagento-2-how-to-create-shipment-label-programmatically%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Polycentropodidae

          Magento 2 Error message: Invalid state change requested

          Paulmy