Garmaine Staff asked 4 months ago

So, I am trying to get the quantity of the user input and if it matches the total quantity stock then it will be sold as a wholesale product. But I cannot get the jquery function "add_cart_func('qty')" working. Please help!! Trying for hours. Edit- I have added the declaration of let qty = $("#"+type).val();. I have used "radio" for users to choose if they want to buy wholesale or retail.

<?php 
    if ($this->session->user=='good') 
    {   
        $price = $single['pro_quantity_stock'] == add_cart_func('qty') ? $single["pro_wholesale"] : $single["pro_retail"];  
?>
        <input type="radio" name="tt" value="Wholesale" onclick="doDisplay(this);"/>&nbsp;Buy Wholesale
        <span id="wholesales" style="display:none">&nbsp;&nbsp;&nbsp;&nbsp;
            <?php 
                if ($single['pro_wholesale'] != NULL) 
                { 
            ?>
                    <input name="qty" class ='txtbx' size="10" type="number" id="txtNumber" value="<?php echo $single['pro_quantity_stock'] ?>"  disabled />

            <?php
                }
                else
                {
            ?>
                    <input name="qty" class ='txtbx' size="10" type="number" id="txtNumber" value="0"  disabled />
            <?php
                }
            ?>
        </span>
        <br />

        <input type="radio" name="tt" value="Retail" onclick="doDisplay(this);"/> Buy Retail
        <span id="retails" style="display:none">&nbsp;&nbsp;&nbsp;&nbsp;
            <input name="qty" class ='txtbx1' size="10" type="number" id="txtNumber1" />
        </span>
        <br />

        <a href="#" class="btn btn-info btn-normal" onclick="add_cart_func(<?= $single['id'] ?>, '<?= $single["pro_title"] ?>', '<?= $price ?>')">
            <span class="glyphicon glyphicon-shopping-cart"></span> ADD TO CART
        </a>




<script type="text/javascript">
    function add_cart_func(id, name, price){
        // let qty = $("input[name='qty']").val();
        let qty = $("#"+type).val();
        jQuery.ajax({
            url: '<?= base_url("product/add_to_cart")?>',
            data: {'id':id, 'name':name, 'price': price, 'qty': qty},
            type: 'post',
            dataType: 'html',
            success: function(res){
                let _res = JSON.parse(res);
                $('span.p1.fa-stack.fa-2x.has-badge').attr('data-count', _res['qtys']);
                $('#navbar-cart-lists').html(_res['contents']);
            },
            error: function(err){
                console.log(error);
            }
        });
    }
</script>

<script>
    var type = "txtNumber";
    function doDisplay(radio)
    {
        switch (radio.value)
        {
            case "Wholesale":
                document.getElementById("wholesales").style.display = "inline";
                document.getElementById("retails").style.display = "none";
                type = "txtNumber"; 
                break;

            case "Retail":
                document.getElementById("wholesales").style.display = "none";
                document.getElementById("retails").style.display = "inline";
                type = "txtNumber1"; 
                break;
        }
    }
</script>

Controller

function add_to_cart(){ 
    $this->addToCart($this->input->post('id'), 
                    $this->input->post('name'), 
                    $this->input->post('price'), 
                    $this->input->post('qty'),
                    "product");
}