Created
September 22, 2023 02:58
-
-
Save wasalwayshere/903def4d3b711f916ad5e49beaa67685 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Controllers; | |
use Illuminate\Http\Request; | |
use App\Http\Controllers\ShopifyGraphQLQuery; | |
use Illuminate\Support\Facades\App; | |
use Log; | |
class ShopifyGraphQLProductAddMedia extends Controller | |
{ | |
/** | |
* Handle the incoming request. | |
* | |
* @param \Illuminate\Http\Request $request | |
* @return \Illuminate\Http\Response | |
*/ | |
public function __invoke($data) | |
{ | |
$shop_url = $data['shop_url']; | |
$ShopifyGraphQLQuery = new ShopifyGraphQLQuery(); | |
$media = $data['images']; | |
$productId = $data['id']; | |
// Gather existing media if any media | |
$query = 'query getProductMedia($productId: ID!) { | |
product(id: $productId) { | |
media(first: 100) { | |
edges { | |
node { | |
... on MediaImage { | |
id | |
} | |
... on Video { | |
id | |
} | |
... on Model3d { | |
id | |
} | |
... on ExternalVideo { | |
id | |
} | |
} | |
} | |
} | |
} | |
} | |
'; | |
$variables = [ | |
'productId' => $productId, | |
]; | |
$response = $ShopifyGraphQLQuery->__invoke($shop_url, $query, $variables); | |
if (App::environment(['local', 'staging', 'testing'])) { | |
Log::info('ShopifyGraphQLProductAddMedia-found:', ['$response' => $response]); | |
} | |
$mediaIds = collect([]); | |
$mediaIds = isset($response['data']['product']['media']['edges']) ? collect($response['data']['product']['media']['edges'])->pluck('node.id') : collect([]); | |
if (App::environment(['local', 'staging', 'testing'])) { | |
Log::info('ShopifyGraphQLProductAddMedia-found:', ['$mediaIds' => $mediaIds->toArray()]); | |
} | |
// Delete the media if found | |
if ($mediaIds->isNotEmpty()) { | |
$query = 'mutation productDeleteMedia($mediaIds: [ID!]!, $productId: ID!) { | |
productDeleteMedia(mediaIds: $mediaIds, productId: $productId) { | |
deletedMediaIds | |
deletedProductImageIds | |
mediaUserErrors { | |
# MediaUserError fields | |
code | |
field | |
message | |
} | |
product { | |
# Product fields | |
id | |
} | |
} | |
}'; | |
$variables = [ | |
'productId' => $productId, | |
'mediaIds' => $mediaIds->toArray(), | |
]; | |
$response = $ShopifyGraphQLQuery->__invoke($shop_url, $query, $variables); | |
if (App::environment(['local', 'staging', 'testing'])) { | |
Log::info('ShopifyGraphQLProductAddMedia-delete:', ['$response' => $response]); | |
} | |
} | |
// Now add the media from the request | |
$query = 'mutation productCreateMedia($media: [CreateMediaInput!]!, $productId: ID!) { | |
productCreateMedia(media: $media, productId: $productId) { | |
media { | |
alt | |
mediaContentType | |
} | |
mediaUserErrors { | |
field | |
message | |
} | |
product { | |
id | |
title | |
} | |
} | |
}'; | |
$variables = [ | |
'productId' => $productId, | |
'media' => $media | |
]; | |
$response = $ShopifyGraphQLQuery->__invoke($shop_url, $query, $variables); | |
if (App::environment(['local', 'staging', 'testing'])) { | |
Log::info('ShopifyGraphQLProductAddMedia:', ['$response' => $response]); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment