4.30 how to get shipments in order from plugin

2 months ago
Hi
I´m trying to get shipments in order but it returns null eventhough I have a shipment.
Example: I have an order 6903 with shipmentID 485

I try following:
var shipments = _shipmentService.GetShipmentsByOrderId(order.id);
It returns null

Also tried this:

var sm = _shipmentService.GetShipmentById(485);

It also returns null

How to do this? :-)
Br
Tommy
2 months ago
Problem solved that was my mistake :-)

New problem, I try to insert shipment but get this error:

The INSERT statement conflicted with the FOREIGN KEY constraint "FK_ShipmentItem_Shipment_ShipmentId". The conflict occurred in database "databse_db_production", table "dbo.Shipment", column 'Id'.
The statement has been terminated.

Any ideas?
2 months ago
It means you are inserting a ShipmentItem whose ShipmentId column value does not reference an existing Id in the Shipment table.

Be sure to 'save changes' on any Shipment you insert, before trying to insert Shipment Items.
2 months ago
Thank you,  I copied code from shipstation plugin and wonder if that fails to:
try
            {
                var order = _orderService.GetOrderByGuid(Guid.Parse(orderNumber));

                if (order == null)
                    return;

                var shipments = _shipmentService.GetShipmentsByOrderId(order.Id);

                if (!shipments.Any())
                {
                    var shipment = new Shipment
                    {
                        CreatedOnUtc = DateTime.UtcNow,
                        ShippedDateUtc = DateTime.UtcNow,
                        OrderId = order.Id,
                        TrackingNumber = trackingNumber
                    };

                    decimal totalWeight = 0;

                    foreach (var orderItem in _orderService.GetOrderItems(order.Id))
                    {
                        var product = _productService.GetProductById(orderItem.ProductId);
                        
                        //is shippable
                        if (!product.IsShipEnabled)
                            continue;

                        //ensure that this product can be shipped (have at least one item to ship)
                        var maxQtyToAdd = _orderService.GetTotalNumberOfItemsCanBeAddedToShipment(orderItem);
                        if (maxQtyToAdd <= 0)
                            continue;

                        var warehouseId = product.WarehouseId;

                        //ok. we have at least one item. let's create a shipment (if it does not exist)

                        var orderItemTotalWeight = orderItem.ItemWeight * orderItem.Quantity;
                        if (orderItemTotalWeight.HasValue)
                            totalWeight += orderItemTotalWeight.Value;

                        //create a shipment item
                        var shipmentItem = new ShipmentItem
                        {
                            OrderItemId = orderItem.Id,
                            Quantity = orderItem.Quantity,
                            WarehouseId = warehouseId
                        };

                        _shipmentService.InsertShipmentItem(shipmentItem);
                    }

                    shipment.TotalWeight = totalWeight;

                    _shipmentService.InsertShipment(shipment);
                }
                else
                {
                    var shipment = shipments.FirstOrDefault();

                    if (shipment == null)
                        return;

                    shipment.TrackingNumber = trackingNumber;

                    _shipmentService.UpdateShipment(shipment);
                }

                order.ShippingStatus = ShippingStatus.Shipped;
                order.ShippingMethod = string.IsNullOrEmpty(service) ? carrier : service;

                _orderService.UpdateOrder(order);
            }
            catch (Exception e)
            {
                _logger.Error(e.Message, e);
            }
2 months ago
Yes it does have the same problem - Here is a quick fix

/// <summary>
/// Create or upadete shipping
/// </summary>
/// <param name="orderNumber"></param>
/// <param name="carrier"></param>
/// <param name="service"></param>
/// <param name="trackingNumber"></param>
public void CreateOrUpadeteShipping(string orderNumber, string carrier, string service, string trackingNumber)
{
    try
    {
        var order = _orderService.GetOrderByGuid(Guid.Parse(orderNumber));

        if (order == null)
            return;

        var shipments = _shipmentService.GetShipmentsByOrderId(order.Id);

        if (!shipments.Any())
        {
            var shipment = new Shipment
            {
                CreatedOnUtc = DateTime.UtcNow,
                ShippedDateUtc = DateTime.UtcNow,
                OrderId = order.Id,
                TrackingNumber = trackingNumber
            };

            _shipmentService.InsertShipment(shipment);

            var newShipment = _shipmentService.GetShipmentsByOrderId(order.Id).FirstOrDefault();

            decimal totalWeight = 0;

            foreach (var orderItem in _orderService.GetOrderItems(order.Id))
            {
                var product = _productService.GetProductById(orderItem.ProductId);

                //is shippable
                if (!product.IsShipEnabled)
                    continue;

                //ensure that this product can be shipped (have at least one item to ship)
                var maxQtyToAdd = _orderService.GetTotalNumberOfItemsCanBeAddedToShipment(orderItem);
                if (maxQtyToAdd <= 0)
                    continue;

                var warehouseId = product.WarehouseId;

                //ok. we have at least one item. let's create a shipment (if it does not exist)

                var orderItemTotalWeight = orderItem.ItemWeight * orderItem.Quantity;
                if (orderItemTotalWeight.HasValue)
                    totalWeight += orderItemTotalWeight.Value;

                //create a shipment item
                var shipmentItem = new ShipmentItem
                {
                    OrderItemId = orderItem.Id,
                    Quantity = orderItem.Quantity,
                    WarehouseId = warehouseId,
                    ShipmentId = newShipment.Id
                };

                _shipmentService.InsertShipmentItem(shipmentItem);
            }

            newShipment.TotalWeight += totalWeight;

            _shipmentService.UpdateShipment(newShipment);
        }
        else
        {
            var shipment = shipments.FirstOrDefault();

            if (shipment == null)
                return;

            shipment.TrackingNumber = trackingNumber;

            _shipmentService.UpdateShipment(shipment);
        }

        order.ShippingStatus = ShippingStatus.Shipped;
        order.ShippingMethod = string.IsNullOrEmpty(service) ? carrier : service;

        _orderService.UpdateOrder(order);
    }
    catch (Exception e)
    {
        _logger.Error(e.Message, e);
    }
}
2 months ago
Great thank you I will try it out :-)
2 months ago
It worked well, thank you :-)

Yidna wrote:
Yes it does have the same problem - Here is a quick fix

/// <summary>
/// Create or upadete shipping
/// </summary>
/// <param name="orderNumber"></param>
/// <param name="carrier"></param>
/// <param name="service"></param>
/// <param name="trackingNumber"></param>
public void CreateOrUpadeteShipping(string orderNumber, string carrier, string service, string trackingNumber)
{
    try
    {
        var order = _orderService.GetOrderByGuid(Guid.Parse(orderNumber));

        if (order == null)
            return;

        var shipments = _shipmentService.GetShipmentsByOrderId(order.Id);

        if (!shipments.Any())
        {
            var shipment = new Shipment
            {
                CreatedOnUtc = DateTime.UtcNow,
                ShippedDateUtc = DateTime.UtcNow,
                OrderId = order.Id,
                TrackingNumber = trackingNumber
            };

            _shipmentService.InsertShipment(shipment);

            var newShipment = _shipmentService.GetShipmentsByOrderId(order.Id).FirstOrDefault();

            decimal totalWeight = 0;

            foreach (var orderItem in _orderService.GetOrderItems(order.Id))
            {
                var product = _productService.GetProductById(orderItem.ProductId);

                //is shippable
                if (!product.IsShipEnabled)
                    continue;

                //ensure that this product can be shipped (have at least one item to ship)
                var maxQtyToAdd = _orderService.GetTotalNumberOfItemsCanBeAddedToShipment(orderItem);
                if (maxQtyToAdd <= 0)
                    continue;

                var warehouseId = product.WarehouseId;

                //ok. we have at least one item. let's create a shipment (if it does not exist)

                var orderItemTotalWeight = orderItem.ItemWeight * orderItem.Quantity;
                if (orderItemTotalWeight.HasValue)
                    totalWeight += orderItemTotalWeight.Value;

                //create a shipment item
                var shipmentItem = new ShipmentItem
                {
                    OrderItemId = orderItem.Id,
                    Quantity = orderItem.Quantity,
                    WarehouseId = warehouseId,
                    ShipmentId = newShipment.Id
                };

                _shipmentService.InsertShipmentItem(shipmentItem);
            }

            newShipment.TotalWeight += totalWeight;

            _shipmentService.UpdateShipment(newShipment);
        }
        else
        {
            var shipment = shipments.FirstOrDefault();

            if (shipment == null)
                return;

            shipment.TrackingNumber = trackingNumber;

            _shipmentService.UpdateShipment(shipment);
        }

        order.ShippingStatus = ShippingStatus.Shipped;
        order.ShippingMethod = string.IsNullOrEmpty(service) ? carrier : service;

        _orderService.UpdateOrder(order);
    }
    catch (Exception e)
    {
        _logger.Error(e.Message, e);
    }
}