Ad
Replace WooCommerce "Place Order" Button Text With Cart Total And Subscription Period
I'm using the following code to target specifically, subscription-based products.
// 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;
}
Now, I want to display the subscription period after the product price, so for example, if someone is purchasing a monthly subscription product, the button should read (Pay $20/Month).
Ad
Answer
Use the following to get a custom "Place order" button on checkout page, when there are only subscription products is in cart, displaying the cart total with the subscription period on the submit button:
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;
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
Ad
source: stackoverflow.com
Related Questions
- → "failed to open stream" error when executing "migrate:make"
- → October CMS Plugin Routes.php not registering
- → OctoberCMS Migrate Table
- → OctoberCMS Rain User plugin not working or redirecting
- → October CMS Custom Mail Layout
- → October CMS - How to correctly route
- → October CMS create a multi select Form field
- → October CMS - Conditionally Load a Different Page
- → How to disable assets combining on development in OctoberCMS
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → OctoberCms component: How to display all ID(items) instead of sorting only one ID?
- → In OctoberCMS how do you find the hint path?
- → How to register middlewares in OctoberCMS plugin?
Ad