php - WooCommerce 订阅 - 自动完成续订订单和订阅状态

标签 php woocommerce hook-woocommerce woocommerce-subscriptions woocommerce-memberships

我正在开发一个使用 WooCommerce 订阅和 WooCommerce 成员(member)资格的网站。它出售每年续订的订阅服务。

使用的唯一支付网关是支票(重命名为通过发票付款),因此所有续订都是手动续订。发票将发送给客户(WooCommerce 之外),并且商店所有者在付款后在管理员中将订单标记为“已完成”。

订阅状态与最新订单状态相关:

  • 如果订单完成,则子订单处于事件状态。

  • 如果订单处于待处理、处理、暂停等状态,则子订单处于暂停状态。

由于发票是通过外部会计系统发送和处理的,因此客户希望自动完成续订订单,以便用户的成员(member)访问权限不会中断。

我看过类似的问题,例如这里,并且遇到了类似的问题。

Change subscription status to active based on order id WooCommerce

我已经测试了钩子(Hook) woocommerce_new_order 并且可以看到它针对所有订单(包括续订)触发。

使用 wcs_get_subscriptions_for_order(),可以将 'renewal' 作为 order_type 传递,以测试给定订单是续订还是订单新客户。

我有一小段代码来检查新订单,更新它(如果适用),然后更新订阅状态。但是,似乎我的第一个 if block 被跳过 - 它总是运行到 else block 中的代码。

此外,如果我将用于更新订单和子状态的行移动到 else block (因此它会更新所有订单),订单状态会更新,但子状态将保持为“暂停”。

我猜测如果从管理员更新订单或通过未触发的付款更新订单,通常会调用一些触发器,但我从文档中看不到它会是什么。

有人可以告诉我这里哪里出了问题吗?

<?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);

?>

仅供引用,我使用的日志记录功能仅用于调试并将给定的消息记录到文件中。

提前致谢。

最佳答案

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;
}

仅在创建续订订单后才会触发 Hook wcs_renewal_order_created。只需将上面的代码片段添加到您的事件主题 functions.php 文件中即可。

关于php - WooCommerce 订阅 - 自动完成续订订单和订阅状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71990312/

相关文章:

php - Woocommerce 结帐自定义选择字段

php - 如果产品描述为空,WooCommerce 如何隐藏 "Description"选项卡

javascript - 在我的例子中如何使用 php 传递参数

php - 我的表格安全吗?

php - 管理不同用户的时间偏移?

java - 通过 POST 将 BitMap 对象(和一些其他字符串)发送到服务器

php - 用 WooCommerce 中的自定义数量输入字段替换 "add to cart"

php - WooCommerce 自定义 Ajax 添加到购物车 : How to set a custom price?

php - 下载自定义尺寸的图像 - 重新链接到页面

javascript - 按钮点击警报未触发