【问题标题】:Replace WooCommerce "Place order" button text with cart total and subscription period用购物车总数和订阅期替换 WooCommerce“下订单”按钮文本
【发布时间】:2020-12-07 10:51:50
【问题描述】:

我正在使用以下代码专门针对基于订阅的产品。

// Display total amount on place order button
add_filter('woocommerce_order_button_text', 'subscriptions_custom_checkout_submit_button_text' );
function subscriptions_custom_checkout_submit_button_text( $order_button_text ) {
if ( WC_Subscriptions_Cart::cart_contains_subscription() ) {
    $cart_total = WC()->cart->total;    
return __('Pay $' . $cart_total, 'woocommerce');
    
} else {
    // You can change it here for other products types in cart
    # $order_button_text =  __( 'Something here', 'woocommerce-subscriptions'  );
}
return $order_button_text;
}

现在,我想在产品价格之后显示订阅期,例如,如果有人购买包月订阅产品,按钮应显示为(支付 20 美元/月)。

【问题讨论】:

    标签: php wordpress woocommerce checkout woocommerce-subscriptions


    【解决方案1】:

    使用以下方法在结帐页面上获取自定义“下订单”按钮,当购物车中只有订阅产品时,在提交按钮上显示购物车总数和订阅期限:

    add_filter('woocommerce_order_button_text', 'subscriptions_custom_checkout_submit_button_text' );
    function subscriptions_custom_checkout_submit_button_text( $button_text ) {
        if ( WC_Subscriptions_Cart::cart_contains_subscription() ) {
            $cart  = WC()->cart;
            $total = $cart->total;
            $other = false;
    
            // Loop through cart items
            foreach ( $cart->get_cart() as $item )  {
                $product = $item['data'];
                if( in_array( $product->get_type(), ['subscription', 'subscription_variation'] ) ) {
                    $period = get_post_meta( $product->get_id(), '_subscription_period', true );
                } else {
                    $other = true; // There are other items in cart
                }
            }
            // When there are only Subscriptions in cart
            if ( isset($period) && ! $other ) {
                return sprintf( __('Pay %s/%s' , 'woocommerce'), strip_tags( wc_price($total) ), ucfirst($period) );
            }
        }
        return $button_text;
    }
    

    代码在您的活动子主题(或活动主题)的functions.php 文件中。经过测试并且可以工作。

    【讨论】:

    • 非常感谢。你帮助了我很多次。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 2020-10-01
    • 1970-01-01
    • 2016-09-04
    • 2018-01-02
    • 2021-01-10
    • 1970-01-01
    • 1970-01-01
    • 2021-01-21
    相关资源
    最近更新 更多