Замените текст кнопки «Разместить заказ WooCommerce» на общую сумму корзины и период подписки.

Я использую следующий код для конкретных продуктов на основе подписки.

// 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 долларов в месяц).


person Usama Shabbier    schedule 18.08.2020    source источник


Ответы (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 вашей активной дочерней темы (или активной темы). Проверено и работает.

person LoicTheAztec    schedule 18.08.2020
comment
Большое спасибо. Вы помогали мне, много раз. Я очень благодарен. - person Usama Shabbier; 18.08.2020