Support

Search results for ""

Sorry, no results found. Perhaps you would like to search the documentation?
All Topics
Deon Kretzschmar

Inline Editing Product Variations – Update Post

Hi,

We’re editing product variation prices using the inline edit option.

We need to update the post “modified date” to enable our sync back to Quickbooks to see that there was a change and fire the sync.

I have added this piece of code, but not seeing any results?

/**
 * By default, the post modified date is not always updated when using inline or bulk edit with Admin Column Pro.
 * For example, when updating meta data the <code>wp_update_post</code> is not called, which is the call that set then post's modified date.
 * In this example we will trigger this call manually.
 *
 * @param AC\Column $column
 * @param int       $id
 */
function acp_editing_update_post_modified_date( AC\Column $column, $id ) {
	$meta_type = $column->get_list_screen()->get_meta_type();

	// Update the <code>modified_date</code> after making any changes using inline or bulk editing
	if ( AC\MetaType::POST === $meta_type ) {
		wp_update_post( [ 'ID' => $id ] );
	}

	// Update the <code>modified_date</code> after making changes to a specific custom field
	if ( AC\MetaType::POST === $meta_type && $column instanceof AC\Column\Meta && 'my_custom_field' === $column->get_meta_key() ) {
		wp_update_post( [ 'ID' => $id ] );
	}
}

add_action( 'acp/editing/saved', 'acp_editing_update_post_modified_date', 10, 2 );

Any suggestions?

Thanks!

2 years, 1 month ago
Stefan van den Dungen Gronovius
Developer

Your snippet looks fine and the code should run when you make a change to a product variation. In fact, it now runs for every change to a column for a (custom) post-type overview.

Please notice that when you do an update_post for a Product variation, you actually update the variation itself, not its parent post (Product). You could check for the Variation overview page and make sure that the product is updated instead of the product variation. Something like this (not tested):

if ( $column->get_list_screen() instanceof ACA\WC\ListScreen\ProductVariation ) {
	$parent_product = get_post( $id )->post_parent;
	wp_update_post( [ 'ID' => $parent_product ] );
}
2 years, 1 month ago
Deon Kretzschmar

Thanks Stefan,

I’ve added it as below, but for some reason, it is not updating the parent product?

/**
 * By default, the post modified date is not always updated when using inline or bulk edit with Admin Column Pro.
 * For example, when updating meta data the <code>wp_update_post</code> is not called, which is the call that set then post's modified date.
 * In this example we will trigger this call manually.
 *
 * @param AC\Column $column
 * @param int       $id
 */
function acp_editing_update_post_modified_date( AC\Column $column, $id ) {
	$meta_type = $column->get_list_screen()->get_meta_type();
	
	// Find product variations parent product and update
	if ( $column->get_list_screen() instanceof ACA\WC\ListScreen\ProductVariation ) {
		$parent_product = get_post( $id )->post_parent;
		wp_update_post( [ 'ID' => $parent_product ] );
	}

	// Update the <code>modified_date</code> after making any changes using inline or bulk editing
	if ( AC\MetaType::POST === $meta_type ) {
		wp_update_post( [ 'ID' => $id ] );
	}

	// Update the <code>modified_date</code> after making changes to a specific custom field
	if ( AC\MetaType::POST === $meta_type && $column instanceof AC\Column\Meta && 'my_custom_field' === $column->get_meta_key() ) {
		wp_update_post( [ 'ID' => $id ] );
	}

}

add_action( 'acp/editing/saved', 'acp_editing_update_post_modified_date', 10, 2 );
2 years, 1 month ago
Deon Kretzschmar

Hi Stefan,

It is updating the “last modified” date, but somehow the MyWorks Sync between Woo and Quickbooks is not picking it up. I’ll check with them in the meantime.

Thanks!

2 years, 1 month ago
Stefan van den Dungen Gronovius
Developer

Hi Deon,

Maybe the sync works on an actual product save.
Instead of the wp_update_post() method you could try to retrieve the product and use the internal WC save method

$product = wc_get_product( $parent_product );
$product->save();
2 years, 1 month ago
Deon Kretzschmar

Still not having any luck…

This is the code that I have in place:

/**
 * By default, the post modified date is not always updated when using inline or bulk edit with Admin Column Pro.
 * For example, when updating meta data the <code>wp_update_post</code> is not called, which is the call that set then post's modified date.
 * In this example we will trigger this call manually.
 *
 * @param AC\Column $column
 * @param int       $id
 */
function acp_editing_update_post_modified_date( AC\Column $column, $id ) {
	$meta_type = $column->get_list_screen()->get_meta_type();
	
	// Find product variations parent product and update
	if ( $column->get_list_screen() instanceof ACA\WC\ListScreen\ProductVariation ) {
		$parent_product = get_post( $id )->post_parent;
		wp_update_post( [ 'ID' => $parent_product ] );
		$product = wc_get_product( $parent_product );
		$product->save();
	}

	// Update the <code>modified_date</code> after making any changes using inline or bulk editing
	if ( AC\MetaType::POST === $meta_type ) {
		wp_update_post( [ 'ID' => $id ] );
	}

}

add_action( 'acp/editing/saved', 'acp_editing_update_post_modified_date', 10, 2 );

This is the feedback from MyWorks:

The way our sync is aware of an updated product in WooCommerce is by watching/reading the “woocommerce_update_product” hook. As long as this hook is called when WooCommerce product details are updated, then we would be able to sync this into QuickBooks for you.

Not sure how to update the code to use the product update hook?

2 years, 1 month ago
Stefan van den Dungen Gronovius
Developer

Hmm, that hook should be triggered when you call save() on $product.
You could try to trigger the hook yourself in the conditional to see if it works,

do_action( 'woocommerce_update_product', $product->get_id(), $product );

Maybe they only register the hook on specific pages. Our call runs through Ajax.

2 years, 1 month ago
Deon Kretzschmar

Thanks,

Any chance you can send me the code snippet? Not sure where to slot this in…

2 years, 1 month ago
Stefan van den Dungen Gronovius
Developer

You can place it inside the conditional for the Product Variation overview.

// Find product variations parent product and update
if ( $column->get_list_screen() instanceof ACA\WC\ListScreen\ProductVariation ) {
	$parent_product = get_post( $id )->post_parent;
	wp_update_post( [ 'ID' => $parent_product ] );
	$product = wc_get_product( $parent_product );
	$product->save();
	do_action( 'woocommerce_update_product', $product->get_id(), $product );
}
2 years, 1 month ago

You must be logged in to reply to this topic.