我在wordpress中创建了一个名为“约会”的自定义post类型。每当有人在woocommerce预订产品时,就会创建一个新的应用程序。该任命有3种状态公布、起草和待审。我想要实现的是,当我改变任何任命的状态时,与之相关的产品的订单状态会发生变化。例如,如果我将约会状态更改为起草,则订单状态将被取消。我一直在努力想办法解决这个问题,但我真的很挣扎。请帮我解决这个问题。谢谢
发布于 2022-07-07 12:00:33
在更改约会职位类型的post状态之前,您应该能够连接到transition_post_status操作以添加自定义代码。
在下面的示例中,我正在检查更改后的帖子的post_type是否是“约会”(请用您的post状态段塞替换它)。然后,我尝试获取WC_Order对象(假设链接的订单ID存储为每个约会的post元linked_order_id,那么您的问题中没有指定这一点)。如果找到订单,我将根据约会的职位状态设置订单状态。
function update_order_status_by_appointment( $new_status, $old_status, $post ) {
// Perform only for 'appointment' post type
if( get_post_type( $post ) === 'appointment' ) {
// Get WC_Order linked to this appointment
$order_id = get_post_meta( $post->ID, 'linked_order_id', true);
$order = wc_get_order( $order_id );
// Check if order with this ID exists
if( $order ) {
// If appointment is being changed to 'draft', change order status to 'cancelled'
if( $new_status === 'draft' ) {
$order->update_status('cancelled');
}
// Add other conditions as you wish
// ...
}
}
}
add_action('transition_post_status', 'update_order_status_by_appointment', 10, 3);代码在您的活动子主题或主题的functions.php中。虽然它没有经过测试,但它应该有效。
https://wordpress.stackexchange.com/questions/407394
复制相似问题