Ad

How To Get Product Id From Product On Loaded Product Page Using Ajax And Also Get Related Product Ids In Shopify

- 1 answer

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.
        }
    });

Ad

Answer

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
            });
        }
    });
Ad
source: stackoverflow.com
Ad