Re: How to get shop name inside embedded app.

How to get shop name inside embedded app.

zack_dev
Shopify Partner
75 14 10

Hi I am following https://www.sitepoint.com/shopify-app-development-made-simple/ tutorial.

I have obtained access token and stored it in database after install the app goes to myapp page.

Lets say user has already installed the app and goes to my app . How can I get the shop name in my embedded app so I can get its accesstoken and perform requests.

Since the app already is in iframe and to get access token from database I have to use shop name but how can I get that in iframe ?



- Helpful? Like or Accept solution, - Buy me Coffee


-

Contact me

Replies 5 (5)

Felix2
Shopify Partner
98 0 17

You should see a GET request and its parameters. One of the parameters is the shop name.

In the tutorial, it's done in the following line:

1. $query = $_GET; 
2. $store = $query['shop'];

So $store is the shop name.

Looga.io

zack_dev
Shopify Partner
75 14 10

Thanks a lot Felix !

There is no good php api for detailed stuff support is good only for ruby on rails . I had figued this by checking there were iframe parameters but it good to be confident about things from a official person !



- Helpful? Like or Accept solution, - Buy me Coffee


-

Contact me

soorajlal
Visitor
1 0 0
if (isset($_SERVER['HTTP_REFERER'])) {
	$referalUrl = $_SERVER['HTTP_REFERER']; 
	$qs = parse_url($referalUrl, PHP_URL_QUERY);
	if (!empty($qs)) {
	    parse_str($qs, $output);
	    print_r($output);
	    $shop = preg_replace('/.myshopify.com$/', '', $output['shop']); //get store name without fullurl
	    // TODO check for key existence
	    $_SESSION['shop'] = $shop; 
	}

}

This code will return shop name everytime you logged in. Hope this will help anyone. This code works for me. 


$_SESSION['shop'] = $_GET['shop'];

works for first time only. When you logged in after logout, it doesnot return any value. 

Felix2
Shopify Partner
98 0 17

Hello Zack,

just as a side note: I'm not an employee or "official person" of shopify.

Looga.io

rohit_martires
Shopify Partner
49 3 7

Hey so to get the shop if ur using the next.js boilerplate code from shopify-cli

go to pages -> _app.js,  u'll get it in the ctx.query object in the getInitialProps method so just return it 

MyApp.getInitialProps = async ({ ctx }) => {
  return {
    host: ctx.query.host,
    shop: ctx.query.shop
  };
};

 

now u should be able to access it from props of MyApps class

class MyApp extends App {
  render() {
    const { Component, pageProps, host, shop } = this.props;
    return (
      <AppProvider i18n={translations}>
        <Provider
          config={{
            apiKey: API_KEY,
            host: host,
            forceRedirect: true,
          }}
        >
          <MyProvider Component={Component} shop={shop} {...pageProps} />
        </Provider>
      </AppProvider>
    );
  }
}