[Gelöst] Update custom field on woo product after purchase
This support ticket is created vor 2 Jahre, 3 Monate. There's a good chance that you are reading advice that it now obsolete.
This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.
Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.
No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.
Thank you for contacting us and I'd be happy to assist.
If your goal is to update the product's custom field value, when its order status is completed, you can use a custom function attached to WooCommerce's "woocommerce_order_status_completed" hook.
For example:
add_action('woocommerce_order_status_completed', 'custom_update_field_on_order_competed_status', 10, 1);
function custom_update_field_on_order_competed_status( $order_id ) {
// target custom field's slug
$target_field_slug = "wpcf-product-radio-field";
// get order info
$order = wc_get_order( $order_id );
// cycle through each product included in the order
foreach ( $order->get_items() as $item_id => $product_item ) {
// get the ID of the ordered product
$product_id = $product_item->get_product_id();
// get current field value
$current_field_value = get_post_meta($product_id, $target_field_slug, true);
// if value is not already equal to 2, set it to 2
if ($current_field_value != 2) {
update_post_meta( $product_id, $target_field_slug, 2 );
}
}
}
Note: The custom code examples from our forum are shared to get you started in the right direction. You're welcome to adjust them as needed and for more personalized customization assistance, you can consider hiring a professional from our list of recommended contractors: https://toolset.com/contractors/
regards,
Waqar
This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.