wordpress 如何从Woocommerce订阅创建的订阅中删除项目,在php中使用钩子?

1rhkuytd  于 2023-03-07  发布在  WordPress
关注(0)|答案(1)|浏览(140)

我正在使用Woocommerce订阅来创建我的订阅产品。我有一个用例,我需要用不同的产品更改新创建的订阅中的产品。为了实现这一点,我的计划是从订阅中删除"旧"产品,并将"新"产品添加到订阅中。
我曾尝试使用woocommerce_subscription_status_pending_to_active action钩子上的回调函数从新创建的订阅中删除"旧"产品,但它一直在循环,直到耗尽允许的内存大小。
这是我的回调函数:

/**
 * Triggered when a subscription status is changed from pending to active.
 *
 * @since 2.2.22
 * @param WC_Subscription $subscription
 */

function action_activated_subscription( $subscription ) {

    error_log('Hi before doing foreach subscription');

    foreach( $subscription->get_items() as $item_id => $subscription_item ){
        $product_id = $subscription_item['product_id'];
        $subscription_item_id = $subscription_item['id'];
        error_log('test product ID is: ' . $product_id);
        if ($product_id === 3913) {
            error_log('Remove item!');           
            $subscription->remove_item($subscription_item_id);
            // Update totals.
            error_log('Calculating totals.');   
            $subscription->calculate_totals();
            error_log('Saving subscription.');   
            $subscription->save();
        }
    }
}
add_action( 'woocommerce_subscription_status_pending_to_active', 'action_activated_subscription', 20, 1 );

这是我的错误日志,表示以致命错误结束的连续循环:

[01-Aug-2022 21:41:45 UTC] Hi before doing foreach subscription
[01-Aug-2022 21:41:45 UTC] test product ID is: 3873
[01-Aug-2022 21:41:45 UTC] test product ID is: 3913
[01-Aug-2022 21:41:45 UTC] Remove item!
[01-Aug-2022 21:41:45 UTC] Calculating totals.
[01-Aug-2022 21:41:45 UTC] Hi before doing foreach subscription
[01-Aug-2022 21:41:45 UTC] test product ID is: 3873
[01-Aug-2022 21:41:45 UTC] test product ID is: 3913
[01-Aug-2022 21:41:45 UTC] Remove item!
[01-Aug-2022 21:41:45 UTC] Calculating totals.
[01-Aug-2022 21:41:45 UTC] Hi before doing foreach subscription
[01-Aug-2022 21:41:45 UTC] test product ID is: 3873
[01-Aug-2022 21:41:45 UTC] test product ID is: 3913
[01-Aug-2022 21:41:45 UTC] Remove item!
[01-Aug-2022 21:41:45 UTC] Calculating totals.
[01-Aug-2022 21:41:45 UTC] Hi before doing foreach subscription
[01-Aug-2022 21:41:45 UTC] test product ID is: 3873
[01-Aug-2022 21:41:45 UTC] test product ID is: 3913
[01-Aug-2022 21:41:45 UTC] Remove item!
[01-Aug-2022 21:41:45 UTC] Calculating totals.
[01-Aug-2022 21:41:45 UTC] Hi before doing foreach subscription
[01-Aug-2022 21:41:45 UTC] test product ID is: 3873
[01-Aug-2022 21:41:45 UTC] test product ID is: 3913
[01-Aug-2022 21:41:45 UTC] Remove item!
[01-Aug-2022 21:41:45 UTC] Calculating totals.
[01-Aug-2022 21:42:59 UTC] Hi before doing foreach subscription
[01-Aug-2022 21:42:59 UTC] test product ID is: 3873
[01-Aug-2022 21:42:59 UTC] test product ID is: 3913
[01-Aug-2022 21:42:59 UTC] Remove item!
[01-Aug-2022 21:42:59 UTC] Calculating totals.
[01-Aug-2022 21:42:59 UTC] Hi before doing foreach subscription
[01-Aug-2022 21:42:59 UTC] test product ID is: 3873
[01-Aug-2022 21:42:59 UTC] test product ID is: 3913
[01-Aug-2022 21:42:59 UTC] Remove item!
[01-Aug-2022 21:42:59 UTC] Calculating totals.
[01-Aug-2022 21:42:59 UTC] Hi before doing foreach subscription
[01-Aug-2022 21:42:59 UTC] test product ID is: 3873
[01-Aug-2022 21:42:59 UTC] test product ID is: 3913
[01-Aug-2022 21:42:59 UTC] Remove item!
[01-Aug-2022 21:42:59 UTC] Calculating totals.
[01-Aug-2022 21:42:59 UTC] PHP Fatal error:  Allowed memory size of 134217728 bytes exhausted (tried to allocate 20480 bytes) in /usr/local/lsws/tuttelue/html/wp-includes/meta.php on line 1198
[01-Aug-2022 21:42:59 UTC] PHP Fatal error:  Allowed memory size of 134217728 bytes exhausted (tried to allocate 20480 bytes) in /usr/local/lsws/tuttelue/html/wp-includes/shortcodes.php on line 265

如何从订阅中成功删除项目?

ttisahbt

ttisahbt1#

它看起来像是$订阅-〉calculate_total();行阻止循环到达末尾,并且它似乎正在重新启动,因为我们从未在日志文件中看到'Saving subscription.'。我认为calculate_totals()函数正在从循环内部更改数组,这会使事情变得混乱。
也许可以将calculate_totals()和保存()从foreach循环中移到最后。

相关问题