Shopify REST API无法添加product.json我的应用程序块



在我的主题应用程序扩展中

<span style="color: {{ block.settings.color }}">
Hello world
</span>
{% schema %}
{
"name": "Hello World",
"target": "section",
"stylesheet": "app.css",
"javascript": "app.js",
"settings": [
{ "label": "Color", "id": "color", "type": "color", "default": "#000000" }
]
}
{% endschema %}

推送扩展并创建版本。当我手动编辑主题时,应用程序块是可见的。但我无法从php在product.json中编辑它。

$user = User::where('store', $this->store)->first();
$headers =  array('X-Shopify-Access-Token: ' . $user->access_token,'Content-Type: application/json', 'Accept: application/json');
$config = array(
'ShopUrl' => $this->store,
'AccessToken' => $user->access_token,
);

PHPShopifyShopifySDK::config($config);
$shopify = new PHPShopifyShopifySDK;
// Get store themes
$themes = $shopify->Theme->get();

foreach ($themes as $theme) {
if ($theme['role'] === 'main') {
$theme_id = $theme['id'];
}
}
// Get store themes
$assets = $shopify->Theme($theme_id)->Asset->get(["asset[key]" => "templates/product.json"]);

$product_json = json_decode($assets['asset']['value']);
$product_json->sections->main->blocks->{"d8a2f128-c540-411d-adb6-bb5e0ae9aea1"} = (object) [
'type' => 'shopify://apps/product-reviews/blocks/product_reviews/7ef9607f-87c1-495e-b328-40a9baa6d672',
'settings' => (object) [
'color' => 3
]
];

array_keys($product_json->sections->main->block_order);
foreach ($product_json->sections->main->block_order as $k => $v) {
if ($v === 'price') {
$output = array_splice(
$product_json->sections->main->block_order, 
$k + 1, 
0, 
(object) array("d8a2f128-c540-411d-adb6-bb5e0ae9aea1")
);
}
}
$replace = [
'asset' => [
'key' => 'templates/product.json',
'value' => json_encode($product_json)
]];

$ch = curl_init("https://$user->store/admin/api/2022-10/themes/$theme_id/assets.json");
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($replace));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);

我收到成功回复

{"asset":{"key":"templates/product.json","public_url":null,"created_at":"2022-10-23T20:45:31+03:00","updated_at":"2022-11-10T23:11:48+02:00","content_type":"application/json","size":5486,"checksum":"49020edbf9140badaae714935705f800","theme_id":136707506477,"warnings":[]}}

当我打开产品时,它不会被编辑。同样在template/product.json中,我的应用程序块丢失。如果我尝试删除未由我添加的现有块,它会起作用。

我正试图在templates/product.json 中的价格之后添加我的应用程序块

我不熟悉Shopify,但我确实看到了一些东西

此代码不起任何作用:

array_keys($product_json->sections->main->block_order);

我不得不假设你打算使用json_encode()

$product_json = json_decode($assets['asset']['value']);

唯一的其他建议是检查$replace'的内容

var_export($replace);

最新更新