首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用购物车总数和订购期替换WooCommerce“下单”按钮文本

用购物车总数和订购期替换WooCommerce“下单”按钮文本
EN

Stack Overflow用户
提问于 2020-08-18 17:13:35
回答 1查看 137关注 0票数 2

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

代码语言:javascript
复制
// 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;
}

现在,我希望在产品价格之后显示订阅期限,例如,如果有人购买每月订阅的产品,按钮应该显示为(Pay $20/Month)。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-08-18 18:14:29

当购物车中只有订阅产品时,您可以通过以下方式在结账页面获得自定义的“下单”按钮,并在提交按钮上显示购物车合计和订阅期限:

代码语言:javascript
复制
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文件中。经过测试,效果良好。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63465471

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档