Shopify app block

2024년 4월 9일 화요일

Today I Learned

날짜

2024년 4월 9일 화요일

내용

고객들의 기능 활성화 여부를 체크하는 기능을 만들었다.

코어스크립트

고객이 위젯을 스토어에 설치하기 위해선, “코어 스크립트”를 활성화 해야한다. 위젯이 정상적으로 작동하도로록 해준다. 우리 서비스에서 위젯은 핵심적인 부분이기 떄문에 코어스크립트가 활성화되어 있지 않다는 의미는 우리 서비스를 제대로 활용하지 못하는 뜻이다.

각 서비스(알파리뷰, 리스트 디자이너)는 제각각 코어스크립트를 가지는데 이를 활성화했는지를 알아봐야한다. 스토어가 사용하고 있는 메인 테마를 찾아야 한다. Shopify로 요청을 보내면 된다.

1
2
3
4
url_themes = (
    f"https://{shop.platform_shop_url}/admin/api/{API_VERSION}/themes.json"
)
res_themes = requests.get(url=url_themes, headers=headers)

이 요청으로 스토어가 가진 모든 테마를 불러오는데, 여기서 탐색을 통해 메인 테마를 찾을 수 있다. 찾고 나면 해당 테마 내의 settings_data.json 을 불러온다.

1
2
url_asset = f"https://{shop.platform_shop_url}/admin/api/{API_VERSION}/themes/{target_theme_id}/assets.json?asset[key]=config/settings_data.json"
res_asset = requests.get(url=url_asset, headers=headers)

config안에 있는 settings_data.json에는 추가된 적이 있는 app_embed를 볼 수 있다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
"blocks": {
      "고유 id": {
        "type": "shopify:\/\/apps\/{App 이름}\/blocks\/{설정한 이름1}\/{App ID}",
        "disabled": false,
        "settings": {
        }
      },
      "고유 id": {
        "type": "shopify:\/\/apps\/{App 이름}\/blocks\/{설정한 이름2}\/{App ID}",
        "disabled": true,
        "settings": {
        }
      },
      ...
}

이렇게 목록들이 나타나니 원하는 값을 찾으면 된다. 한번도 활성화한 적이 없는 항목은 나타나지 않는다. 따라서 없거나, “disabled”가 true일 떄가 비활성화인 상태다.

앱블록 체크

1
2
3
4
5
6
7
8
9
10
INFO:root:{'type': 'apps', 'blocks': 
{'고윳값(1)': {'type': 'shopify://apps/alphareview-product-reviews/blocks/board-review/app-id값', 'settings': {'review_length_per_page': 5, 'widget_title': 'Reviews', 'pagination': 'INDICATOR', 'write_message': 'true', 'use_review_store': 'false', 'customer_name': 'true', 'review_product_desc': 'true', 'review_title': 'true', 'review_rating': 'true', 'padding_top': 10, 'padding_bottom': 0, 'padding_x': 0}}, 
'고윳값(2)': {'type': 'shopify://apps/alphareview-product-reviews/blocks/simple-photo/app-id값', 'settings': {'review_length_per_page': 6, 'widget_title': 'Gallery', 'pagination': 'SLIDE', 'write_message': 'false', 'is_photo': 'true', 'is_video': 'true', 'simple_info': 'true', 'padding_top': 10, 'padding_bottom': 0, 'padding_x': 0}}, 
'고윳값(3)': {'type': 'shopify://apps/clickme-click-me/blocks/app-block/app-id값', 'settings': {}}, '2a38f967-6b61-4852-9d67-40217d5687ab': {'type': 'shopify://apps/alpha-review-test/blocks/rating/dd2c5bb8-24e8-4acf-9889-fd1a5e80e0e2', 'settings': {'widget_title': 'Rating', 'use_review_store': 'false', 'active_graph': 'true', 'active_write_btn': 'true', 'padding_top': 10, 'padding_bottom': 0, 'padding_x': 0}}, 
'고윳값(4)': {'type': 'shopify://apps/alphareview-product-reviews/blocks/rating/app-id값', 'settings': {'widget_title': 'Rating', 'use_review_store': 'false', 'active_graph': 'true', 'active_write_btn': 'true', 'padding_top': 10, 'padding_bottom': 0, 'padding_x': 0}}, 
'고윳값(5)': {'type': 'shopify://apps/alphareview-product-reviews/blocks/store-review/app-id값', 'settings': {'review_length_per_page': 3, 'widget_title': 'Store Reviews', 'pagination': 'SLIDE', 'write_message': 'false', 'customer_name': 'true', 'review_title': 'true', 'review_rating': 'true', 'padding_top': 10, 'padding_bottom': 0, 'padding_x': 0}}}, 
'block_order': ['고윳값(1)', '고윳값(2)', '고윳값(3)', '고윳값(4)', '고윳값(5)'], 
'settings': {'include_margins': True}}

Shopify에 요청을 보내면 이런식의 데이터를 받을 수 있다. type 내에서 어떤 위젯인지 확인할 수 있다. app embed와 달리 비활성화하면 아예 나타나지 않는다.

따라서 로직은

  1. Shopify에서 앱블록 데이터를 가져온다.
  2. 기존에 서버에 저장된 데이터를 확인하여 여전히 활성화 되어있는 앱블록을 확인한다.
  3. 없으면 제외한다.
  4. 새로 추가된 앱블록이 있으면 등록하고 시간을 기록한다.

방식으로 구현했다. 아무래도, 모든 Shop의 앱블록을 다 확인하다보니 너무 무거운 것 같아 하루에 한번만 실행되도록 처리했다.

회고

기획을 제대로 파악하지 않아 괜히 일을 키웠다. 물어볼껄.