获取当前值输入字段数量Woocommerce

时间:2017-02-17 08:24:42

标签: javascript php jquery woocommerce product-quantity

在PHP中有什么方法可以获得Woocommerce数量输入的当前值?

我的意思是这个领域:

enter image description here

我需要在这种情况下得到10并输出它。 我该怎么办呢?

谢谢

1 个答案:

答案 0 :(得分:0)

我决定安装此插件:https://it.wordpress.org/plugins/woo-min-max-quantity-limit/

有了这个,我可以得到每个产品的最小值和最大值。 我使用里面的代码:

$max = get_post_meta($product->id, '_wc_mmax_max', true);
$min = get_post_meta($product->id, '_wc_mmax_min', true);

完整的代码是:

// we are going to hook this on priority 31, so that it would display below add to cart button.
add_action('woocommerce_single_product_summary', 'woocommerce_total_product_price', 31);

function woocommerce_total_product_price() {

    global $woocommerce, $product;
    $max = get_post_meta($product->id, '_wc_mmax_max', true);
    $min = get_post_meta($product->id, '_wc_mmax_min', true);

    echo sprintf('<div id="product_total_price" style="margin-bottom:20px;display:none">%s %s</div>', __('Prezzo Prodotto :', 'woocommerce'), '<span class="price">' . $product->get_price() . '</span>');
    echo sprintf('<div id="cart_total_price" style="margin-bottom:20px;display:none">%s %s</div>', __('Totale Carrello :', 'woocommerce'), '<span class="price">' . $product->get_price() . '</span>');

    $moltiplicazione_prodotto = $product->get_price() * $max;
    $moltiplicazione_prodotto = number_format($moltiplicazione_prodotto, 2, '.', '');
    echo sprintf('<div id="prodotti_quantity" style="margin-bottom:20px;">%s %s</div>', __('Prezzo Prodotto :', 'woocommerce'), '<span class="price">' . get_woocommerce_currency_symbol() . $moltiplicazione_prodotto . '</span>');
    ?>
<script>
    jQuery(function ($) {
        var price = <?php echo $product->get_price(); ?>,
                current_cart_total = <?php echo $woocommerce->cart->cart_contents_total; ?>,
                currency = '<?php echo get_woocommerce_currency_symbol(); ?>';

        var quantita_iniziale = $('[name=quantity]').val();
        $('#prodotti_quantity').html(this.value);

        console.log("quantita iniziale", this.value);

        $('[name=quantity]').change(function () {
            if (!(this.value < 1)) {

                var product_total = parseFloat(price * this.value),
                cart_total = parseFloat(product_total + current_cart_total);

                $('#product_total_price .price').html(currency + product_total.toFixed(2));
                $('#cart_total_price .price').html(currency + cart_total.toFixed(2));
                if(this.value){
                $('#prodotti_quantity').html(this.value).hide();  
                }


            }
            $('#product_total_price,#cart_total_price').toggle(!(this.value <= 1));
        });
    });
</script>
<?php
}