Questions and discussions about using the Shopify CLI and Shopify-built libraries.
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 ?
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.
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 !
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.
Hello Zack,
just as a side note: I'm not an employee or "official person" of shopify.
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>
);
}
}