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

rhfm7lfc  于 2022-11-13  发布在  其他
关注(0)|答案(1)|浏览(116)

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

<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,
            );
            
            PHPShopify\ShopifySDK::config($config);
            $shopify = new PHPShopify\ShopifySDK;

            // 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中的价格后添加我的应用程序块

ie3xauqp

ie3xauqp1#

我不熟悉Shopify,但我确实看到了一些东西。
此代码不执行任何操作:

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

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

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

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

var_export($replace);

相关问题