• Как сделать, отображение цены в зависимости от количества товара прямо в карточке товара?

    Palych_tw
    @Palych_tw
    Типа веб-разработчик
    6e41f69aee52432fb5aa4bfcee10bb4c.png

    Такой вариант вам подойдет? могу скинуть готовый код.

    UPD. Вам нужно переопределить шаблон quantity-input.php (скопируйте себе в тему)... и вот такой код в него вставить
    <?php
    if ( ! defined( 'ABSPATH' ) ) {
        exit; // Exit if accessed directly
    }
    $post_type = get_post_type( $post_id );
    if ($post_type=='product') {
    global $product;
    ?>
    <div class="quantity">
        Количество: <button type="button" id="remove_one">-</button><input type="text" name="<?php echo esc_attr( $input_name ); ?>" value="<?php echo esc_attr( $input_value ); ?>" title="<?php echo esc_attr_x( 'Qty', 'Product quantity input tooltip', 'woocommerce' ) ?>" class="input-text qty text" size="4" pattern="<?php echo esc_attr( $pattern ); ?>" inputmode="<?php echo esc_attr( $inputmode ); ?>" /><button type="button" id="add_one">+</button>
        <div class="order-summ">Сумма: <span id="orderSumm"><?php echo $product->get_price();?></span> грн.</div>
    </div>
    <script>
        var price = jQuery('#orderSumm').html();
        var quantity;
        var summ;
        jQuery('#remove_one').on('click',function(){
            quantity = jQuery('input.qty').val();
            if (quantity <= 1) {
                summ = price*quantity;
                jQuery('input.qty').val(quantity);
                jQuery('#orderSumm').html(summ);
            } else {
                quantity--;
                summ = price*quantity;
                jQuery('input.qty').val(quantity);
                jQuery('#orderSumm').html(summ);
            }
        });
        jQuery('#add_one').on('click',function(){
            quantity = jQuery('input.qty').val();
            quantity++;
            summ = price*quantity;
            jQuery('input.qty').val(quantity);
            jQuery('#orderSumm').html(summ);
        });
        jQuery('input.qty').keyup(function(){
            var removedText = $(this).val().replace(/\D/, '');
            jQuery(this).val(removedText);
            quantity = jQuery(this).val();
            summ = price*quantity;
            jQuery('input.qty').val(quantity);
            jQuery('#orderSumm').html(summ);
        });
    
    </script>
    <?php } else {?>
        <div class="quantity">
            <input type="number" step="<?php echo esc_attr( $step ); ?>" min="<?php echo esc_attr( $min_value ); ?>" max="<?php echo esc_attr( $max_value ); ?>" name="<?php echo esc_attr( $input_name ); ?>" value="<?php echo esc_attr( $input_value ); ?>" title="<?php echo esc_attr_x( 'Qty', 'Product quantity input tooltip', 'woocommerce' ) ?>" class="input-text qty text" size="4" pattern="<?php echo esc_attr( $pattern ); ?>" inputmode="<?php echo esc_attr( $inputmode ); ?>" />
        </div>
    <?php }?>
    Ответ написан
    6 комментариев