I’m using Square checkout from a php script. I’ve set all my items and taxes but I can’t seem to figure out on how to add a shipping fee to the order. I’m using the php order class but looking at the model, I don’t see a method to add it.
$order = new Order($location_id);
$order->setLineItems($items);
$order->setTaxes($tax_items);
$shipping = new ShippingFee();
$shipping->setName("Shipping total");
$shipping->setCharge($money);
How do I add it?
If you’d like to add a shipping fee to your order using the Square PHP SDK, you will need to create a OrderLineItem
object for the shipping fee and then add it to the order’s line items. The OrderLineItem
class can be used to represent any charge in the order, including shipping fees.
Here’s how you can modify your existing code to include a shipping fee:
$order = new Order($location_id);
$order->setLineItems($items);
$order->setTaxes($tax_items);
// Create a Money object for the shipping fee amount
$shippingMoney = new Money();
$shippingMoney->setAmount($shippingAmount); // Replace $shippingAmount with the actual amount in the smallest unit of currency (e.g., cents for USD)
$shippingMoney->setCurrency('GBP'); // Replace 'GBP' with your currency code if different
// Create an OrderLineItem for the shipping fee
$shippingItem = new OrderLineItem(1); // The quantity for shipping is typically 1
$shippingItem->setName('Shipping Fee');
$shippingItem->setBasePriceMoney($shippingMoney);
// Add the shipping OrderLineItem to the order
$lineItems = $order->getLineItems();
$lineItems[] = $shippingItem;
$order->setLineItems($lineItems);
// Now you can proceed with creating the checkout using the updated order object
Make sure to replace $shippingAmount
with the actual amount you want to charge for shipping and adjust the currency code if necessary.
By adding the shipping fee as a line item, it will be included in the order total and processed along with the other items and taxes in the checkout flow.
That’s what I ended up doing. It just looks a bit strange to associate a quantity to shipping (1 X Shipping total). But at least it works!
Thanks!