Custom shipping methods are powerful in Commerce but because of that also a little complicated the first time.
You need to start building your own module (extension). https://docs.modmore.com/en/Commerce/v1/Developer/Guides/Bootstrapping_a_Module.html
Once you have that, you can extend the comShippingMethod class through the xPDO model in the extension to create your custom shipping method. https://docs.modmore.com/en/Commerce/v1/Developer/Custom_Shipping_Methods.html
Once you have that, you can take your example code and put it in the getPriceForShipment method. I see some minor issues you’d need to fix:
- It’s
$shipment->getItems(), or$order = $shipment->getOrder()and then$order->getItems()to get the items. - It’s
$product->getTarget(), not$product->getResource(). - Careful using strict type matching for TV values. Those typically come through as strings, so I suspect your $profit match would not work.