简体   繁体   English

更改 WooCommerce 订阅产品的注册费购物车项目价格

[英]Change sign up fee cart item price for WooCommerce subscription products

In WooCommerce I am changing the price of products in cart using the following hooked function:在 WooCommerce 中,我正在使用以下钩子 function 更改购物车中产品的价格:

add_action('woocommerce_before_calculate_totals', 'set_custom_price',1000,1);
function set_custom_price($cart_obj) {
    foreach ($cart_obj->get_cart() as $key => $value) {
        if($value['alredy_have_number'] == true) {
            $value['data']->set_price(0.90);
        }
    }
}

it works fine for recurring price but I wants to change signup fee for subscription products.它适用于经常性价格,但我想更改订阅产品的注册费。 what function or hook can I use for that?我可以使用什么 function 或挂钩?

To change some cart item specific metadata like the subscription signup fee , you will use the WC_Data method update_meta_data() that can be used on any WC_Product Object like Subscription products.要更改某些特定于购物车项目的元数据,例如订阅注册费,您将使用WC_Data方法update_meta_data() ,该方法可用于任何WC_Product Object (例如订阅产品)。

The related subscription signup fee meta_key to be used is _subscription_sign_up_fee so in your hooked function code, you will use it this way to change its price:要使用的相关订阅注册费meta_key_subscription_sign_up_fee所以在你的钩子 function 代码中,你将使用它来改变它的价格:

add_action( 'woocommerce_before_calculate_totals', 'change_subscription_data', 1000, 1 );
function change_subscription_data( $cart ) {
    if (is_admin() && !defined('DOING_AJAX'))
        return;

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    // Loop through cart items
    foreach ( $cart->get_cart() as $cart_item ) {

        // Check that product custom cart item data "alredy_have_number" exist and is true
        if( isset($cart_item['alredy_have_number']) && $cart_item['alredy_have_number'] ) {

            // Change subscription price
            $cart_item['data']->set_price(10.60);

            // Only for subscription products
            if ( in_array( $cart_item['data']->get_type(), ['subscription', 'subscription_variation']) ) {
                // Change subscription Sign up fee
                $cart_item['data']->update_meta_data('_subscription_sign_up_fee', 3.40);
            }
        }
    }
}

Code goes in functions.php file of your active child theme (or active theme).代码进入您的活动子主题(或活动主题)的functions.php 文件。 Tested and works.测试和工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 获取简单订阅产品的订阅价格及其注册费 - Get subscription price of simple subscription product and its sign up fee 根据购物车中的产品更改每​​月订阅费用 - Changing the monthly subscription fee based on products in the cart 更改woocommerce中特定产品的购物车项目数量 - Change the cart item quantity for specific products in woocommerce 从 Woocommerce 订阅价格中隐藏“免费试用”文本,但保留注册费 - Hide “free trial” text from Woocommerce Subscriptions price but keep the Sign-Up Fee 根据自定义字段和数量阈值更改 WooCommerce 购物车商品价格 - Change WooCommerce cart item price based on a custom field and quantity threshold 在 Woocommerce 中动态更改特定产品的购物车项目价格和名称 - Change cart item price and name for a specific product dynamically in Woocommerce 跳过特定订阅产品的 WooCommerce 购物车 - Skip WooCommerce cart for specific subscription products WooCommerce 购物车费用从自定义产品输入字段计算 - WooCommerce cart Fee calculated from Custom products input field 基于 Woocommerce 中特定产品类别购物车项目数量的购物车费用 - Cart fee based on specific product category cart item quantity in Woocommerce WooCommerce 以自定义价格添加到购物车产品 - WooCommerce add to cart products with customized price
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM