|
|
@@ -30,8 +30,10 @@ class Studiou_WC_FPP_Cart {
|
|
|
// Save file reference to order item
|
|
|
add_action('woocommerce_checkout_create_order_line_item', array($this, 'save_order_item_meta'), 10, 4);
|
|
|
|
|
|
- // Link file records to order after checkout
|
|
|
+ // Link file records to order after checkout (classic + block checkout)
|
|
|
add_action('woocommerce_checkout_order_processed', array($this, 'link_files_to_order'), 10, 3);
|
|
|
+ add_action('woocommerce_store_api_checkout_order_processed', array($this, 'link_files_to_order_from_store_api'), 10, 1);
|
|
|
+ add_action('woocommerce_thankyou', array($this, 'link_files_to_order_fallback'), 10, 1);
|
|
|
|
|
|
// Replace order item thumbnail with uploaded image (admin)
|
|
|
add_filter('woocommerce_admin_order_item_thumbnail', array($this, 'admin_order_item_thumbnail'), 10, 3);
|
|
|
@@ -183,10 +185,39 @@ class Studiou_WC_FPP_Cart {
|
|
|
}
|
|
|
|
|
|
public function link_files_to_order($order_id, $posted_data, $order) {
|
|
|
+ $this->do_link_files($order);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Block checkout fires this hook with only the $order parameter.
|
|
|
+ */
|
|
|
+ public function link_files_to_order_from_store_api($order) {
|
|
|
+ $this->do_link_files($order);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Fallback: runs on thank-you page to catch any missed linking.
|
|
|
+ */
|
|
|
+ public function link_files_to_order_fallback($order_id) {
|
|
|
+ $order = wc_get_order($order_id);
|
|
|
+ if ($order) {
|
|
|
+ $this->do_link_files($order);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private function do_link_files($order) {
|
|
|
+ if (!$order) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ $order_id = $order->get_id();
|
|
|
foreach ($order->get_items() as $item_id => $item) {
|
|
|
$file_record_id = $item->get_meta('_studiou_fpp_file_record_id');
|
|
|
if ($file_record_id) {
|
|
|
- $this->db->link_file_to_order((int) $file_record_id, $order_id, $item_id);
|
|
|
+ // Only link if not already linked
|
|
|
+ $record = $this->db->get_file_record((int) $file_record_id);
|
|
|
+ if ($record && (int) $record->order_id === 0) {
|
|
|
+ $this->db->link_file_to_order((int) $file_record_id, $order_id, $item_id);
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|