| 1 |
<?php |
| 2 |
/** |
| 3 |
* Order Delivery Method Builder Interface |
| 4 |
* |
| 5 |
* @package SeQura/WC |
| 6 |
* @subpackage SeQura/WC/Services |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace SeQura\WC\Services\Order\Builder; |
| 10 |
|
| 11 |
use SeQura\Core\BusinessLogic\Domain\Order\Models\OrderRequest\DeliveryMethod; |
| 12 |
use WC_Order; |
| 13 |
|
| 14 |
/** |
| 15 |
* Order Delivery Method Builder Interface |
| 16 |
*/ |
| 17 |
class Order_Delivery_Method_Builder implements Interface_Order_Delivery_Method_Builder { |
| 18 |
|
| 19 |
/** |
| 20 |
* Get Delivery Method |
| 21 |
*/ |
| 22 |
public function build( ?WC_Order $order ): DeliveryMethod { |
| 23 |
|
| 24 |
if ( ! $order ) { |
| 25 |
$session = WC()->session; |
| 26 |
$shipping_methods = $session ? WC()->session->chosen_shipping_methods : array(); |
| 27 |
|
| 28 |
if ( ! $shipping_methods || empty( WC()->shipping->get_packages() ) ) { |
| 29 |
return new DeliveryMethod( 'default', null, 'default' ); |
| 30 |
} |
| 31 |
$package = current( WC()->shipping->get_packages() ); |
| 32 |
$shipping_method = current( $shipping_methods ); |
| 33 |
|
| 34 |
if ( ! isset( $package['rates'][ $shipping_method ] ) ) { |
| 35 |
return new DeliveryMethod( 'default', null, 'default' ); |
| 36 |
} |
| 37 |
|
| 38 |
$rate = $package['rates'][ $shipping_method ]; |
| 39 |
return new DeliveryMethod( $rate->label, null, $rate->id ); |
| 40 |
} |
| 41 |
|
| 42 |
$shipping_method = current( $order->get_shipping_methods() ); |
| 43 |
|
| 44 |
return new DeliveryMethod( |
| 45 |
$shipping_method['name'] ?? 'default', |
| 46 |
null, |
| 47 |
$shipping_method['method_id'] ?? 'default' |
| 48 |
); |
| 49 |
} |
| 50 |
} |
| 51 |
|