Search code examples
shopifylaravel-5.5

how to get product id from product on loaded product page using ajax and also get related product ids in shopify


when I click on product title then I want to get product id and then post product id to another URL and get related product id from the database.

<script>
var a = window.location.href;
    $.ajax({
        type: "GET",
        dataType: "json",
        success: function (data) {
            var id = data.product.id;
            console.log(id);// i got product id but how i post this id to another url and get related product id from database.
        }
    });


Solution

  • Do this:

    <script>
    var a = window.location.href;
        $.ajax({
            type: "GET",
            dataType: "json",
            success: function (data) {
                var id = data.product.id;
                $.post("{{url('productIdApi')}}", {product_id: id}, function(result){
                   //do whatever after response returns
                });
            }
        });