结帐前更改购物车项目订阅属性值 [英] Change cart items subscription properties values before checkout
问题描述
这可能是一个菜鸟问题,所以提前致歉.
This may be a noob question so apologies in advance.
我有可变的订阅产品,其价格随订阅时间的长短而异.我也有一个简单的订阅,其subscription_length =0.当购物车同时包含两种类型时,Woocommerce订阅会创建两个订阅.
I have variable subscription products whose price varies by subscription length. I also have simple subscriptions that have a subscription_length = 0. When the cart contains both types, Woocommerce subscriptions creates two subscriptions.
我正在尝试修改购物车中所有商品的subscription_length元数据以匹配购物车中最长的长度,因此只能创建一个订阅.
I am trying to modify the subscription_length metadata of all the items in the cart to match the longest length in the cart so only one subscription will be created.
我似乎找不到正确的方法来更新购物车.这可能只是愚蠢的人类PHP技巧.
I just can't seem to find right way to update the cart. It might just be stupid human PHP tricks.
这是该语句的最新变体,目前无法正常运行:
This is the latest variation of the statement that is not working right now:
$cart[$item_key]['data']->subscription_length = $maxlength;
我已经尝试了数十种或更多种主题,以更新$ cart数据.有些失败,有些成功,但是$ cart中的数据没有改变.
I've tried a dozen or more variations on the theme to update the $cart data. some failed and some succeeded but the data in $cart did not change.
function csi_align_subscription_length() {
$maxlength = 0;
$cart = WC()->cart->get_cart();
//find the longest length
foreach ( $cart as $item_key => $item ){
if ( WC_Subscriptions_Product::is_subscription( $item['data'] ) ) {
$length = WC_Subscriptions_Product::get_length( $item['data'] );
$maxlength = ($length > $maxlength) ? $length : $maxlength ; //get the longest length
}
}
//set all subscription lengths to the longest lengths
foreach ( $cart as $item_key => $item ){
if ( WC_Subscriptions_Product::is_subscription( $item['data'] ) ) {
echo '<pre>';
echo "cart-item before: ". var_dump($cart[$item_key]);
$cart[$item_key]['data']->subscription_length = $maxlength;
echo "cart-item after: ".var_dump($cart[$item_key]);
echo '</pre>';
}
}
WC()->cart->set_cart_contents($cart);
}
add_filter( 'woocommerce_subscription_cart_before_grouping', 'csi_align_subscription_length', 10, 1 );
我只需要在购物车中获得订阅即可与一个普通组对齐,因此每次结帐时我只有1个订单和1个订阅.如果我在切线上迷路了,我愿意接受另一种方法.
I just need to get the subscriptions in the cart to align to a common group so I only have 1 order and 1 subscription per checkout. I am open to a different approach if I've gotten lost off on a tangent.
推荐答案
最终解决了该问题.没有用于"subscription_length"的设置器.不得不使用update_meta_data方法.元密钥为"_subscription_length".以下代码有效.
Finally resolved the issue. No setter for 'subscription_length'. Had to use update_meta_data method. The meta key is '_subscription_length'. the following code works.
function csi_align_subscription_length( $cart ) {
if ( ( is_admin() && ! defined( 'DOING_AJAX' ) ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
$maxlength = (float) 0;
// Find the longest subscription length
foreach ( $cart->get_cart() as $cart_item ){
if ( is_a( $cart_item['data'], 'WC_Product_Subscription' ) || is_a( $cart_item['data'], 'WC_Product_Subscription_Variation' ) ) {
$length = (float) WC_Subscriptions_Product::get_length( $cart_item['data'] );
$maxlength = ($length > $maxlength) ? $length : $maxlength ; //get the longest length
}
}
// set the subscription length for everything in the cart
foreach ( $cart->get_cart() as $item_key => $cart_item ){
if ( is_a( $cart_item['data'], 'WC_Product_Subscription' ) || is_a( $cart_item['data'], 'WC_Product_Subscription_Variation' )) {
$cart_item['data']->update_meta_data( '_subscription_length', $maxlength );
}
}
}
add_action( 'woocommerce_before_calculate_totals', 'csi_align_subscription_length', 5, 1 );
这篇关于结帐前更改购物车项目订阅属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!