我正在一个使用WooCommerce订阅和WooCommerce成员资格的网站上工作。它出售每年续订的订阅。
唯一使用的支付网关是支票(改名为通过发票付款),因此所有更新都是手动更新。发票被发送给客户(在WooCommerce之外),店主在付款后将订单标记为在管理中完成。
订阅状态与最新的订单状态相关联:
由于发票是通过外部会计系统发送和处理的,客户希望自动完成续订订单,这样用户的会员访问权限就不会中断。
我看过类似的问题,比如这里,我遇到了类似的问题。
Change subscription status to active based on order id WooCommerce
我已经测试了钩子woocommerce_new_order,可以看到它正在为所有订单启动,包括续订。
使用wcs_get_subscriptions_for_order(),可以将'renewal'作为order_type传递,以测试给定的订单是更新的还是新的客户。
我有一个非常小的代码来检查新订单,如果适用的话更新它,然后更新订阅状态。但是,我的第一个if块似乎被跳过了--它总是运行到else块中的代码。
此外,如果我将行移动以更新订单和子状态到else块(因此它确实更新了所有订单),则订单状态将更新,但子状态将保持搁置状态。
我猜想,如果更新来自管理员的订单,或者通过没有触发的支付方式更新订单,通常会调用一些触发器,但我无法从文档中看到它将是什么。
有人能告诉我哪里出了问题吗?
<?php
/**
* Autocomplete Renewal Orders
*/
function ctz_autocomplete_renewal_order($order_id) {
/**
* Check if the order is associated with a subscription
*
* WC Subscriptions gives us a function to obtain the related orders for a subscription in an array. The second parameter allows us to retrieve only renewal orders.
*
* We can use this function to check if the given order ID is for a new customer order or an existing customer's renewal.
*
* If this array returns blank, it's a first order, and should be skipped over.
*/
// Get related Subscriptions
$subscriptions = wcs_get_subscriptions_for_order($order_id, array('order_type' => 'renewal'));
if(is_array($subscriptions) && !empty($subscriptions) ) :
// This is a renewal order - change the status
$order = wc_get_order($order_id);
$order->update_status( 'completed');
$order->save();
// Check and update the subscription status
activate_subscriptions_for_order($order);
// Log the change
$message = $order_id . " Renewal automatically completed.";
ctz_custom_logs('autorenewals', $message);
else :
// This is a new order - leave unchanged
$message = $order_id . " Initial order - status unchanged.";
ctz_custom_logs('autorenewals', $message);
endif;
}
add_action('woocommerce_new_order', 'ctz_autocomplete_renewal_order', 11, 1);
?>FYI我使用的日志功能只是用于调试,并将给定的消息记录到一个文件中。
提前谢谢。
发布于 2022-04-24 19:07:35
add_filter('wcs_renewal_order_created', 'wcs_after_create_renewal_order', 10, 2);
function wcs_after_create_renewal_order($renewal_order, $subscription) {
// Set the current renewal order as completed.
$renewal_order->update_status('completed');
$renewal_order->add_order_note( __( 'Renewal automatically processed' ) );
// Set the corresponding subscription as active.
$subscription->update_status('active');
return $renewal_order;
}钩子wcs_renewal_order_created只有在创建更新命令之后才会触发。只需将上面的代码片段添加到活动主题functions.php文件中即可。
https://stackoverflow.com/questions/71990312
复制相似问题