@Vlasinfo

Как изменить кнопку при выборе определенного метода оплаты?

Есть следующий код, с его помощью при выборе метода доставки "" я меняю css кнопки "Оформить заказ".

add_filter('woocommerce_order_button_html', 'disable_place_order_button_html' );
function disable_place_order_button_html( $button ) {
    // HERE define your targeted shipping method id
    $targeted_shipping_method = "flat_rate:9";

    // Get the chosen shipping method (if it exist)
    $chosen_shipping_methods = WC()->session->get('chosen_shipping_methods');
    
    // If the targeted shipping method is selected, we disable the button
    if( in_array( $targeted_shipping_method, $chosen_shipping_methods ) ) {
        $style  = 'style="background: #606060 !important; color: white !important; cursor: not-allowed !important; text-align: center; display: flex; align-items: center; justify-content: center; font-weight: 500; font-size: 14px; line-height: 22px; letter-spacing: 0.03em; text-transform: uppercase; border: none; width: 100%; height: 48px;"';
        $text   = apply_filters( 'woocommerce_order_button_text', __( 'Place order', 'woocommerce' ) );
        $button = '<a class="button" '.$style.'>' . $text . '</a>';
    }
    return $button;
}


Нужно сделать тоже самое, но при выборе метода оплаты "alg_custom_gateway_1"
Нашел следующие решение, оно меняет текст кнопки при выборе этого метода, но как изменить css кнопки как в первом варианте?

add_filter('woocommerce_order_button_text', 'custom_order_button_text' );
function custom_order_button_text( $order_button_text ) {
    $default = __( 'Place order', 'woocommerce' ); // If needed
    $chosen_payment_method = WC()->session->get('chosen_payment_method');

    if( $chosen_payment_method == 'alg_custom_gateway_1'){

        $order_button_text = __( 'Не выбран способ оплаты', 'woocommerce' ); 
    }
	
    // jQuery code: Make dynamic text button "on change" event ?>
    <script type="text/javascript">
    (function($){
        $('form.checkout').on( 'change', 'input[name^="payment_method"]', function() {
            var t = { updateTimer: !1,  dirtyInput: !1,
                reset_update_checkout_timer: function() {
                    clearTimeout(t.updateTimer)
                },  trigger_update_checkout: function() {
                    t.reset_update_checkout_timer(), t.dirtyInput = !1,
                    $(document.body).trigger("update_checkout")
                }
            };
            t.trigger_update_checkout();
        });
    })(jQuery);
    </script><?php

    return $order_button_text;
}
  • Вопрос задан
  • 67 просмотров
Решения вопроса 1
add_filter('woocommerce_order_button_html', 'custom_order_button_text' );
function custom_order_button_text( $button_shop ) {
    $default = __( 'Place order', 'woocommerce' ); // If needed
    $chosen_payment_method = WC()->session->get('chosen_payment_method');

    if( $chosen_payment_method == 'alg_custom_gateway_1'){

    $button_shop = '<button type="submit">Ваша кнопка с нужными значениями и классами</button>';
    }
	
    // jQuery code: Make dynamic text button "on change" event ?>
    <script type="text/javascript">
    (function($){
        $('form.checkout').on( 'change', 'input[name^="payment_method"]', function() {
            var t = { updateTimer: !1,  dirtyInput: !1,
                reset_update_checkout_timer: function() {
                    clearTimeout(t.updateTimer)
                },  trigger_update_checkout: function() {
                    t.reset_update_checkout_timer(), t.dirtyInput = !1,
                    $(document.body).trigger("update_checkout")
                }
            };
            t.trigger_update_checkout();
        });
    })(jQuery);
    </script><?php

    return $button_shop;
}
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы