Shopify Graph_ql setting sku using text box?

Viewed 49

I am having an issue related to shopify graphql i am using private app that allow to fetch the information that is given inside the query but i want extend it to search sku using textbox in php reference link where i found code is : https://github.com/kiranrohra/shopify/blob/main/query%20based_orders.php

and this my approch:

<?php


$shopify_api_key = '';
$shopify_api_password = "";
$shop_name = '';

$graphql_url = 'https://'.$shopify_api_key.':'.$shopify_api_password.'@'.$shop_name.'/admin/api/2020-07/graphql.json';

$query = <<< 'JSON'
query {
    products(first: 1, query: "sku:SK-3228" ) { //here i want to put text from text box
    edges {
      node {
        id
        title
        status
        tags
        variants(first:1)
        {
          edges {
            node {
              id
              price
              compareAtPrice
              sku
              
            }
          }
        }
        images(first:5){
          edges{
            node{
              originalSrc
              
            }
          }
        }
        
      }
    }
  }
}
JSON; 


$post_data = array();
$post_data['query'] = $query;
//$post_data=$query;

$curl_init = curl_init();
curl_setopt($curl_init, CURLOPT_URL, $graphql_url);
curl_setopt($curl_init, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Accept: application/json'));
curl_setopt($curl_init, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_init, CURLOPT_VERBOSE, 0);
curl_setopt($curl_init, CURLOPT_HEADER, false);
curl_setopt($curl_init, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl_init, CURLOPT_POSTFIELDS, json_encode($post_data));
curl_setopt($curl_init, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec ($curl_init);
$decode_response = json_decode($response, true);
$httpcode = curl_getinfo($curl_init, CURLINFO_HTTP_CODE);
curl_close ($curl_init);

//print_r($decode_response);

$productss =$decode_response['data']['products'];

?>

and html is this:

<?php include_once("include/header.php"); ?>

<section>
    <form action="" method="post">
    <input type="hidden" name="action_type" value="search">

    <div class="row">
      <label for="ProductTitle"><b>Title</b></label>
    <input type="text" name="SKU" id="SKU">
    </div>
    </form>

    <table>
        <thead>
            <tr>
                <!-- <th colspan="1">Product Image</th> -->
                <th>Product Name</th>
                <th>Size</th>
                <th>status</th>
                <th>price</th>
                <th>compared at price</th>
                <th>SKU</th>
                <th>Image 1</th>
                <th>Image 2</th>
                <th>Image 3</th>
                <th>Image 4</th>
                <th>Image 5</th>

                <!-- <th>compareAtPrice</th> -->

            </tr>
        </thead>
        <tbody>
            <?php
            //for product title 
            foreach ($productss as $edge)
            {
                foreach($edge as $node)
                {
                    foreach ($node as $key => $value)
                    {
                        $image1 = count($value['images']['edges']) > 0 ? $value['images']['edges'][0]['node']['originalSrc'] : "";
                        $image2 = count($value['images']['edges']) > 0 ? $value['images']['edges'][1]['node']['originalSrc'] : "";
                        $image3 = count($value['images']['edges']) > 0 ? $value['images']['edges'][2]['node']['originalSrc'] : "";
                        $image4 = count($value['images']['edges']) > 0 ? $value['images']['edges'][3]['node']['originalSrc'] : "";
                        $image5 = count($value['images']['edges']) > 0 ? $value['images']['edges'][4]['node']['originalSrc'] : "";
                        ?>
                            <tr>
                            <td><?php echo $value['title'] ?></td>
                            <td><?php echo $value['tags'][1] ?></td>
                            <td><?php echo $value['status'] ?></td>
                            <td><?php echo $value['variants']['edges'][0]['node']['price'] ?></td>
                            <td><?php echo $value['variants']['edges'][0]['node']['compareAtPrice'] ?></td>
                            <td><?php echo $value['variants']['edges'][0]['node']['sku'] ?></td>
                            <td><img width="75" height="65" src="<?php echo $image1;  ?>"></td>
                            <td><img width="75" height="65" src="<?php echo $image2;  ?>"></td>
                            <td><img width="75" height="65" src="<?php echo $image3;  ?>"></td>
                            <td><img width="75" height="65" src="<?php echo $image4;  ?>"></td>
                            <td><img width="75" height="65" src="<?php echo $image5;  ?>"></td>
                            
                            </tr>
                        <?php
    
                    }
                }
            }



            ?>
        </tbody>
    </table>


</section>
<?php include_once("include/footer.php"); ?>
0 Answers
Related