Skip to content

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. #1020

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php
/**
* Gravity Forms Advanced Post Creation // Product Configurator
*
* This snippet will update the post status of the post created by Gravity Forms Advanced Post Creation when the WooCommerce order status is changed.
*
* 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
*
* https://gravitywiz.com/documentation/gs-product-configurator/
*
* Instructions:
* 1. Install per https://gravitywiz.com/documentation/how-do-i-install-a-snippet/
* 2. Configure the snippet based on inline instructions.
*/

class GSPC_Update_Post_Status_On_WC_Order_Status_Change {
private $_args;

public function __construct( $args = array() ) {
$this->_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.
) );
Loading