Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 取代“商业”;下单“;包含购物车总数和订阅期的按钮文本_Php_Wordpress_Woocommerce_Checkout_Woocommerce Subscriptions - Fatal编程技术网

Php 取代“商业”;下单“;包含购物车总数和订阅期的按钮文本

Php 取代“商业”;下单“;包含购物车总数和订阅期的按钮文本,php,wordpress,woocommerce,checkout,woocommerce-subscriptions,Php,Wordpress,Woocommerce,Checkout,Woocommerce Subscriptions,我使用以下代码专门针对基于订阅的产品 // 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 (

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

// 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美元)。

当购物车中只有订阅产品时,使用以下命令在结帐页上获得自定义的“下单”按钮,在submit(提交)按钮上显示购物车总额和订阅期:

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文件。经过测试,效果良好。

非常感谢。你帮了我很多次。我非常感激。