Skip to content

Instantly share code, notes, and snippets.

@jsacksick
Created October 20, 2020 09:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsacksick/fde031197df92236ef5a916775e17a2e to your computer and use it in GitHub Desktop.
Save jsacksick/fde031197df92236ef5a916775e17a2e to your computer and use it in GitHub Desktop.
<?php declare(strict_types=1);
namespace Drupal\my_module\OrderProcessor;
use Drupal\commerce_order\Entity\OrderInterface;
use Drupal\commerce_order\OrderProcessorInterface;
use Drupal\commerce_shipping\ShipmentManagerInterface;
use Drupal\commerce_shipping\ShippingOrderManagerInterface;
use Drupal\profile\Entity\Profile;
/**
* Ensures there are shipments from the start.
*/
final class ShippingProcessor implements OrderProcessorInterface {
/**
* The shipping order manager.
*
* @var \Drupal\commerce_shipping\ShippingOrderManagerInterface
*/
protected $shippingOrderManager;
/**
* The shipment manager.
*
* @var \Drupal\commerce_shipping\ShipmentManagerInterface
*/
protected $shipmentManager;
public function __construct(ShippingOrderManagerInterface $shippingOrderManager, ShipmentManagerInterface $shipmentManager) {
$this->shippingOrderManager = $shippingOrderManager;
$this->shipmentManager = $shipmentManager;
}
public function process(OrderInterface $order) {
if ($order->isNew() || $this->shippingOrderManager->hasShipments($order)) {
return;
}
$collected_profiles = $order->collectProfiles();
$shipping_profile = $collected_profiles['shipping'] ?? NULL;
// @todo remove and force specifying a zip code.
if ($shipping_profile === NULL) {
$shipping_profile = Profile::create([
'type' => 'customer',
'uid' => 0,
]);
}
$shipments = $this->shippingOrderManager->pack($order, $shipping_profile);
foreach ($shipments as $shipment) {
$rates = $this->shipmentManager->calculateRates($shipment);
if (count($rates) > 0 ) {
$rate = $this->shipmentManager->selectDefaultRate($shipment, $rates);
$this->shipmentManager->applyRate($shipment, $rate);
}
$shipment->save();
}
$order->set('shipments', $shipments);
}
}
@GolubovicM
Copy link

GolubovicM commented Oct 23, 2020

After adding this I had an issue that shipping costs were appearing with delay, after I add second item to cart, not after first one. Problem was low priority (it was -300 at fist try) I set in services file. After changing priority to 300 it started working well:

services:
  my_module.shipping_order_processor:
    class: Drupal\my_module\ShippingProcessor
    arguments: ['@commerce_shipping.order_manager', '@commerce_shipping.shipment_manager']
    tags:
      - { name: commerce_order.order_processor, priority: 300 }

Also I named file just "ShippingProcessor.php" to match class name. Not mandatory, but easier to avoid confusion.

@GolubovicM
Copy link

BTW, if I understood well this code is creating an empty customer profile in order to create shipments . Problem is that later, on order overview page, where user is setting shipping address that empty profile (address) is also offered. Drop-down for selecting shipping address looks like:

<select class="available-profiles ..." name="shipping_information[shipping_profile][select_address]">
    <option value="183">Some street 233</option>
    <option value="_original" selected="selected">Customer information #498</option>
    <option value="_new">+ Enter a new address</option>
</select>

and that "Customer information #498" option is selected by default and all address fields are empty for it (similar as for entering new address". Can this be removed somehow?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment