php 我如何才能得到只有在Shopify使用graphql和laravel应用程序的产品编号id

g0czyy6m  于 2023-09-29  发布在  PHP
关注(0)|答案(1)|浏览(84)

这是我的路线

Route::get('/carts/{numericProductID}', 'App\Http\Controllers\ShopifyController@showProductDetails')
->name('showProductDetails');

此链接用于重定向到另一个页面,以显示/获取所选产品的详细信息。

<a href="{{ route('showProductDetails', ['numericProductID' => $product['node']['id']]) }}">View Details</a>
bt1cpqcv

bt1cpqcv1#

试试这个

use Shopify;
use Illuminate\Http\Request;

public function showProductDetails(Request $request, $numericProductID)
{
    // Build your GraphQL query to fetch the product details
    $query = <<<GRAPHQL
    {
        product(id: $numericProductID) {
            id
            title
            # Add other fields you want to retrieve
        }
    }
    GRAPHQL;

    // Make the GraphQL request to Shopify
    $response = Shopify::graph($query);

    // Parse the response JSON
    $productData = json_decode($response->getBody(), true);

    // Extract the numeric product ID
    $numericID = $productData['data']['product']['id'];

    // You can now use $numericID in your view or return it as needed
    // For example, return a view with the product details
    return view('product_details', ['product' => $productData['data']['product']]);
}

在查看文件中

<a href="{{ route('showProductDetails', ['numericProductID' => $product['id']]) }}">View Details</a>

相关问题