|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Gravity Forms Advanced Post Creation // Product Configurator |
| 4 | + * |
| 5 | + * This snippet will update the post status of the post created by Gravity Forms Advanced Post Creation when the WooCommerce order status is changed. |
| 6 | + * |
| 7 | + * Possible statuses are: publish, future, draft, pending, private, trash, wc-active, wc-switched, wc-expired, wc-pending-cancel, wc-pending, wc-processing, wc-on-hold, wc-completed, wc-cancelled, wc-refunded, wc-failed |
| 8 | + * |
| 9 | + * https://gravitywiz.com/documentation/gs-product-configurator/ |
| 10 | + * |
| 11 | + * Instructions: |
| 12 | + * 1. Install per https://gravitywiz.com/documentation/how-do-i-install-a-snippet/ |
| 13 | + * 2. Configure the snippet based on inline instructions. |
| 14 | + */ |
| 15 | + |
| 16 | +class GSPC_Update_Post_Status_On_WC_Order_Status_Change { |
| 17 | + private $_args; |
| 18 | + |
| 19 | + public function __construct( $args = array() ) { |
| 20 | + $this->_args = wp_parse_args( $args, array( |
| 21 | + 'form_id' => false, |
| 22 | + 'post_status' => 'draft', |
| 23 | + ) ); |
| 24 | + |
| 25 | + if ( ! empty( $this->_args['post_status'] ) ) { |
| 26 | + // Change `woocommerce_order_status_cancelled` to the WooCommerce order status you want to trigger the post status update. |
| 27 | + add_action( 'woocommerce_order_status_cancelled', array( $this, 'update_post_status' ) ); |
| 28 | + } |
| 29 | + |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * Update the post status. |
| 34 | + * |
| 35 | + * @param int $subscription_id The subscription ID. |
| 36 | + */ |
| 37 | + function update_post_status( $subscription_id ) { |
| 38 | + $order = wc_get_order( $subscription_id ); |
| 39 | + |
| 40 | + if ( ! is_a( $order, 'WC_Order' ) ) { |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + $order_items = $order->get_items(); |
| 45 | + |
| 46 | + foreach ( $order_items as $order_item ) { |
| 47 | + $item = \GS_Product_Configurator\WC_Order_Item::from( $order_item ); |
| 48 | + |
| 49 | + $entry_ids = $item->get_entry_ids(); |
| 50 | + |
| 51 | + if ( empty( $entry_ids ) ) { |
| 52 | + continue; |
| 53 | + } |
| 54 | + |
| 55 | + foreach ( $entry_ids as $entry_id ) { |
| 56 | + if ( ! GFAPI::entry_exists( $entry_id ) ) { |
| 57 | + continue; |
| 58 | + } |
| 59 | + |
| 60 | + if ( ! $this->is_applicable_form( $entry_id ) ) { |
| 61 | + continue; |
| 62 | + } |
| 63 | + |
| 64 | + $posts_data = maybe_unserialize( gform_get_meta( $entry_id, 'gravityformsadvancedpostcreation_post_id' ) ); |
| 65 | + |
| 66 | + if ( ! $posts_data ) { |
| 67 | + continue; |
| 68 | + } |
| 69 | + |
| 70 | + foreach ( $posts_data as $post_data ) { |
| 71 | + $post_id = $post_data['post_id'] ?? null; |
| 72 | + if ( $post_id ) { |
| 73 | + wp_update_post( array( |
| 74 | + 'ID' => $post_id, |
| 75 | + 'post_status' => $this->_args['post_status'], |
| 76 | + ) ); |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Check if the form is applicable. |
| 85 | + * |
| 86 | + * @param int $entry_id The entry ID. |
| 87 | + * |
| 88 | + * @return bool |
| 89 | + */ |
| 90 | + public function is_applicable_form( $entry_id ) { |
| 91 | + if ( empty( $this->_args['form_id'] ) ) { |
| 92 | + return true; |
| 93 | + } |
| 94 | + |
| 95 | + $entry = GFAPI::get_entry( $entry_id ); |
| 96 | + $form_id = rgar( $entry, 'form_id' ); |
| 97 | + |
| 98 | + return (int) $form_id === (int) $this->_args['form_id']; |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +new GSPC_Update_Post_Status_On_WC_Order_Status_Change( array( |
| 103 | + 'form_id' => 123, // Add `form_id` when you want to target a specific form. |
| 104 | + 'post_status' => 'wc-expired', // Add the post status you want to update the post to. |
| 105 | +) ); |
0 commit comments