HandleAction.php
1 week ago
HandleActionLabels.php
1 week ago
HandleActionManifest.php
1 week ago
HandleActionSend.php
1 week ago
HandleActionStrategy.php
1 week ago
HandleActionStrategyInterface.php
1 week ago
HandleActionSend.php
73 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Class HandleActionSend |
| 4 | */ |
| 5 | |
| 6 | namespace WPDesk\FS\Shipment\BulkAction; |
| 7 | |
| 8 | use Exception; |
| 9 | use FSVendor\WPDesk\Session\SessionFactory; |
| 10 | use WPDesk_Flexible_Shipping_Shipment; |
| 11 | use WPDesk_Flexible_Shipping_Shipment_Interface; |
| 12 | |
| 13 | /** |
| 14 | * . |
| 15 | */ |
| 16 | class HandleActionSend implements HandleActionStrategyInterface { |
| 17 | |
| 18 | /** |
| 19 | * @var SessionFactory |
| 20 | */ |
| 21 | private $session_factory; |
| 22 | |
| 23 | /** |
| 24 | * @param SessionFactory $session_factory . |
| 25 | */ |
| 26 | public function __construct( SessionFactory $session_factory ) { |
| 27 | $this->session_factory = $session_factory; |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * @param string $redirect_to . |
| 32 | * @param array $post_ids . |
| 33 | * |
| 34 | * @return string |
| 35 | */ |
| 36 | public function handle( string $redirect_to, array $post_ids ): string { |
| 37 | $messages = []; |
| 38 | foreach ( $post_ids as $post_id ) { |
| 39 | $shipments = fs_get_order_shipments( $post_id ); |
| 40 | $messages[ $post_id ] = []; |
| 41 | |
| 42 | foreach ( $shipments as $shipment ) { |
| 43 | /* @var $shipment WPDesk_Flexible_Shipping_Shipment|WPDesk_Flexible_Shipping_Shipment_Interface */ |
| 44 | try { |
| 45 | $shipment->set_sent_via_bulk(); |
| 46 | $shipment->api_create(); |
| 47 | $messages[ $post_id ][ $shipment->get_id() ] = [ |
| 48 | 'status' => 'created', |
| 49 | 'message' => __( 'Shipment created.', 'flexible-shipping' ), |
| 50 | ]; |
| 51 | } catch ( Exception $e ) { |
| 52 | $messages[ $post_id ][ $shipment->get_id() ] = [ |
| 53 | 'status' => 'error', |
| 54 | 'message' => $e->getMessage(), |
| 55 | ]; |
| 56 | } |
| 57 | } |
| 58 | $messages[ $post_id ][] = apply_filters( |
| 59 | 'flexible_shipping_bulk_send', |
| 60 | [ |
| 61 | 'status' => 'none', |
| 62 | 'message' => __( 'No action performed.', 'flexible-shipping' ), |
| 63 | ], |
| 64 | $post_id |
| 65 | ); |
| 66 | } |
| 67 | |
| 68 | $this->session_factory->get_woocommerce_session_adapter()->set( 'flexible_shipping_bulk_send', $messages ); |
| 69 | |
| 70 | return add_query_arg( 'bulk_flexible_shipping_send', count( $post_ids ), $redirect_to ); |
| 71 | } |
| 72 | } |
| 73 |