Magento 2 : How to create shipment label programmatically
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
New contributor
add a comment |
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
New contributor
add a comment |
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
New contributor
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
magento2.2.3 fedex
New contributor
New contributor
edited 23 mins ago
Amit Naraniwal
40237
40237
New contributor
asked 20 hours ago
Debditta NandiDebditta Nandi
12
12
New contributor
New contributor
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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
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
32 mins ago
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
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
32 mins ago
add a comment |
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
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
32 mins ago
add a comment |
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
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
edited 33 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
32 mins ago
add a comment |
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
32 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
32 mins ago
@DebdittaNandi Answer edited. I added more detail though I am not able to test the newly added part.
– Muhammad Hasham
32 mins ago
add a comment |
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.
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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