4.30 how to get shipments in order from plugin

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
3 年 前
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
3 年 前
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?
3 年 前
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.
3 年 前
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);
            }
3 年 前
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);
    }
}
3 年 前
Great thank you I will try it out :-)
3 年 前
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);
    }
}
2 年 前
For the life of me, I can't get this working. I uninstalled the plugin. Downloaded a fresh copy of the plugin. Made changes under the 4.3 version as above. Saved. Uploaded plugin with saved changes. Installed, connect back to shipstaion. Sent notification from shipstation. Same error in the log.
2 年 前
Did you try to restart the webserver to ensure new pluign is loaded ?
2 年 前
Yes Sir, just tried that. Did not work. Still getting error.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.