BulkAction
1 week ago
views
1 week ago
AdminNotices.php
1 week ago
BulkAction.php
1 week ago
DispatchLabelFile.php
1 week ago
FilterOrders.php
1 week ago
ModifyOrderTable.php
1 week ago
ModifyStatuses.php
1 week ago
SubscriptionsIntegration.php
1 week ago
SubscriptionsIntegration.php
99 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Class SubscriptionsIntegration |
| 4 | * |
| 5 | * @package WPDesk\FS\Shipment |
| 6 | */ |
| 7 | |
| 8 | namespace WPDesk\FS\Shipment; |
| 9 | |
| 10 | use FSVendor\WPDesk\FS\Shipment\CustomPostType; |
| 11 | use FSVendor\WPDesk\PluginBuilder\Plugin\Hookable; |
| 12 | |
| 13 | /** |
| 14 | * Integrates Shipments with WooCommerce Subscriptions plugin. |
| 15 | */ |
| 16 | class SubscriptionsIntegration implements Hookable { |
| 17 | |
| 18 | /** |
| 19 | * @var CustomPostType |
| 20 | */ |
| 21 | private $shipment_cpt; |
| 22 | |
| 23 | /** |
| 24 | * SubscriptionsIntegration constructor. |
| 25 | * |
| 26 | * @param CustomPostType $shipment_cpt . |
| 27 | */ |
| 28 | public function __construct( CustomPostType $shipment_cpt ) { |
| 29 | $this->shipment_cpt = $shipment_cpt; |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * . |
| 34 | */ |
| 35 | public function hooks() { |
| 36 | $last_priority = PHP_INT_MAX; |
| 37 | add_action( 'woocommerce_checkout_subscription_created', [ $this, 'create_shipping_for_subscription' ], $last_priority, 3 ); |
| 38 | add_filter( 'wcs_renewal_order_created', [ $this, 'create_shipping_for_order_from_subscription' ], 10, 2 ); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * @param \WC_Subscription $subscription . |
| 43 | * @param \WC_Order $order . |
| 44 | * @param \WC_Cart $recurring_cart . |
| 45 | */ |
| 46 | public function create_shipping_for_subscription( $subscription, $order, $recurring_cart ) { |
| 47 | $this->shipment_cpt->create_shipping_for_order_and_cart( $subscription, $recurring_cart ); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * @param \WC_Order $order . |
| 52 | * @param \WC_Subscription $subscription . |
| 53 | */ |
| 54 | public function create_shipping_for_order_from_subscription( $order, $subscription ) { |
| 55 | $subscription_shipments = fs_get_order_shipments( $subscription->get_id() ); |
| 56 | foreach ( $subscription_shipments as $shipment ) { |
| 57 | $this->create_single_shipment( $shipment, $order ); |
| 58 | } |
| 59 | |
| 60 | return $order; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * @param \WPDesk_Flexible_Shipping_Shipment $shipment . |
| 65 | * @param \WC_Order $order . |
| 66 | */ |
| 67 | private function create_single_shipment( \WPDesk_Flexible_Shipping_Shipment $shipment, \WC_Order $order ) { |
| 68 | $meta_data = $shipment->get_meta_data(); |
| 69 | $fs_method = $shipment->get_meta( '_fs_method', [ 'method_integration' => $shipment->get_integration() ] ); |
| 70 | $order_shipment = fs_create_shipment( $order, $fs_method ); |
| 71 | $integration = $order_shipment->get_integration(); |
| 72 | $this->setup_shipment_meta_data( $meta_data, $integration, $shipment, $order_shipment ); |
| 73 | $order_shipment->save(); |
| 74 | |
| 75 | /** |
| 76 | * New shipment created from subscription. |
| 77 | * |
| 78 | * @param \WPDesk_Flexible_Shipping_Shipment $order_shipment Created shipment. |
| 79 | */ |
| 80 | do_action( 'flexible-shipping/shipment-from-subscription/created/' . $integration, $order_shipment ); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * @param array $meta_data . |
| 85 | * @param string $integration . |
| 86 | * @param \WPDesk_Flexible_Shipping_Shipment $shipment . |
| 87 | * @param \WPDesk_Flexible_Shipping_Shipment $order_shipment . |
| 88 | */ |
| 89 | private function setup_shipment_meta_data( array $meta_data, $integration, \WPDesk_Flexible_Shipping_Shipment $shipment, \WPDesk_Flexible_Shipping_Shipment $order_shipment ) { |
| 90 | foreach ( $meta_data as $meta_key => $meta_value ) { |
| 91 | $order_shipment_meta_value = |
| 92 | apply_filters( 'flexible-shipping/shipment-from-subscription/meta-value/' . $integration, $shipment->get_meta( $meta_key ), $meta_key, $order_shipment ); |
| 93 | $order_shipment->set_meta( $meta_key, $order_shipment_meta_value ); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | |
| 98 | } |
| 99 |