wordpress 编程更改WooCommerce订阅付款方式

j2cgzkjk  于 5个月前  发布在  WordPress
关注(0)|答案(1)|浏览(74)

在添加新卡时,如果用户单击特定卡将其设置为“默认”,则其他卡的所有订阅都应移动到选定的卡。
$new_default_card:此变量有新的卡片数据(对象)
$subscription:此变量具有订阅数据(对象)
我想将其他卡的付款方式更新为新卡。
set_payment_method:此方法不会更新任何内容,而是从其他卡中删除订阅。

// Get the new default payment method details
$new_default_card = WC_Payment_Tokens::get($token_id);

$payment_method = WC_Payment_Tokens::get_customer_tokens(get_current_user_id());

$user_id = get_current_user_id(); // Get the current user ID

// Query subscriptions using WC_Subscriptions function
$args = array(
    'subscriptions_per_page' => -1, // Get all subscriptions
    'status' => array( 'active', 'on-hold', 'pending', 'cancelled', 'switched', 'expired' ), // 
    'customer_id' => $user_id, // Filter by customer ID
);

$subscription = wcs_get_subscriptions( $args );

$subscription->set_payment_method($payment_method->get_data()['token']);

$subscription->save();

字符串

yptwkmov

yptwkmov1#

由于wcs_get_subscriptions( $args );不是WC_Subscription对象,而是WC_Subscription对象的数组,因此方法set_payment_method()不能应用于非WC_Subscription对象,并且应该抛出错误...
在你的代码中,尝试替换:

$subscription = wcs_get_subscriptions( $args );

$subscription->set_payment_method($payment_method->get_data()['token']);

$subscription->save();

字符串
其中包括:

// Loop through customer subscriptions
foreach( wcs_get_subscriptions( $args ) as $subscription ) {
    $subscription->set_payment_method($payment_method->get_data()['token']);
    $subscription->save();
}


最好能成功...

相关问题