2

Here I stuck in shopify creating webhook through API

I am using cake php for creating public shopify app

Now I would like to create carts/update hook for my app using API here is my code Cake php library : https://github.com/cmcdonaldca/CakePHP-Shopify-Plugin

File : ShopifyApiComponent.php

CODE :

public function createwebhook($shop_domain, $access_token){

    $method = "POST";
    $path = "/admin/webhooks.json";

    $params = array("webhook" => array( "topic"=>"carts/create",
                    "address"=>  $this->site_url."users/upUpdateCart",
                    "format"=> "json"));


    $password = md5($this->secret.$access_token);//If your shopify app is public
    $baseurl = "https://".$this->api_key.":".$password."@".$shop_domain."/";

    $url = $baseurl.ltrim($path, '/');
    $query = in_array($method, array('GET','DELETE')) ? $params : array();
    $payload = in_array($method, array('POST','PUT')) ? stripslashes(json_encode($params)) : array();
    $request_headers = in_array($method, array('POST','PUT')) ? array("Content-Type: application/json; charset=utf-8", 'Expect:') : array();
    $request_headers[] = 'X-Shopify-Access-Token: ' . $access_token;
                list($response_body, $response_headers) = $this->Curl->HttpRequest($method, $url, $query, $payload, $request_headers);


    $this->last_response_headers = $response_headers;
    $response = json_decode($response_body, true);

    if (isset($response['errors']) or ($this->last_response_headers['http_status_code'] >= 400))
        $body = $response['errors'];
    else
        $body = $response_body;
    /*Debug the output in a text_file*/
    $destination = realpath('../../app/webroot/execution_log') . '/';
    $fh = fopen($destination."shopify_app.txt",'a') or die("can't open file");
    date_default_timezone_set('GMT');

    fwrite($fh, "\n\nDATE: ".date("Y-m-d H:i:s")."\n".$body);
    fclose($fh);
    /*Debug Code Ends*/
    return (is_array($response) and (count($response) > 0)) ? array_shift($response) : $response;
}

and I called this function when I visit my app dashboard mean Controller : Offers function :dashboard

But its not seems to work because when I visit https://test.myshopify.com/admin/webhooks.json its showing nothing but If I am creating webhook through Admin->Setting->Notification then it show listing here https://test.myshopify.com/admin/webhooks.json

Please let me know how we can create webhook using API (cake php )

1 Answer 1

2

Shopify shows the list of webhooks through webhooks.json, those are created manually from the shopify admin. If you want to get the list of webhooks created through api then you need to run it from another browser or from a private browser (where shopify admin is not loged in)

Your link will be something like this - https://api-key:[email protected]/admin/webhooks.json

Note: replace api key and password of your app and replace shop name in the link and try it in a new/private browser window.

Sign up to request clarification or add additional context in comments.

2 Comments

is there a way we can add sone script on thanku page after order placed
I don't think you will be able to customize anything in the think page.(through coding) because thank you page belongs to checkout.shopify.com domain and shopify does not allow to modify anything there. But its my thought..there might be a way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.