How do i create a shipment class object in magento 2? The 2019 Stack Overflow Developer Survey Results Are InCreate custom module to modify sales/order/shipment/api.phpCall to a member function setLock() on a non-object app/code/core/Mage/Index/Model/Lock.php on line 162Unit Test for overwrite collection class in magento2main.CRITICAL: Plugin class doesn't existHow to change save path of PDF files in magento 2Magento 2.1.6 Cannot save shipmentUnable to create order from admin in magento 2magento 2.2 load model vs objectManager: the first returns the wrong id while the second returns the correct oneMonolog Error After 2.2 UpgradeAdding tracking number to existing orders from CSV file Programmatically in Magento 2

How can I create a character who can assume the widest possible range of creature sizes?

What tool would a Roman-age civilization have to grind silver and other metals into dust?

Why is it "Tumoren" and not "Tumore"?

Is there a name of the flying bionic bird?

Why don't Unix/Linux systems traverse through directories until they find the required version of a linked library?

How are circuits which use complex ICs normally simulated?

How to change the limits of integration

Extreme, unacceptable situation and I can't attend work tomorrow morning

What is this 4-propeller plane?

Is this food a bread or a loaf?

aging parents with no investments

Is three citations per paragraph excessive for undergraduate research paper?

How to deal with fear of taking dependencies

Why do UK politicians seemingly ignore opinion polls on Brexit?

Monty Hall variation

Inflated grade on resume at previous job, might former employer tell new employer?

How can I fix this gap between bookcases I made?

Geography at the pixel level

If a poisoned arrow's piercing damage is reduced to 0, do you still get poisoned?

Should I write numbers in words or as numerals when there are multiple next to each other?

Limit the amount of RAM Mathematica may access?

Which Sci-Fi work first showed weapon of galactic-scale mass destruction?

Does duplicating a spell with wish count as casting that spell?

Evaluating number of iteration with a certain map with While



How do i create a shipment class object in magento 2?



The 2019 Stack Overflow Developer Survey Results Are InCreate custom module to modify sales/order/shipment/api.phpCall to a member function setLock() on a non-object app/code/core/Mage/Index/Model/Lock.php on line 162Unit Test for overwrite collection class in magento2main.CRITICAL: Plugin class doesn't existHow to change save path of PDF files in magento 2Magento 2.1.6 Cannot save shipmentUnable to create order from admin in magento 2magento 2.2 load model vs objectManager: the first returns the wrong id while the second returns the correct oneMonolog Error After 2.2 UpgradeAdding tracking number to existing orders from CSV file Programmatically in Magento 2



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








2















i am trying to get tracking from csv and save to magento 2 order programmatically.
i grabbed the create shipment & add tracking code from here https://www.scommerce-mage.com/blog/magento-2-how-to-create-shipment-programatically.html, and add an execute function (line 142) to load data from csv. but i'm not sure how to create an shipment object (line 179).



<?php
$file = fopen('track.csv', 'r', '"'); // set path to the CSV file
if ($file !== false)

require __DIR__ . '/app/bootstrap.php';
$bootstrap = MagentoFrameworkAppBootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get('MagentoFrameworkAppState');
$state->setAreaCode('adminhtml');
// add logging capability
$writer = new ZendLogWriterStream(BP . '/var/log/import-update.log');
$logger = new ZendLogLogger();
$logger->addWriter($writer);


class ShipmentObject
/*There are two main functions
prepareShipment of MagentoSalesModelOrderShipmentFactory class which prepare invoice and
addObject of MagentoFrameworkDBTransactionFactory class which helps to create shipment and associate the shipment with original order in Magento 2.
*/

/**
* @var MagentoSalesModelOrderShipmentTrackFactory
*/
protected $_shipmentTrackFactory;

/**
* The ShipmentFactory is used to create a new Shipment.
*
* @var MagentoSalesModelOrderShipmentFactory
*/
protected $_shipmentFactory;

/**
* @var MagentoFrameworkDBTransactionFactory
*/
protected $_transactionFactory;

/**
* @var MagentoSalesApiOrderRepositoryInterface
*/
protected $_orderRepository;

protected $_file;
/**
* @param MagentoSalesModelOrderShipmentTrackFactory $shipmentTrackFactory
* @param MagentoSalesModelOrderShipmentFactory $shipmentFactory
* @param MagentoFrameworkDBTransactionFactory $transactionFactory
* @param MagentoSalesApiOrderRepositoryInterface $orderRepository
*/
public function __construct(
MagentoSalesModelOrderShipmentTrackFactory $shipmentTrackFactory,
MagentoSalesModelOrderShipmentFactory $shipmentFactory,
MagentoFrameworkDBTransactionFactory $transactionFactory,
MagentoSalesApiOrderRepositoryInterface $orderRepository,
MagentoSalesModelOrderFactory $orderFactory
)
$this->_shipmentTrackFactory = $shipmentTrackFactory;
$this->_shipmentFactory = $shipmentFactory;
$this->_transactionFactory = $transactionFactory;
$this->_orderRepository = $orderRepository;
$this->orderFactory = $orderFactory;

/**
* @param int $orderId
* @param string $trackingNumber
* @return MagentoSalesModelShipment $shipment
*/
protected function createShipment($orderId, $trackingNumber)

try
//$order = $this->_orderRepository->get($orderId); // this is increment id
$order = $this->orderFactory->create()->loadByIncrementId($orderId);

if ($order)
$data = array(array(
'carrier_code' => 'fedex', //$order->getShippingMethod(), //'carrier_code' => 'fedex', // need to lower case
'title' => 'Federal Express', //$order->getShippingDescription(), //'title' => 'Federal Express', // how to set here 'Federal Express' or 'United Parcel Service'
'number' => $trackingNumber, //$trackingnumber,
));

$shipment = $this->prepareShipment($order, $data);
/*
if ($shipment)
$order->setIsInProcess(true);
$order->addStatusHistoryComment('Automatically SHIPPED', false);
$transactionSave = $this->_transactionFactory->create()->addObject($shipment)->addObject($shipment->getOrder());
$transactionSave->save();

*/
if ($shipment)
$order->setIsInProcess(true);
$order->addStatusHistoryComment('Automatically SHIPPED', false);
$transactionSave = $this->_transactionFactory->create();
$transactionSave->addObject($shipment);
$transactionSave->addObject($shipment->getOrder());

$transactionSave->save();


//return $shipment; //original
//return $shipment->save(); //magefms
return $order; //helgeB

catch (Exception $e)
throw new MagentoFrameworkExceptionLocalizedException(
__($e->getMessage())
);



/**
* @param $order MagentoSalesModelOrder
* @param $track array
* @return $this
*/
protected function prepareShipment($order, $track)

$shipment = $this->_shipmentFactory->create(
$order,
$this->prepareShipmentItems($order),
$track
);
return $shipment->getTotalQty() ? $shipment->register() : false;


/**
* @param $order MagentoSalesModelOrder
* @return array
*/
protected function prepareShipmentItems($order)

$items = [];

foreach($order->getAllItems() as $item)
$items[$item->getItemId()] = $item->getQtyOrdered();

return $items;

//after prepareShipmentItems function
/**
* @param $file
*/
public function setFile($file)

$this->_file = $file;


/**
* @param null
* @return ordersave
*/
public function execute()

// enter the number of data fields you require the product row inside the CSV file to contain

$required_data_fields = 3; //number of column in csv
//$header = fgetcsv($file); // get data headers and skip 1st row

while (($row = fgetcsv($this->_file, 100, ",")) !== FALSE)
//while (($row = fgetcsv($file, 100, ",")) !== FALSE)
//while ( $row = fgetcsv($file, 100, ",") )
//while (($row = fgetcsv($file)) !== FALSE)

$data_count = count($row);
if ($data_count < 1)
continue;


$data = array();
//$data = array_combine($header, $row);
$data = array($row);

$ponumber = $row[0]; //$data['ponumber']; // column A
if ($data_count < $required_data_fields)
$logger->info("Skipping Order Number " . $ponumber . ". Not enough data to import.");
continue;

//$shipvia = trim($data[1]); //trim($data['shipvia']); // column B
//$trackingnumber = trim($data[2]);//trim($data['trackingnumber']); // column C
$shipvia = isset($row[1]) ? $row[1] : null;
$trackingnumber = isset($row[2]) ? $row[2] : null;

switch ($shipvia)
case "FEDEX":
$shipvia = "fedex";
break;
case "UPS":
$shipvia = "ups";
break;
case "USPS":
$shipvia = "usps";
break;
default:
$shipvia = "fedex";


///////////////////////////////start adding tracking///////////////////////////////
echo 'Updating Order: '.$ponumber.', with '.$shipvia.':'.$trackingnumber."rn";
//print_r($row);

$order = $this->createShipment($ponumber, $trackingnumber); /* @param int $orderId@param // end of while loop

//end of execute function

// end of class shipmentobject

/**
* @param MagentoSalesModelOrderShipmentTrackFactory $shipmentTrackFactory
* @param MagentoSalesModelOrderShipmentFactory $shipmentFactory
* @param MagentoFrameworkDBTransactionFactory $transactionFactory
* @param MagentoSalesApiOrderRepositoryInterface $orderRepository
*/
$shipmentTrackFactory = $objectManager->get('MagentoSalesModelOrderShipmentTrackFactory');
$shipmentFactory = $objectManager->get('MagentoSalesModelOrderShipmentFactory');
$transactionFactory = $objectManager->get('MagentoFrameworkDBTransactionFactory');
$orderRepository = $objectManager->get('MagentoSalesApiOrderRepositoryInterface');
$orderFactory = $objectManager->get('MagentoSalesModelOrderFactory');

//create new class object
$test = new ShipmentObject($shipmentTrackFactory,$shipmentFactory,$transactionFactory,$orderRepository,$orderFactory);
$test->setFile($file);
$test->execute();
fclose($file);
// if file not exist, then do nothing


new error
tracking information does not copy to (shipment tab) "sales_shipment_grid"



it is showing like this in admin panel, the shipment tab is empty, though the tracking is in there with no carrier assign.



enter image description hereenter image description hereenter image description here










share|improve this question






























    2















    i am trying to get tracking from csv and save to magento 2 order programmatically.
    i grabbed the create shipment & add tracking code from here https://www.scommerce-mage.com/blog/magento-2-how-to-create-shipment-programatically.html, and add an execute function (line 142) to load data from csv. but i'm not sure how to create an shipment object (line 179).



    <?php
    $file = fopen('track.csv', 'r', '"'); // set path to the CSV file
    if ($file !== false)

    require __DIR__ . '/app/bootstrap.php';
    $bootstrap = MagentoFrameworkAppBootstrap::create(BP, $_SERVER);
    $objectManager = $bootstrap->getObjectManager();
    $state = $objectManager->get('MagentoFrameworkAppState');
    $state->setAreaCode('adminhtml');
    // add logging capability
    $writer = new ZendLogWriterStream(BP . '/var/log/import-update.log');
    $logger = new ZendLogLogger();
    $logger->addWriter($writer);


    class ShipmentObject
    /*There are two main functions
    prepareShipment of MagentoSalesModelOrderShipmentFactory class which prepare invoice and
    addObject of MagentoFrameworkDBTransactionFactory class which helps to create shipment and associate the shipment with original order in Magento 2.
    */

    /**
    * @var MagentoSalesModelOrderShipmentTrackFactory
    */
    protected $_shipmentTrackFactory;

    /**
    * The ShipmentFactory is used to create a new Shipment.
    *
    * @var MagentoSalesModelOrderShipmentFactory
    */
    protected $_shipmentFactory;

    /**
    * @var MagentoFrameworkDBTransactionFactory
    */
    protected $_transactionFactory;

    /**
    * @var MagentoSalesApiOrderRepositoryInterface
    */
    protected $_orderRepository;

    protected $_file;
    /**
    * @param MagentoSalesModelOrderShipmentTrackFactory $shipmentTrackFactory
    * @param MagentoSalesModelOrderShipmentFactory $shipmentFactory
    * @param MagentoFrameworkDBTransactionFactory $transactionFactory
    * @param MagentoSalesApiOrderRepositoryInterface $orderRepository
    */
    public function __construct(
    MagentoSalesModelOrderShipmentTrackFactory $shipmentTrackFactory,
    MagentoSalesModelOrderShipmentFactory $shipmentFactory,
    MagentoFrameworkDBTransactionFactory $transactionFactory,
    MagentoSalesApiOrderRepositoryInterface $orderRepository,
    MagentoSalesModelOrderFactory $orderFactory
    )
    $this->_shipmentTrackFactory = $shipmentTrackFactory;
    $this->_shipmentFactory = $shipmentFactory;
    $this->_transactionFactory = $transactionFactory;
    $this->_orderRepository = $orderRepository;
    $this->orderFactory = $orderFactory;

    /**
    * @param int $orderId
    * @param string $trackingNumber
    * @return MagentoSalesModelShipment $shipment
    */
    protected function createShipment($orderId, $trackingNumber)

    try
    //$order = $this->_orderRepository->get($orderId); // this is increment id
    $order = $this->orderFactory->create()->loadByIncrementId($orderId);

    if ($order)
    $data = array(array(
    'carrier_code' => 'fedex', //$order->getShippingMethod(), //'carrier_code' => 'fedex', // need to lower case
    'title' => 'Federal Express', //$order->getShippingDescription(), //'title' => 'Federal Express', // how to set here 'Federal Express' or 'United Parcel Service'
    'number' => $trackingNumber, //$trackingnumber,
    ));

    $shipment = $this->prepareShipment($order, $data);
    /*
    if ($shipment)
    $order->setIsInProcess(true);
    $order->addStatusHistoryComment('Automatically SHIPPED', false);
    $transactionSave = $this->_transactionFactory->create()->addObject($shipment)->addObject($shipment->getOrder());
    $transactionSave->save();

    */
    if ($shipment)
    $order->setIsInProcess(true);
    $order->addStatusHistoryComment('Automatically SHIPPED', false);
    $transactionSave = $this->_transactionFactory->create();
    $transactionSave->addObject($shipment);
    $transactionSave->addObject($shipment->getOrder());

    $transactionSave->save();


    //return $shipment; //original
    //return $shipment->save(); //magefms
    return $order; //helgeB

    catch (Exception $e)
    throw new MagentoFrameworkExceptionLocalizedException(
    __($e->getMessage())
    );



    /**
    * @param $order MagentoSalesModelOrder
    * @param $track array
    * @return $this
    */
    protected function prepareShipment($order, $track)

    $shipment = $this->_shipmentFactory->create(
    $order,
    $this->prepareShipmentItems($order),
    $track
    );
    return $shipment->getTotalQty() ? $shipment->register() : false;


    /**
    * @param $order MagentoSalesModelOrder
    * @return array
    */
    protected function prepareShipmentItems($order)

    $items = [];

    foreach($order->getAllItems() as $item)
    $items[$item->getItemId()] = $item->getQtyOrdered();

    return $items;

    //after prepareShipmentItems function
    /**
    * @param $file
    */
    public function setFile($file)

    $this->_file = $file;


    /**
    * @param null
    * @return ordersave
    */
    public function execute()

    // enter the number of data fields you require the product row inside the CSV file to contain

    $required_data_fields = 3; //number of column in csv
    //$header = fgetcsv($file); // get data headers and skip 1st row

    while (($row = fgetcsv($this->_file, 100, ",")) !== FALSE)
    //while (($row = fgetcsv($file, 100, ",")) !== FALSE)
    //while ( $row = fgetcsv($file, 100, ",") )
    //while (($row = fgetcsv($file)) !== FALSE)

    $data_count = count($row);
    if ($data_count < 1)
    continue;


    $data = array();
    //$data = array_combine($header, $row);
    $data = array($row);

    $ponumber = $row[0]; //$data['ponumber']; // column A
    if ($data_count < $required_data_fields)
    $logger->info("Skipping Order Number " . $ponumber . ". Not enough data to import.");
    continue;

    //$shipvia = trim($data[1]); //trim($data['shipvia']); // column B
    //$trackingnumber = trim($data[2]);//trim($data['trackingnumber']); // column C
    $shipvia = isset($row[1]) ? $row[1] : null;
    $trackingnumber = isset($row[2]) ? $row[2] : null;

    switch ($shipvia)
    case "FEDEX":
    $shipvia = "fedex";
    break;
    case "UPS":
    $shipvia = "ups";
    break;
    case "USPS":
    $shipvia = "usps";
    break;
    default:
    $shipvia = "fedex";


    ///////////////////////////////start adding tracking///////////////////////////////
    echo 'Updating Order: '.$ponumber.', with '.$shipvia.':'.$trackingnumber."rn";
    //print_r($row);

    $order = $this->createShipment($ponumber, $trackingnumber); /* @param int $orderId@param // end of while loop

    //end of execute function

    // end of class shipmentobject

    /**
    * @param MagentoSalesModelOrderShipmentTrackFactory $shipmentTrackFactory
    * @param MagentoSalesModelOrderShipmentFactory $shipmentFactory
    * @param MagentoFrameworkDBTransactionFactory $transactionFactory
    * @param MagentoSalesApiOrderRepositoryInterface $orderRepository
    */
    $shipmentTrackFactory = $objectManager->get('MagentoSalesModelOrderShipmentTrackFactory');
    $shipmentFactory = $objectManager->get('MagentoSalesModelOrderShipmentFactory');
    $transactionFactory = $objectManager->get('MagentoFrameworkDBTransactionFactory');
    $orderRepository = $objectManager->get('MagentoSalesApiOrderRepositoryInterface');
    $orderFactory = $objectManager->get('MagentoSalesModelOrderFactory');

    //create new class object
    $test = new ShipmentObject($shipmentTrackFactory,$shipmentFactory,$transactionFactory,$orderRepository,$orderFactory);
    $test->setFile($file);
    $test->execute();
    fclose($file);
    // if file not exist, then do nothing


    new error
    tracking information does not copy to (shipment tab) "sales_shipment_grid"



    it is showing like this in admin panel, the shipment tab is empty, though the tracking is in there with no carrier assign.



    enter image description hereenter image description hereenter image description here










    share|improve this question


























      2












      2








      2








      i am trying to get tracking from csv and save to magento 2 order programmatically.
      i grabbed the create shipment & add tracking code from here https://www.scommerce-mage.com/blog/magento-2-how-to-create-shipment-programatically.html, and add an execute function (line 142) to load data from csv. but i'm not sure how to create an shipment object (line 179).



      <?php
      $file = fopen('track.csv', 'r', '"'); // set path to the CSV file
      if ($file !== false)

      require __DIR__ . '/app/bootstrap.php';
      $bootstrap = MagentoFrameworkAppBootstrap::create(BP, $_SERVER);
      $objectManager = $bootstrap->getObjectManager();
      $state = $objectManager->get('MagentoFrameworkAppState');
      $state->setAreaCode('adminhtml');
      // add logging capability
      $writer = new ZendLogWriterStream(BP . '/var/log/import-update.log');
      $logger = new ZendLogLogger();
      $logger->addWriter($writer);


      class ShipmentObject
      /*There are two main functions
      prepareShipment of MagentoSalesModelOrderShipmentFactory class which prepare invoice and
      addObject of MagentoFrameworkDBTransactionFactory class which helps to create shipment and associate the shipment with original order in Magento 2.
      */

      /**
      * @var MagentoSalesModelOrderShipmentTrackFactory
      */
      protected $_shipmentTrackFactory;

      /**
      * The ShipmentFactory is used to create a new Shipment.
      *
      * @var MagentoSalesModelOrderShipmentFactory
      */
      protected $_shipmentFactory;

      /**
      * @var MagentoFrameworkDBTransactionFactory
      */
      protected $_transactionFactory;

      /**
      * @var MagentoSalesApiOrderRepositoryInterface
      */
      protected $_orderRepository;

      protected $_file;
      /**
      * @param MagentoSalesModelOrderShipmentTrackFactory $shipmentTrackFactory
      * @param MagentoSalesModelOrderShipmentFactory $shipmentFactory
      * @param MagentoFrameworkDBTransactionFactory $transactionFactory
      * @param MagentoSalesApiOrderRepositoryInterface $orderRepository
      */
      public function __construct(
      MagentoSalesModelOrderShipmentTrackFactory $shipmentTrackFactory,
      MagentoSalesModelOrderShipmentFactory $shipmentFactory,
      MagentoFrameworkDBTransactionFactory $transactionFactory,
      MagentoSalesApiOrderRepositoryInterface $orderRepository,
      MagentoSalesModelOrderFactory $orderFactory
      )
      $this->_shipmentTrackFactory = $shipmentTrackFactory;
      $this->_shipmentFactory = $shipmentFactory;
      $this->_transactionFactory = $transactionFactory;
      $this->_orderRepository = $orderRepository;
      $this->orderFactory = $orderFactory;

      /**
      * @param int $orderId
      * @param string $trackingNumber
      * @return MagentoSalesModelShipment $shipment
      */
      protected function createShipment($orderId, $trackingNumber)

      try
      //$order = $this->_orderRepository->get($orderId); // this is increment id
      $order = $this->orderFactory->create()->loadByIncrementId($orderId);

      if ($order)
      $data = array(array(
      'carrier_code' => 'fedex', //$order->getShippingMethod(), //'carrier_code' => 'fedex', // need to lower case
      'title' => 'Federal Express', //$order->getShippingDescription(), //'title' => 'Federal Express', // how to set here 'Federal Express' or 'United Parcel Service'
      'number' => $trackingNumber, //$trackingnumber,
      ));

      $shipment = $this->prepareShipment($order, $data);
      /*
      if ($shipment)
      $order->setIsInProcess(true);
      $order->addStatusHistoryComment('Automatically SHIPPED', false);
      $transactionSave = $this->_transactionFactory->create()->addObject($shipment)->addObject($shipment->getOrder());
      $transactionSave->save();

      */
      if ($shipment)
      $order->setIsInProcess(true);
      $order->addStatusHistoryComment('Automatically SHIPPED', false);
      $transactionSave = $this->_transactionFactory->create();
      $transactionSave->addObject($shipment);
      $transactionSave->addObject($shipment->getOrder());

      $transactionSave->save();


      //return $shipment; //original
      //return $shipment->save(); //magefms
      return $order; //helgeB

      catch (Exception $e)
      throw new MagentoFrameworkExceptionLocalizedException(
      __($e->getMessage())
      );



      /**
      * @param $order MagentoSalesModelOrder
      * @param $track array
      * @return $this
      */
      protected function prepareShipment($order, $track)

      $shipment = $this->_shipmentFactory->create(
      $order,
      $this->prepareShipmentItems($order),
      $track
      );
      return $shipment->getTotalQty() ? $shipment->register() : false;


      /**
      * @param $order MagentoSalesModelOrder
      * @return array
      */
      protected function prepareShipmentItems($order)

      $items = [];

      foreach($order->getAllItems() as $item)
      $items[$item->getItemId()] = $item->getQtyOrdered();

      return $items;

      //after prepareShipmentItems function
      /**
      * @param $file
      */
      public function setFile($file)

      $this->_file = $file;


      /**
      * @param null
      * @return ordersave
      */
      public function execute()

      // enter the number of data fields you require the product row inside the CSV file to contain

      $required_data_fields = 3; //number of column in csv
      //$header = fgetcsv($file); // get data headers and skip 1st row

      while (($row = fgetcsv($this->_file, 100, ",")) !== FALSE)
      //while (($row = fgetcsv($file, 100, ",")) !== FALSE)
      //while ( $row = fgetcsv($file, 100, ",") )
      //while (($row = fgetcsv($file)) !== FALSE)

      $data_count = count($row);
      if ($data_count < 1)
      continue;


      $data = array();
      //$data = array_combine($header, $row);
      $data = array($row);

      $ponumber = $row[0]; //$data['ponumber']; // column A
      if ($data_count < $required_data_fields)
      $logger->info("Skipping Order Number " . $ponumber . ". Not enough data to import.");
      continue;

      //$shipvia = trim($data[1]); //trim($data['shipvia']); // column B
      //$trackingnumber = trim($data[2]);//trim($data['trackingnumber']); // column C
      $shipvia = isset($row[1]) ? $row[1] : null;
      $trackingnumber = isset($row[2]) ? $row[2] : null;

      switch ($shipvia)
      case "FEDEX":
      $shipvia = "fedex";
      break;
      case "UPS":
      $shipvia = "ups";
      break;
      case "USPS":
      $shipvia = "usps";
      break;
      default:
      $shipvia = "fedex";


      ///////////////////////////////start adding tracking///////////////////////////////
      echo 'Updating Order: '.$ponumber.', with '.$shipvia.':'.$trackingnumber."rn";
      //print_r($row);

      $order = $this->createShipment($ponumber, $trackingnumber); /* @param int $orderId@param // end of while loop

      //end of execute function

      // end of class shipmentobject

      /**
      * @param MagentoSalesModelOrderShipmentTrackFactory $shipmentTrackFactory
      * @param MagentoSalesModelOrderShipmentFactory $shipmentFactory
      * @param MagentoFrameworkDBTransactionFactory $transactionFactory
      * @param MagentoSalesApiOrderRepositoryInterface $orderRepository
      */
      $shipmentTrackFactory = $objectManager->get('MagentoSalesModelOrderShipmentTrackFactory');
      $shipmentFactory = $objectManager->get('MagentoSalesModelOrderShipmentFactory');
      $transactionFactory = $objectManager->get('MagentoFrameworkDBTransactionFactory');
      $orderRepository = $objectManager->get('MagentoSalesApiOrderRepositoryInterface');
      $orderFactory = $objectManager->get('MagentoSalesModelOrderFactory');

      //create new class object
      $test = new ShipmentObject($shipmentTrackFactory,$shipmentFactory,$transactionFactory,$orderRepository,$orderFactory);
      $test->setFile($file);
      $test->execute();
      fclose($file);
      // if file not exist, then do nothing


      new error
      tracking information does not copy to (shipment tab) "sales_shipment_grid"



      it is showing like this in admin panel, the shipment tab is empty, though the tracking is in there with no carrier assign.



      enter image description hereenter image description hereenter image description here










      share|improve this question
















      i am trying to get tracking from csv and save to magento 2 order programmatically.
      i grabbed the create shipment & add tracking code from here https://www.scommerce-mage.com/blog/magento-2-how-to-create-shipment-programatically.html, and add an execute function (line 142) to load data from csv. but i'm not sure how to create an shipment object (line 179).



      <?php
      $file = fopen('track.csv', 'r', '"'); // set path to the CSV file
      if ($file !== false)

      require __DIR__ . '/app/bootstrap.php';
      $bootstrap = MagentoFrameworkAppBootstrap::create(BP, $_SERVER);
      $objectManager = $bootstrap->getObjectManager();
      $state = $objectManager->get('MagentoFrameworkAppState');
      $state->setAreaCode('adminhtml');
      // add logging capability
      $writer = new ZendLogWriterStream(BP . '/var/log/import-update.log');
      $logger = new ZendLogLogger();
      $logger->addWriter($writer);


      class ShipmentObject
      /*There are two main functions
      prepareShipment of MagentoSalesModelOrderShipmentFactory class which prepare invoice and
      addObject of MagentoFrameworkDBTransactionFactory class which helps to create shipment and associate the shipment with original order in Magento 2.
      */

      /**
      * @var MagentoSalesModelOrderShipmentTrackFactory
      */
      protected $_shipmentTrackFactory;

      /**
      * The ShipmentFactory is used to create a new Shipment.
      *
      * @var MagentoSalesModelOrderShipmentFactory
      */
      protected $_shipmentFactory;

      /**
      * @var MagentoFrameworkDBTransactionFactory
      */
      protected $_transactionFactory;

      /**
      * @var MagentoSalesApiOrderRepositoryInterface
      */
      protected $_orderRepository;

      protected $_file;
      /**
      * @param MagentoSalesModelOrderShipmentTrackFactory $shipmentTrackFactory
      * @param MagentoSalesModelOrderShipmentFactory $shipmentFactory
      * @param MagentoFrameworkDBTransactionFactory $transactionFactory
      * @param MagentoSalesApiOrderRepositoryInterface $orderRepository
      */
      public function __construct(
      MagentoSalesModelOrderShipmentTrackFactory $shipmentTrackFactory,
      MagentoSalesModelOrderShipmentFactory $shipmentFactory,
      MagentoFrameworkDBTransactionFactory $transactionFactory,
      MagentoSalesApiOrderRepositoryInterface $orderRepository,
      MagentoSalesModelOrderFactory $orderFactory
      )
      $this->_shipmentTrackFactory = $shipmentTrackFactory;
      $this->_shipmentFactory = $shipmentFactory;
      $this->_transactionFactory = $transactionFactory;
      $this->_orderRepository = $orderRepository;
      $this->orderFactory = $orderFactory;

      /**
      * @param int $orderId
      * @param string $trackingNumber
      * @return MagentoSalesModelShipment $shipment
      */
      protected function createShipment($orderId, $trackingNumber)

      try
      //$order = $this->_orderRepository->get($orderId); // this is increment id
      $order = $this->orderFactory->create()->loadByIncrementId($orderId);

      if ($order)
      $data = array(array(
      'carrier_code' => 'fedex', //$order->getShippingMethod(), //'carrier_code' => 'fedex', // need to lower case
      'title' => 'Federal Express', //$order->getShippingDescription(), //'title' => 'Federal Express', // how to set here 'Federal Express' or 'United Parcel Service'
      'number' => $trackingNumber, //$trackingnumber,
      ));

      $shipment = $this->prepareShipment($order, $data);
      /*
      if ($shipment)
      $order->setIsInProcess(true);
      $order->addStatusHistoryComment('Automatically SHIPPED', false);
      $transactionSave = $this->_transactionFactory->create()->addObject($shipment)->addObject($shipment->getOrder());
      $transactionSave->save();

      */
      if ($shipment)
      $order->setIsInProcess(true);
      $order->addStatusHistoryComment('Automatically SHIPPED', false);
      $transactionSave = $this->_transactionFactory->create();
      $transactionSave->addObject($shipment);
      $transactionSave->addObject($shipment->getOrder());

      $transactionSave->save();


      //return $shipment; //original
      //return $shipment->save(); //magefms
      return $order; //helgeB

      catch (Exception $e)
      throw new MagentoFrameworkExceptionLocalizedException(
      __($e->getMessage())
      );



      /**
      * @param $order MagentoSalesModelOrder
      * @param $track array
      * @return $this
      */
      protected function prepareShipment($order, $track)

      $shipment = $this->_shipmentFactory->create(
      $order,
      $this->prepareShipmentItems($order),
      $track
      );
      return $shipment->getTotalQty() ? $shipment->register() : false;


      /**
      * @param $order MagentoSalesModelOrder
      * @return array
      */
      protected function prepareShipmentItems($order)

      $items = [];

      foreach($order->getAllItems() as $item)
      $items[$item->getItemId()] = $item->getQtyOrdered();

      return $items;

      //after prepareShipmentItems function
      /**
      * @param $file
      */
      public function setFile($file)

      $this->_file = $file;


      /**
      * @param null
      * @return ordersave
      */
      public function execute()

      // enter the number of data fields you require the product row inside the CSV file to contain

      $required_data_fields = 3; //number of column in csv
      //$header = fgetcsv($file); // get data headers and skip 1st row

      while (($row = fgetcsv($this->_file, 100, ",")) !== FALSE)
      //while (($row = fgetcsv($file, 100, ",")) !== FALSE)
      //while ( $row = fgetcsv($file, 100, ",") )
      //while (($row = fgetcsv($file)) !== FALSE)

      $data_count = count($row);
      if ($data_count < 1)
      continue;


      $data = array();
      //$data = array_combine($header, $row);
      $data = array($row);

      $ponumber = $row[0]; //$data['ponumber']; // column A
      if ($data_count < $required_data_fields)
      $logger->info("Skipping Order Number " . $ponumber . ". Not enough data to import.");
      continue;

      //$shipvia = trim($data[1]); //trim($data['shipvia']); // column B
      //$trackingnumber = trim($data[2]);//trim($data['trackingnumber']); // column C
      $shipvia = isset($row[1]) ? $row[1] : null;
      $trackingnumber = isset($row[2]) ? $row[2] : null;

      switch ($shipvia)
      case "FEDEX":
      $shipvia = "fedex";
      break;
      case "UPS":
      $shipvia = "ups";
      break;
      case "USPS":
      $shipvia = "usps";
      break;
      default:
      $shipvia = "fedex";


      ///////////////////////////////start adding tracking///////////////////////////////
      echo 'Updating Order: '.$ponumber.', with '.$shipvia.':'.$trackingnumber."rn";
      //print_r($row);

      $order = $this->createShipment($ponumber, $trackingnumber); /* @param int $orderId@param // end of while loop

      //end of execute function

      // end of class shipmentobject

      /**
      * @param MagentoSalesModelOrderShipmentTrackFactory $shipmentTrackFactory
      * @param MagentoSalesModelOrderShipmentFactory $shipmentFactory
      * @param MagentoFrameworkDBTransactionFactory $transactionFactory
      * @param MagentoSalesApiOrderRepositoryInterface $orderRepository
      */
      $shipmentTrackFactory = $objectManager->get('MagentoSalesModelOrderShipmentTrackFactory');
      $shipmentFactory = $objectManager->get('MagentoSalesModelOrderShipmentFactory');
      $transactionFactory = $objectManager->get('MagentoFrameworkDBTransactionFactory');
      $orderRepository = $objectManager->get('MagentoSalesApiOrderRepositoryInterface');
      $orderFactory = $objectManager->get('MagentoSalesModelOrderFactory');

      //create new class object
      $test = new ShipmentObject($shipmentTrackFactory,$shipmentFactory,$transactionFactory,$orderRepository,$orderFactory);
      $test->setFile($file);
      $test->execute();
      fclose($file);
      // if file not exist, then do nothing


      new error
      tracking information does not copy to (shipment tab) "sales_shipment_grid"



      it is showing like this in admin panel, the shipment tab is empty, though the tracking is in there with no carrier assign.



      enter image description hereenter image description hereenter image description here







      magento2 php shipment object-manager shipment-tracking






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 6 hours ago







      Kris Wen

















      asked Apr 5 at 23:16









      Kris WenKris Wen

      1418




      1418




















          2 Answers
          2






          active

          oldest

          votes


















          1














          You will have to get the objects you need in the constructor of your ShipmentObject using the object manager and pass them as parameter.



          Your code should look like this from line 175:



          $shipmentTrackFactory = $objectManager->get('MagentoSalesModelOrderShipmentTrackFactory');
          $shipmentFactory = $objectManager->get('MagentoSalesModelOrderShipmentFactory');
          $transactionFactory = $objectManager->get('MagentoFrameworkDBTransactionFactory');
          $orderRepository = $objectManager->get('MagentoSalesApiOrderRepositoryInterface');


          $test = new ShipmentObject($shipmentTrackFactory,$shipmentFactory,$transactionFactory,$orderRepository);


          But you will also have to pass the variable $file to the object in order to access it in the execute method. You can either extend the constructor and add a 5th parameter or implement a setter and call it like this:



          protected $_file;
          [...]
          public function setFile($file)

          $this->_file = $file;

          [...]
          public function execute()

          [...]
          while (($row = fgetcsv($this->_file, 100, ",")) !== FALSE)
          [...]

          [...]
          $test = new ShipmentObject($shipmentTrackFactory,$shipmentFactory,$transactionFactory,$orderRepository);
          $test->setFile($file);
          $test->execute();


          Update to your comment:



          Change your method createShipment to return the order object if an order is found or false if not:



          protected function createShipment($orderId, $trackingNumber)

          [...]
          if ($order)
          [...]
          return $order;

          return false;
          [...]



          Change your execute method to save the order only if an order object is returned and remove return inside the execute method:



          public function execute()

          [...]
          while (($row = fgetcsv($this->_file, 100, ",")) !== FALSE)
          @param string $trackingNumber */

          if ($order)
          $order->save();

          // end of while loop

          //end of execute function





          share|improve this answer

























          • Hi,thanks i'm still trying out the solution, but for now, the tracking number display in scientific notation like this " Array ( [0] => 100007331 [1] => FEDEX [2] => 4.90903E+11 )" how can i fix it?

            – Kris Wen
            yesterday











          • The good part: Your code works, I'm sure the data comes that way from your input file. It looks like the original file was an Excel file with that kind of column formatting. Change the column format in the Excel file to text and check the data before you save it as csv.

            – HelgeB
            yesterday












          • it was mistake on my part, opened it using excel and re-saved it causing that error. it print the file correctly now, but it's not saving to magento order. can you help me check my execute function? i posted my updated code. Thank you.

            – Kris Wen
            yesterday











          • i believe i call the correct function "createShipment" with the correct paremeters ($orderid & $trackingnumber) within the execute function, since the "createShipment" function will call "prepareShipment" then "prepareShipmentItems"

            – Kris Wen
            yesterday











          • Your createShipmentmethod must return the order object, because you call in your execute method $order->save() on the return of createShipment. And please remove the return keyword in your while loop in the execute method. I update also my answer with the changes in the two methods.

            – HelgeB
            15 hours ago


















          0














          With the provided error, you can try to change this line



          if ($shipment) 
          $order->setIsInProcess(true);
          $order->addStatusHistoryComment('Automatically SHIPPED', false);
          $transactionSave = $this->_transactionFactory->create();
          $transactionSave->addObject($shipment);
          $transactionSave->addObject($shipment->getOrder());

          $transactionSave->save();



          You should save objects in a separate line.






          share|improve this answer


















          • 1





            i have update the function with the lines you provided, but still getting error. can you see my updated codes error? i'm not sure if i use this function correctly. basically what i want to achieve is load the tracking number from csv file (#order,carrier,tracking) into magento2 order.

            – Kris Wen
            yesterday












          • when you say load, you mean save tracking number from csv to magento sales order?

            – magefms
            yesterday






          • 1





            yes, save from csv to magento sales order. In the csv file i have 3 columns ( ordernumber, carrier, tracking), i want to save them to magento sales order.

            – Kris Wen
            yesterday











          • in your execute function can you add a return $order->save();

            – magefms
            yesterday












          • added the line, still have the same error message.

            – Kris Wen
            yesterday











          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
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f268995%2fhow-do-i-create-a-shipment-class-object-in-magento-2%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          You will have to get the objects you need in the constructor of your ShipmentObject using the object manager and pass them as parameter.



          Your code should look like this from line 175:



          $shipmentTrackFactory = $objectManager->get('MagentoSalesModelOrderShipmentTrackFactory');
          $shipmentFactory = $objectManager->get('MagentoSalesModelOrderShipmentFactory');
          $transactionFactory = $objectManager->get('MagentoFrameworkDBTransactionFactory');
          $orderRepository = $objectManager->get('MagentoSalesApiOrderRepositoryInterface');


          $test = new ShipmentObject($shipmentTrackFactory,$shipmentFactory,$transactionFactory,$orderRepository);


          But you will also have to pass the variable $file to the object in order to access it in the execute method. You can either extend the constructor and add a 5th parameter or implement a setter and call it like this:



          protected $_file;
          [...]
          public function setFile($file)

          $this->_file = $file;

          [...]
          public function execute()

          [...]
          while (($row = fgetcsv($this->_file, 100, ",")) !== FALSE)
          [...]

          [...]
          $test = new ShipmentObject($shipmentTrackFactory,$shipmentFactory,$transactionFactory,$orderRepository);
          $test->setFile($file);
          $test->execute();


          Update to your comment:



          Change your method createShipment to return the order object if an order is found or false if not:



          protected function createShipment($orderId, $trackingNumber)

          [...]
          if ($order)
          [...]
          return $order;

          return false;
          [...]



          Change your execute method to save the order only if an order object is returned and remove return inside the execute method:



          public function execute()

          [...]
          while (($row = fgetcsv($this->_file, 100, ",")) !== FALSE)
          @param string $trackingNumber */

          if ($order)
          $order->save();

          // end of while loop

          //end of execute function





          share|improve this answer

























          • Hi,thanks i'm still trying out the solution, but for now, the tracking number display in scientific notation like this " Array ( [0] => 100007331 [1] => FEDEX [2] => 4.90903E+11 )" how can i fix it?

            – Kris Wen
            yesterday











          • The good part: Your code works, I'm sure the data comes that way from your input file. It looks like the original file was an Excel file with that kind of column formatting. Change the column format in the Excel file to text and check the data before you save it as csv.

            – HelgeB
            yesterday












          • it was mistake on my part, opened it using excel and re-saved it causing that error. it print the file correctly now, but it's not saving to magento order. can you help me check my execute function? i posted my updated code. Thank you.

            – Kris Wen
            yesterday











          • i believe i call the correct function "createShipment" with the correct paremeters ($orderid & $trackingnumber) within the execute function, since the "createShipment" function will call "prepareShipment" then "prepareShipmentItems"

            – Kris Wen
            yesterday











          • Your createShipmentmethod must return the order object, because you call in your execute method $order->save() on the return of createShipment. And please remove the return keyword in your while loop in the execute method. I update also my answer with the changes in the two methods.

            – HelgeB
            15 hours ago















          1














          You will have to get the objects you need in the constructor of your ShipmentObject using the object manager and pass them as parameter.



          Your code should look like this from line 175:



          $shipmentTrackFactory = $objectManager->get('MagentoSalesModelOrderShipmentTrackFactory');
          $shipmentFactory = $objectManager->get('MagentoSalesModelOrderShipmentFactory');
          $transactionFactory = $objectManager->get('MagentoFrameworkDBTransactionFactory');
          $orderRepository = $objectManager->get('MagentoSalesApiOrderRepositoryInterface');


          $test = new ShipmentObject($shipmentTrackFactory,$shipmentFactory,$transactionFactory,$orderRepository);


          But you will also have to pass the variable $file to the object in order to access it in the execute method. You can either extend the constructor and add a 5th parameter or implement a setter and call it like this:



          protected $_file;
          [...]
          public function setFile($file)

          $this->_file = $file;

          [...]
          public function execute()

          [...]
          while (($row = fgetcsv($this->_file, 100, ",")) !== FALSE)
          [...]

          [...]
          $test = new ShipmentObject($shipmentTrackFactory,$shipmentFactory,$transactionFactory,$orderRepository);
          $test->setFile($file);
          $test->execute();


          Update to your comment:



          Change your method createShipment to return the order object if an order is found or false if not:



          protected function createShipment($orderId, $trackingNumber)

          [...]
          if ($order)
          [...]
          return $order;

          return false;
          [...]



          Change your execute method to save the order only if an order object is returned and remove return inside the execute method:



          public function execute()

          [...]
          while (($row = fgetcsv($this->_file, 100, ",")) !== FALSE)
          @param string $trackingNumber */

          if ($order)
          $order->save();

          // end of while loop

          //end of execute function





          share|improve this answer

























          • Hi,thanks i'm still trying out the solution, but for now, the tracking number display in scientific notation like this " Array ( [0] => 100007331 [1] => FEDEX [2] => 4.90903E+11 )" how can i fix it?

            – Kris Wen
            yesterday











          • The good part: Your code works, I'm sure the data comes that way from your input file. It looks like the original file was an Excel file with that kind of column formatting. Change the column format in the Excel file to text and check the data before you save it as csv.

            – HelgeB
            yesterday












          • it was mistake on my part, opened it using excel and re-saved it causing that error. it print the file correctly now, but it's not saving to magento order. can you help me check my execute function? i posted my updated code. Thank you.

            – Kris Wen
            yesterday











          • i believe i call the correct function "createShipment" with the correct paremeters ($orderid & $trackingnumber) within the execute function, since the "createShipment" function will call "prepareShipment" then "prepareShipmentItems"

            – Kris Wen
            yesterday











          • Your createShipmentmethod must return the order object, because you call in your execute method $order->save() on the return of createShipment. And please remove the return keyword in your while loop in the execute method. I update also my answer with the changes in the two methods.

            – HelgeB
            15 hours ago













          1












          1








          1







          You will have to get the objects you need in the constructor of your ShipmentObject using the object manager and pass them as parameter.



          Your code should look like this from line 175:



          $shipmentTrackFactory = $objectManager->get('MagentoSalesModelOrderShipmentTrackFactory');
          $shipmentFactory = $objectManager->get('MagentoSalesModelOrderShipmentFactory');
          $transactionFactory = $objectManager->get('MagentoFrameworkDBTransactionFactory');
          $orderRepository = $objectManager->get('MagentoSalesApiOrderRepositoryInterface');


          $test = new ShipmentObject($shipmentTrackFactory,$shipmentFactory,$transactionFactory,$orderRepository);


          But you will also have to pass the variable $file to the object in order to access it in the execute method. You can either extend the constructor and add a 5th parameter or implement a setter and call it like this:



          protected $_file;
          [...]
          public function setFile($file)

          $this->_file = $file;

          [...]
          public function execute()

          [...]
          while (($row = fgetcsv($this->_file, 100, ",")) !== FALSE)
          [...]

          [...]
          $test = new ShipmentObject($shipmentTrackFactory,$shipmentFactory,$transactionFactory,$orderRepository);
          $test->setFile($file);
          $test->execute();


          Update to your comment:



          Change your method createShipment to return the order object if an order is found or false if not:



          protected function createShipment($orderId, $trackingNumber)

          [...]
          if ($order)
          [...]
          return $order;

          return false;
          [...]



          Change your execute method to save the order only if an order object is returned and remove return inside the execute method:



          public function execute()

          [...]
          while (($row = fgetcsv($this->_file, 100, ",")) !== FALSE)
          @param string $trackingNumber */

          if ($order)
          $order->save();

          // end of while loop

          //end of execute function





          share|improve this answer















          You will have to get the objects you need in the constructor of your ShipmentObject using the object manager and pass them as parameter.



          Your code should look like this from line 175:



          $shipmentTrackFactory = $objectManager->get('MagentoSalesModelOrderShipmentTrackFactory');
          $shipmentFactory = $objectManager->get('MagentoSalesModelOrderShipmentFactory');
          $transactionFactory = $objectManager->get('MagentoFrameworkDBTransactionFactory');
          $orderRepository = $objectManager->get('MagentoSalesApiOrderRepositoryInterface');


          $test = new ShipmentObject($shipmentTrackFactory,$shipmentFactory,$transactionFactory,$orderRepository);


          But you will also have to pass the variable $file to the object in order to access it in the execute method. You can either extend the constructor and add a 5th parameter or implement a setter and call it like this:



          protected $_file;
          [...]
          public function setFile($file)

          $this->_file = $file;

          [...]
          public function execute()

          [...]
          while (($row = fgetcsv($this->_file, 100, ",")) !== FALSE)
          [...]

          [...]
          $test = new ShipmentObject($shipmentTrackFactory,$shipmentFactory,$transactionFactory,$orderRepository);
          $test->setFile($file);
          $test->execute();


          Update to your comment:



          Change your method createShipment to return the order object if an order is found or false if not:



          protected function createShipment($orderId, $trackingNumber)

          [...]
          if ($order)
          [...]
          return $order;

          return false;
          [...]



          Change your execute method to save the order only if an order object is returned and remove return inside the execute method:



          public function execute()

          [...]
          while (($row = fgetcsv($this->_file, 100, ",")) !== FALSE)
          @param string $trackingNumber */

          if ($order)
          $order->save();

          // end of while loop

          //end of execute function






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 15 hours ago

























          answered Apr 6 at 10:22









          HelgeBHelgeB

          3,1831322




          3,1831322












          • Hi,thanks i'm still trying out the solution, but for now, the tracking number display in scientific notation like this " Array ( [0] => 100007331 [1] => FEDEX [2] => 4.90903E+11 )" how can i fix it?

            – Kris Wen
            yesterday











          • The good part: Your code works, I'm sure the data comes that way from your input file. It looks like the original file was an Excel file with that kind of column formatting. Change the column format in the Excel file to text and check the data before you save it as csv.

            – HelgeB
            yesterday












          • it was mistake on my part, opened it using excel and re-saved it causing that error. it print the file correctly now, but it's not saving to magento order. can you help me check my execute function? i posted my updated code. Thank you.

            – Kris Wen
            yesterday











          • i believe i call the correct function "createShipment" with the correct paremeters ($orderid & $trackingnumber) within the execute function, since the "createShipment" function will call "prepareShipment" then "prepareShipmentItems"

            – Kris Wen
            yesterday











          • Your createShipmentmethod must return the order object, because you call in your execute method $order->save() on the return of createShipment. And please remove the return keyword in your while loop in the execute method. I update also my answer with the changes in the two methods.

            – HelgeB
            15 hours ago

















          • Hi,thanks i'm still trying out the solution, but for now, the tracking number display in scientific notation like this " Array ( [0] => 100007331 [1] => FEDEX [2] => 4.90903E+11 )" how can i fix it?

            – Kris Wen
            yesterday











          • The good part: Your code works, I'm sure the data comes that way from your input file. It looks like the original file was an Excel file with that kind of column formatting. Change the column format in the Excel file to text and check the data before you save it as csv.

            – HelgeB
            yesterday












          • it was mistake on my part, opened it using excel and re-saved it causing that error. it print the file correctly now, but it's not saving to magento order. can you help me check my execute function? i posted my updated code. Thank you.

            – Kris Wen
            yesterday











          • i believe i call the correct function "createShipment" with the correct paremeters ($orderid & $trackingnumber) within the execute function, since the "createShipment" function will call "prepareShipment" then "prepareShipmentItems"

            – Kris Wen
            yesterday











          • Your createShipmentmethod must return the order object, because you call in your execute method $order->save() on the return of createShipment. And please remove the return keyword in your while loop in the execute method. I update also my answer with the changes in the two methods.

            – HelgeB
            15 hours ago
















          Hi,thanks i'm still trying out the solution, but for now, the tracking number display in scientific notation like this " Array ( [0] => 100007331 [1] => FEDEX [2] => 4.90903E+11 )" how can i fix it?

          – Kris Wen
          yesterday





          Hi,thanks i'm still trying out the solution, but for now, the tracking number display in scientific notation like this " Array ( [0] => 100007331 [1] => FEDEX [2] => 4.90903E+11 )" how can i fix it?

          – Kris Wen
          yesterday













          The good part: Your code works, I'm sure the data comes that way from your input file. It looks like the original file was an Excel file with that kind of column formatting. Change the column format in the Excel file to text and check the data before you save it as csv.

          – HelgeB
          yesterday






          The good part: Your code works, I'm sure the data comes that way from your input file. It looks like the original file was an Excel file with that kind of column formatting. Change the column format in the Excel file to text and check the data before you save it as csv.

          – HelgeB
          yesterday














          it was mistake on my part, opened it using excel and re-saved it causing that error. it print the file correctly now, but it's not saving to magento order. can you help me check my execute function? i posted my updated code. Thank you.

          – Kris Wen
          yesterday





          it was mistake on my part, opened it using excel and re-saved it causing that error. it print the file correctly now, but it's not saving to magento order. can you help me check my execute function? i posted my updated code. Thank you.

          – Kris Wen
          yesterday













          i believe i call the correct function "createShipment" with the correct paremeters ($orderid & $trackingnumber) within the execute function, since the "createShipment" function will call "prepareShipment" then "prepareShipmentItems"

          – Kris Wen
          yesterday





          i believe i call the correct function "createShipment" with the correct paremeters ($orderid & $trackingnumber) within the execute function, since the "createShipment" function will call "prepareShipment" then "prepareShipmentItems"

          – Kris Wen
          yesterday













          Your createShipmentmethod must return the order object, because you call in your execute method $order->save() on the return of createShipment. And please remove the return keyword in your while loop in the execute method. I update also my answer with the changes in the two methods.

          – HelgeB
          15 hours ago





          Your createShipmentmethod must return the order object, because you call in your execute method $order->save() on the return of createShipment. And please remove the return keyword in your while loop in the execute method. I update also my answer with the changes in the two methods.

          – HelgeB
          15 hours ago













          0














          With the provided error, you can try to change this line



          if ($shipment) 
          $order->setIsInProcess(true);
          $order->addStatusHistoryComment('Automatically SHIPPED', false);
          $transactionSave = $this->_transactionFactory->create();
          $transactionSave->addObject($shipment);
          $transactionSave->addObject($shipment->getOrder());

          $transactionSave->save();



          You should save objects in a separate line.






          share|improve this answer


















          • 1





            i have update the function with the lines you provided, but still getting error. can you see my updated codes error? i'm not sure if i use this function correctly. basically what i want to achieve is load the tracking number from csv file (#order,carrier,tracking) into magento2 order.

            – Kris Wen
            yesterday












          • when you say load, you mean save tracking number from csv to magento sales order?

            – magefms
            yesterday






          • 1





            yes, save from csv to magento sales order. In the csv file i have 3 columns ( ordernumber, carrier, tracking), i want to save them to magento sales order.

            – Kris Wen
            yesterday











          • in your execute function can you add a return $order->save();

            – magefms
            yesterday












          • added the line, still have the same error message.

            – Kris Wen
            yesterday















          0














          With the provided error, you can try to change this line



          if ($shipment) 
          $order->setIsInProcess(true);
          $order->addStatusHistoryComment('Automatically SHIPPED', false);
          $transactionSave = $this->_transactionFactory->create();
          $transactionSave->addObject($shipment);
          $transactionSave->addObject($shipment->getOrder());

          $transactionSave->save();



          You should save objects in a separate line.






          share|improve this answer


















          • 1





            i have update the function with the lines you provided, but still getting error. can you see my updated codes error? i'm not sure if i use this function correctly. basically what i want to achieve is load the tracking number from csv file (#order,carrier,tracking) into magento2 order.

            – Kris Wen
            yesterday












          • when you say load, you mean save tracking number from csv to magento sales order?

            – magefms
            yesterday






          • 1





            yes, save from csv to magento sales order. In the csv file i have 3 columns ( ordernumber, carrier, tracking), i want to save them to magento sales order.

            – Kris Wen
            yesterday











          • in your execute function can you add a return $order->save();

            – magefms
            yesterday












          • added the line, still have the same error message.

            – Kris Wen
            yesterday













          0












          0








          0







          With the provided error, you can try to change this line



          if ($shipment) 
          $order->setIsInProcess(true);
          $order->addStatusHistoryComment('Automatically SHIPPED', false);
          $transactionSave = $this->_transactionFactory->create();
          $transactionSave->addObject($shipment);
          $transactionSave->addObject($shipment->getOrder());

          $transactionSave->save();



          You should save objects in a separate line.






          share|improve this answer













          With the provided error, you can try to change this line



          if ($shipment) 
          $order->setIsInProcess(true);
          $order->addStatusHistoryComment('Automatically SHIPPED', false);
          $transactionSave = $this->_transactionFactory->create();
          $transactionSave->addObject($shipment);
          $transactionSave->addObject($shipment->getOrder());

          $transactionSave->save();



          You should save objects in a separate line.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered yesterday









          magefmsmagefms

          2,5862426




          2,5862426







          • 1





            i have update the function with the lines you provided, but still getting error. can you see my updated codes error? i'm not sure if i use this function correctly. basically what i want to achieve is load the tracking number from csv file (#order,carrier,tracking) into magento2 order.

            – Kris Wen
            yesterday












          • when you say load, you mean save tracking number from csv to magento sales order?

            – magefms
            yesterday






          • 1





            yes, save from csv to magento sales order. In the csv file i have 3 columns ( ordernumber, carrier, tracking), i want to save them to magento sales order.

            – Kris Wen
            yesterday











          • in your execute function can you add a return $order->save();

            – magefms
            yesterday












          • added the line, still have the same error message.

            – Kris Wen
            yesterday












          • 1





            i have update the function with the lines you provided, but still getting error. can you see my updated codes error? i'm not sure if i use this function correctly. basically what i want to achieve is load the tracking number from csv file (#order,carrier,tracking) into magento2 order.

            – Kris Wen
            yesterday












          • when you say load, you mean save tracking number from csv to magento sales order?

            – magefms
            yesterday






          • 1





            yes, save from csv to magento sales order. In the csv file i have 3 columns ( ordernumber, carrier, tracking), i want to save them to magento sales order.

            – Kris Wen
            yesterday











          • in your execute function can you add a return $order->save();

            – magefms
            yesterday












          • added the line, still have the same error message.

            – Kris Wen
            yesterday







          1




          1





          i have update the function with the lines you provided, but still getting error. can you see my updated codes error? i'm not sure if i use this function correctly. basically what i want to achieve is load the tracking number from csv file (#order,carrier,tracking) into magento2 order.

          – Kris Wen
          yesterday






          i have update the function with the lines you provided, but still getting error. can you see my updated codes error? i'm not sure if i use this function correctly. basically what i want to achieve is load the tracking number from csv file (#order,carrier,tracking) into magento2 order.

          – Kris Wen
          yesterday














          when you say load, you mean save tracking number from csv to magento sales order?

          – magefms
          yesterday





          when you say load, you mean save tracking number from csv to magento sales order?

          – magefms
          yesterday




          1




          1





          yes, save from csv to magento sales order. In the csv file i have 3 columns ( ordernumber, carrier, tracking), i want to save them to magento sales order.

          – Kris Wen
          yesterday





          yes, save from csv to magento sales order. In the csv file i have 3 columns ( ordernumber, carrier, tracking), i want to save them to magento sales order.

          – Kris Wen
          yesterday













          in your execute function can you add a return $order->save();

          – magefms
          yesterday






          in your execute function can you add a return $order->save();

          – magefms
          yesterday














          added the line, still have the same error message.

          – Kris Wen
          yesterday





          added the line, still have the same error message.

          – Kris Wen
          yesterday

















          draft saved

          draft discarded
















































          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%2f268995%2fhow-do-i-create-a-shipment-class-object-in-magento-2%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

          Sum ergo cogito? 1 nng

          三茅街道4182Guuntc Dn precexpngmageondP