1

Есть кнопка, которая должна при клике увеличить количество товара и сразу же обновить корзину

<input type="button" data-index_value="cart[<?php echo $cart_item_key;?>][qty]" id="tooltip_btn_<?php echo esc_html($index);?>" class="button updater" value="<?php esc_html_e( 'Add to', 'wsh' );  ?>">    

JQuery на увеличение количества товара

const tooltip = ( id ) => {      
        jQuery('div.woocommerce').on( 'click', `#tooltip_btn_${id}`, function() {       
            let code_input = jQuery(`#tooltip_btn_${id}`).attr("data-index_value");                   
            let inner_code ='[name="' + code_input + '"]';
            let old_value = jQuery(inner_code).val(); 
            let new_value = parseInt(old_value) + 1;
        jQuery(inner_code).val(new_value);                     
     
     });
    
 }     

const garantee_index = jQuery("input[name='garantee_index']").attr("data-index_full");
for ( let i = 1;i<garantee_index;i++ ) {       
    tooltip ( i );      
}   

Есть еще строка которая должна запустить событие на кнопке

jQuery("[name='update_cart']").trigger("click");

По отдельности все работает, т.е если убираю "последний" количество товара увеличивается, но корзина не обновляется. А если убираю код увеличения количества товара, то корзина обновляется.

2 ответа 2

1

что на счет этого варианта?

jQuery('div.woocommerce').on( 'click', `#tooltip_btn_${id}`, function() {       
    let code_input = jQuery(`#tooltip_btn_${id}`).attr("data-index_value");                   
    let inner_code ='[name="' + code_input + '"]';
    let old_value = jQuery(inner_code).val(); 
    let new_value = parseInt(old_value) + 1;
    jQuery(inner_code).val(new_value);                     

    jQuery(document).find("[name='update_cart']").trigger("click");
});
3
  • Не срабатывает.
    – Nemolite
    4 мар 2021 в 17:06
  • есть ошибки в консоли?
    – Darth KYL
    4 мар 2021 в 17:08
  • Нет. Попробую немного по другому решить. Как всегда, через жесточайшие костыли
    – Nemolite
    4 мар 2021 в 17:32
0

Задача решена.

 const tooltip = ( id ) => {      
        jQuery('div.woocommerce').on( 'click', `#tooltip_btn_${id}`, function() { 
            productID = jQuery(`#product-name__tooltip_${id}`).attr("data-product_id");              

            let code_input = jQuery(`#tooltip_btn_${id}`).attr("data-index_value");                   
            let inner_code ='[name="' + code_input + '"]';
            let old_value = jQuery(inner_code).val(); 
            let new_value = parseInt(old_value) + 1;
            jQuery(inner_code).val(new_value);       

            jQuery.ajax({
                    url: '/wp-admin/admin-ajax.php',
                    type: 'POST',
                    data: {
                        action: 'transfer_tooltip',
                        product_id: productID                        
                    }
                });

        });
        
 }   



const garantee_index = jQuery("input[name='garantee_index']").attr("data-index_full");
    for ( let i = 1;i<=garantee_index;i++ ) {
       
        tooltip ( i );
      
    } 

Далее

add_action( 'wp_ajax_transfer_tooltip', 'wsh_transfer_tooltip' );
add_action( 'wp_ajax_nopriv_transfer_tooltip','wsh_transfer_tooltip' );
function wsh_transfer_tooltip() {
    global $post, $woocommerce;

    if ( isset( $_POST['product_id'] ) ) {
        $product_id = sanitize_text_field( $_POST['product_id'] );        
    }   

    $woocommerce->cart->add_to_cart( $product_id );
    
    die();
}

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.