Shopify API创建履约单报错:REST请求返回‘Not Found’
调用Shopify Admin API创建履约单报错问题(Symfony 6 + PHP 8.2)
连接初始化代码
Context::initialize( $this->key, $this->secret, $_ENV['SHOPIFY_API_SCOPES'], $_ENV['SHOPIFY_URL'], new FileSessionStorage('/tmp/php_sessions'), '2023-01', ); $id = uniqid(); $shopDomain = $this->url; $isOnline = false; $state = bin2hex(random_bytes(16)); $session = new \Shopify\Auth\Session($id, $shopDomain, $isOnline, $state); $session->setAccessToken($this->token); $this->test_session = $session;
第一种创建履约单方式(成功)
$fulfillment = Fulfillment::create( $this->test_session, $fulfillmentData, ['order_id' => $order->getId()] );
该方式可正常返回履约单对象。
第二种创建履约单方式(报错)
$fulfillment = new Fulfillment($this->test_session); $fulfillment->message = "The package was shipped this morning."; $fulfillment->notify_customer = false; $fulfillment->tracking_info = [ "number" => 1562678, "url" => "https://www.my-shipping-company.com", "company" => "my-shipping-company" ]; /** @var Product $item */ foreach ($order->getLineItems() as $item) { $fulfillment->line_items_by_fulfillment_order = [ [ "fulfillment_order_id" => $order->getId(), "fulfillment_order_line_items" => [ [ "id" => $item->getId(), "quantity" => 1 ] ] ] ]; } $fulfillment->save( true, // Update Object );
执行此代码时抛出错误:REST request failed: "Not Found"
错误栈信息
REST request failed: "Not Found" Exception Logs 1 Stack Trace Shopify\Exception\RestResourceRequestException in vendor/shopify/shopify-api/src/Rest/Base.php (line 204) in vendor/shopify/shopify-api/src/Rest/Base.php :: request (line 67) Base->save(true) in src/Service/Api/ShopifyApiService.php (line 131) in namespace Shopify\Rest; abstract class Base protected static function request( i dump request before send pa([ $client, $path, $params ]);
请求信息(已脱敏)
0 => Shopify\Clients\Rest {#1989 ▼ -domain: "xxxx.myshopify.com" -accessToken: "xxx" } 1 => "fulfillments.json"
问题原因分析
- save参数错误:
save(true)是更新操作,但当前是创建新履约单,Shopify API更新履约单需要指定履约单ID,直接请求fulfillments.json会返回404 - 履约单ID赋值错误:
fulfillment_order_id用了主订单ID,而非订单对应的履约单ID(主订单和履约单是不同的实体) - 订单项覆盖问题:循环中每次都直接赋值
line_items_by_fulfillment_order,导致最终只保留最后一个订单项
修复方案
$fulfillment = new Fulfillment($this->test_session); $fulfillment->message = "The package was shipped this morning."; $fulfillment->notify_customer = false; $fulfillment->tracking_info = [ "number" => 1562678, "url" => "https://www.my-shipping-company.com", "company" => "my-shipping-company" ]; // 获取订单对应的履约单ID(根据实际业务取对应履约单,这里假设仅一个) $fulfillmentOrderId = $order->fulfillment_orders[0]->id; // 收集所有要履约的订单项 $fulfillLineItems = []; /** @var LineItem $item */ foreach ($order->getLineItems() as $item) { $fulfillLineItems[] = [ "id" => $item->getId(), "quantity" => 1 ]; } $fulfillment->line_items_by_fulfillment_order = [ [ "fulfillment_order_id" => $fulfillmentOrderId, "fulfillment_order_line_items" => $fulfillLineItems ] ]; // 创建新履约单,使用默认的save(false)(或直接调用save()) $fulfillment->save();
内容的提问来源于stack exchange,提问作者Developer
相关产品推荐
相关产品推荐

