From 5f6dcf3fb71ba6c34d65f76031be6378848a905b Mon Sep 17 00:00:00 2001 From: Malay Ladu Date: Fri, 14 Feb 2025 21:11:40 +0530 Subject: [PATCH] `gspc-update-post-status-on-wc-order-status-change.php`: Added snippet to update the post status of the post created by Gravity Forms Advanced Post Creation when the WooCommerce order status is changed. --- ...-post-status-on-wc-order-status-change.php | 105 ++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 gs-product-configurator/gspc-update-post-status-on-wc-order-status-change.php diff --git a/gs-product-configurator/gspc-update-post-status-on-wc-order-status-change.php b/gs-product-configurator/gspc-update-post-status-on-wc-order-status-change.php new file mode 100644 index 000000000..ce8109c49 --- /dev/null +++ b/gs-product-configurator/gspc-update-post-status-on-wc-order-status-change.php @@ -0,0 +1,105 @@ +_args = wp_parse_args( $args, array( + 'form_id' => false, + 'post_status' => 'draft', + ) ); + + if ( ! empty( $this->_args['post_status'] ) ) { + // Change `woocommerce_order_status_cancelled` to the WooCommerce order status you want to trigger the post status update. + add_action( 'woocommerce_order_status_cancelled', array( $this, 'update_post_status' ) ); + } + + } + + /** + * Update the post status. + * + * @param int $subscription_id The subscription ID. + */ + function update_post_status( $subscription_id ) { + $order = wc_get_order( $subscription_id ); + + if ( ! is_a( $order, 'WC_Order' ) ) { + return; + } + + $order_items = $order->get_items(); + + foreach ( $order_items as $order_item ) { + $item = \GS_Product_Configurator\WC_Order_Item::from( $order_item ); + + $entry_ids = $item->get_entry_ids(); + + if ( empty( $entry_ids ) ) { + continue; + } + + foreach ( $entry_ids as $entry_id ) { + if ( ! GFAPI::entry_exists( $entry_id ) ) { + continue; + } + + if ( ! $this->is_applicable_form( $entry_id ) ) { + continue; + } + + $posts_data = maybe_unserialize( gform_get_meta( $entry_id, 'gravityformsadvancedpostcreation_post_id' ) ); + + if ( ! $posts_data ) { + continue; + } + + foreach ( $posts_data as $post_data ) { + $post_id = $post_data['post_id'] ?? null; + if ( $post_id ) { + wp_update_post( array( + 'ID' => $post_id, + 'post_status' => $this->_args['post_status'], + ) ); + } + } + } + } + } + + /** + * Check if the form is applicable. + * + * @param int $entry_id The entry ID. + * + * @return bool + */ + public function is_applicable_form( $entry_id ) { + if ( empty( $this->_args['form_id'] ) ) { + return true; + } + + $entry = GFAPI::get_entry( $entry_id ); + $form_id = rgar( $entry, 'form_id' ); + + return (int) $form_id === (int) $this->_args['form_id']; + } +} + +new GSPC_Update_Post_Status_On_WC_Order_Status_Change( array( + 'form_id' => 123, // Add `form_id` when you want to target a specific form. + 'post_status' => 'wc-expired', // Add the post status you want to update the post to. +) );