DataStore
1 month ago
Providers
3 months ago
Fulfillment.php
1 month ago
FulfillmentException.php
3 months ago
FulfillmentOrderNotes.php
3 months ago
FulfillmentUtils.php
2 months ago
FulfillmentsController.php
3 months ago
FulfillmentsManager.php
2 months ago
FulfillmentsRenderer.php
1 month ago
FulfillmentsSettings.php
3 months ago
FulfillmentsTracker.php
3 months ago
OrderFulfillmentsRestController.php
1 month ago
ShippingProviders.php
3 months ago
FulfillmentsSettings.php
205 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Automattic\WooCommerce\Admin\Features\Fulfillments; |
| 6 | |
| 7 | use WC_Order; |
| 8 | |
| 9 | /** |
| 10 | * FulfillmentsSettings class. |
| 11 | */ |
| 12 | class FulfillmentsSettings { |
| 13 | |
| 14 | /** |
| 15 | * Registers the hooks related to fulfillments settings. |
| 16 | */ |
| 17 | public function register() { |
| 18 | add_filter( 'admin_init', array( $this, 'init_settings_auto_fulfill' ) ); |
| 19 | add_action( 'woocommerce_order_status_processing', array( $this, 'auto_fulfill_items_on_processing' ), 10, 2 ); |
| 20 | add_action( 'woocommerce_order_status_completed', array( $this, 'auto_fulfill_items_on_completed' ), 10, 2 ); |
| 21 | } |
| 22 | |
| 23 | /** |
| 24 | * Initialize settings for auto-fulfill options. |
| 25 | */ |
| 26 | public function init_settings_auto_fulfill() { |
| 27 | add_filter( 'woocommerce_get_settings_products', array( $this, 'add_auto_fulfill_settings' ), 10, 2 ); |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Add auto-fulfill settings to the WooCommerce settings. |
| 32 | * |
| 33 | * @param array $settings The existing settings. |
| 34 | * @param string|null $current_section The current section being viewed. |
| 35 | * |
| 36 | * @return array Modified settings with auto-fulfill options added. |
| 37 | */ |
| 38 | public function add_auto_fulfill_settings( array $settings, $current_section ): array { |
| 39 | if ( ! empty( $current_section ) ) { |
| 40 | return $settings; |
| 41 | } |
| 42 | |
| 43 | $insertion_index = null; |
| 44 | |
| 45 | // Find the index of the sectionend for 'Shop pages'. |
| 46 | foreach ( $settings as $index => $setting ) { |
| 47 | if ( |
| 48 | isset( $setting['type'], $setting['id'] ) && |
| 49 | 'sectionend' === $setting['type'] && |
| 50 | 'catalog_options' === $setting['id'] // Woo core's ID for Shop pages section. |
| 51 | ) { |
| 52 | $insertion_index = $index + 1; // Insert after the sectionend. |
| 53 | break; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | if ( is_null( $insertion_index ) ) { |
| 58 | return $settings; // fallback if not found. |
| 59 | } |
| 60 | |
| 61 | $auto_fulfill_settings = array( |
| 62 | array( |
| 63 | 'title' => 'Auto-fulfill items', |
| 64 | 'desc' => '', |
| 65 | 'type' => 'title', |
| 66 | 'id' => 'auto_fulfill_options', |
| 67 | ), |
| 68 | array( |
| 69 | 'title' => 'Virtual and downloadable items', |
| 70 | 'desc' => 'Automatically mark downloadable items as fulfilled when the order is created.', |
| 71 | 'id' => 'auto_fulfill_downloadable', |
| 72 | 'type' => 'checkbox', |
| 73 | 'checkboxgroup' => 'start', |
| 74 | 'default' => 'yes', |
| 75 | ), |
| 76 | array( |
| 77 | 'title' => 'Auto-fulfill items', |
| 78 | 'desc' => 'Automatically mark virtual (non-downloadable) items as fulfilled when the order is created.', |
| 79 | 'id' => 'auto_fulfill_virtual', |
| 80 | 'type' => 'checkbox', |
| 81 | 'checkboxgroup' => 'end', |
| 82 | 'default' => 'no', |
| 83 | ), |
| 84 | array( |
| 85 | 'type' => 'sectionend', |
| 86 | 'id' => 'auto_fulfill_options', |
| 87 | ), |
| 88 | ); |
| 89 | |
| 90 | array_splice( $settings, $insertion_index, 0, $auto_fulfill_settings ); |
| 91 | |
| 92 | return $settings; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Automatically fulfill items in the order on the processing state. |
| 97 | * |
| 98 | * @param int $order_id The ID of the order being created. |
| 99 | * @param WC_Order $order The order object. |
| 100 | */ |
| 101 | public function auto_fulfill_items_on_processing( int $order_id, $order ): void { |
| 102 | $order = $order instanceof WC_Order ? $order : wc_get_order( $order_id ); |
| 103 | |
| 104 | if ( ! $order || empty( $order->get_items() ) ) { |
| 105 | return; |
| 106 | } |
| 107 | $auto_fulfill_downloadable = 'yes' === get_option( 'auto_fulfill_downloadable', 'yes' ); |
| 108 | $auto_fulfill_virtual = 'yes' === get_option( 'auto_fulfill_virtual', 'no' ); |
| 109 | |
| 110 | /** |
| 111 | * Filter to get the list of the item, or variant ID's that should be auto-fulfilled. |
| 112 | * |
| 113 | * @since 10.1.0 |
| 114 | * |
| 115 | * @param array $auto_fulfill_items List of product or variant ID's to auto-fulfill. |
| 116 | * @param \WC_Order $order The order object. |
| 117 | * |
| 118 | * @return array Filtered list of product or variant ID's to auto-fulfill |
| 119 | */ |
| 120 | $auto_fulfill_product_ids = apply_filters( 'woocommerce_fulfillments_auto_fulfill_products', array(), $order ); |
| 121 | $auto_fulfill_items = array(); |
| 122 | |
| 123 | foreach ( $order->get_items() as $item ) { |
| 124 | /** |
| 125 | * Get the product associated with the item. |
| 126 | * |
| 127 | * @var \WC_Order_Item_Product $item |
| 128 | * @var \WC_Product $product |
| 129 | */ |
| 130 | $product = $item->get_product(); |
| 131 | if ( ! $product ) { |
| 132 | continue; |
| 133 | } |
| 134 | |
| 135 | if ( ( $product->is_downloadable() && $auto_fulfill_downloadable ) |
| 136 | || ( $product->is_virtual() && $auto_fulfill_virtual ) |
| 137 | || in_array( $product->get_id(), $auto_fulfill_product_ids, true ) ) { |
| 138 | $auto_fulfill_items[] = $item; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | if ( ! empty( $auto_fulfill_items ) ) { |
| 143 | $fulfillment = new Fulfillment(); |
| 144 | $fulfillment->set_entity_type( WC_Order::class ); |
| 145 | $fulfillment->set_entity_id( (string) $order_id ); |
| 146 | $fulfillment->set_status( 'fulfilled' ); |
| 147 | $fulfillment->set_items( |
| 148 | array_map( |
| 149 | function ( $item ) { |
| 150 | return array( |
| 151 | 'item_id' => $item->get_id(), |
| 152 | 'qty' => $item->get_quantity(), |
| 153 | ); |
| 154 | }, |
| 155 | $auto_fulfill_items |
| 156 | ) |
| 157 | ); |
| 158 | $fulfillment->save(); |
| 159 | } |
| 160 | |
| 161 | $order->update_meta_data( '_auto_fulfill_processed', true ); |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Automatically fulfill items in the order for orders that skip the processing state. |
| 166 | * |
| 167 | * @param int $order_id The ID of the order being created. |
| 168 | * @param WC_Order $order The order object. |
| 169 | */ |
| 170 | public function auto_fulfill_items_on_completed( int $order_id, $order ): void { |
| 171 | $order = $order instanceof WC_Order ? $order : wc_get_order( $order_id ); |
| 172 | if ( ! $order || empty( $order->get_items() ) ) { |
| 173 | return; |
| 174 | } |
| 175 | |
| 176 | // If auto-fulfill already processed, skip. |
| 177 | if ( $order->get_meta( '_auto_fulfill_processed', true ) ) { |
| 178 | return; |
| 179 | } |
| 180 | |
| 181 | // If fulfillments already exist, skip auto-fulfillment. |
| 182 | try { |
| 183 | /** |
| 184 | * Fulfillments data store. |
| 185 | * |
| 186 | * @var \Automattic\WooCommerce\Admin\Features\Fulfillments\DataStore\FulfillmentsDataStore $fulfillment_data_store |
| 187 | */ |
| 188 | $fulfillment_data_store = \WC_Data_Store::load( 'order-fulfillment' ); |
| 189 | $fulfillments = $fulfillment_data_store->read_fulfillments( \WC_Order::class, (string) $order_id ); |
| 190 | if ( ! empty( $fulfillments ) ) { |
| 191 | return; |
| 192 | } |
| 193 | } catch ( \Throwable $e ) { |
| 194 | wc_get_logger()->error( |
| 195 | sprintf( 'Failed to load fulfillments for order %d: %s', $order_id, $e->getMessage() ), |
| 196 | array( 'source' => 'fulfillments' ) |
| 197 | ); |
| 198 | return; |
| 199 | } |
| 200 | |
| 201 | // Auto-fulfill items. |
| 202 | $this->auto_fulfill_items_on_processing( $order_id, $order ); |
| 203 | } |
| 204 | } |
| 205 |