修改PHP fetchProducts函数添加仓库ID=2报错,求解决方法
仓库ID=2同步代码添加后报错问题求助
我是编程新手,现有可正常运行的PHP函数fetchProducts,用于同步仓库ID=7的商品数据(含参考ID、库存、名称、价格等)。尝试添加仓库ID=2的同步代码片段后出现错误,以下是原函数及新增代码,求协助解决。
原函数代码
public function fetchProducts($syncAll = true) { $this->library = new B1([ 'apiKey' => $this->settings->get('api_key'), 'privateKey' => $this->settings->get('private_key') ]); $i = 0; $enableQuantitySync = $this->settings->get('quantity_sync'); $enableSyncItemName = $this->settings->get('sync_item_name'); $enableSyncItemPrice = $this->settings->get('sync_item_price'); $this->items->resetAllB1ReferenceId(); $page = 0; $warehouseId = 7; $data = []; do { $i++; try { $response = $this->library->request('e-commerce/items/stock', [ 'warehouseId' => $warehouseId, 'page' => ++$page, 'pageSize' => 100, 'filters' => [ 'groupOp' => 'AND', 'rules' => [], ], ]); $data = $response->getContent(); foreach ($data['data'] as $item) { if ($item['code'] != NULL) { $this->items->updateProductReferenceId($item['id'], $item['code']); if ($enableQuantitySync == 1) { $this->items->updateProductQuantity2($item['quantity'], $item['id']); } if ($enableSyncItemName == 1) { $this->items->updateItemName($item['name'], $item['id']); } if ($enableSyncItemPrice == 1) { $this->items->updateItemPrice($item['priceWithoutVat'], $item['id']); } } } } catch (B1Exception $e) { $content = $e->getResponse()->getContent(); $this->helper->printPre($content['message']); } catch (B1DuplicateException $e) { $content = $e->getResponse()->getContent(); $this->helper->printPre($content['message']); } catch (B1ValidationException $e) { $content = $e->getResponse()->getContent(); $this->helper->printPre($content['message']); } catch (B1ResourceNotFoundException $e) { $content = $e->getResponse()->getContent(); $this->helper->printPre($content['message']); } catch (B1InternalErrorException $e) { $content = $e->getResponse()->getContent(); $this->helper->printPre($content['message']); } echo "$i;"; } while (isset($data['pages']) && $page <= $data['pages'] && $i < self::MAX_ITERATIONS); echo 'OK'; }
新增代码片段
$warehouseId = 2; $data = []; do { $i++; try { $response = $this->library->request('e-commerce/items/stock', [ 'warehouseId' => $warehouseId, 'page' => ++$page, 'pageSize' => 100, 'filters' => [ 'groupOp' => 'AND', 'rules' => [], ], ]); $data = $response->getContent(); foreach ($data['data'] as $item) { if ($item['code'] != NULL) { $this->items->updateProductReferenceId2($item['id'], $item['code']); if ($enableQuantitySync == 1) { $this->items->updateProductQuantity2($item['quantity'], $item['id']); } if ($enableSyncItemName == 1) { $this->items->updateItemName2($item['name'], $item['id']); } if ($enableSyncItemPrice == 1) { $this->items->updateItemPrice2($item['priceWithoutVat'], $item['id']); } } } }
问题分析与修正方案
主要错误点
- 语法不完整:新增代码的
do-while循环未闭合,且缺少异常捕获的catch块,直接导致代码无法解析。 - 变量未重置:仓库7同步完成后,
$page变量未重置为0,仓库2的同步会从上次的页码继续请求,大概率获取不到数据。 - 重复异常捕获:原函数中存在两次
B1DuplicateException捕获,属于冗余代码,需合并。 - 自定义方法存疑:新增代码中的
updateProductReferenceId2、updateItemName2、updateItemPrice2需确认是否已正确实现,若未定义会触发“未找到方法”错误。
修正后的完整函数
将两个仓库的同步逻辑封装为循环,避免重复代码,同时修复上述问题:
public function fetchProducts($syncAll = true) { $this->library = new B1([ 'apiKey' => $this->settings->get('api_key'), 'privateKey' => $this->settings->get('private_key') ]); $totalIterations = 0; $enableQuantitySync = $this->settings->get('quantity_sync'); $enableSyncItemName = $this->settings->get('sync_item_name'); $enableSyncItemPrice = $this->settings->get('sync_item_price'); $this->items->resetAllB1ReferenceId(); // 需要同步的仓库ID列表 $warehouseIds = [7, 2]; foreach ($warehouseIds as $warehouseId) { $page = 0; $data = []; do { $totalIterations++; try { $response = $this->library->request('e-commerce/items/stock', [ 'warehouseId' => $warehouseId, 'page' => ++$page, 'pageSize' => 100, 'filters' => [ 'groupOp' => 'AND', 'rules' => [], ], ]); $data = $response->getContent(); foreach ($data['data'] as $item) { if ($item['code'] != NULL) { // 根据仓库ID调用对应方法,这里假设方法名后缀对应仓库ID $referenceMethod = $warehouseId == 7 ? 'updateProductReferenceId' : 'updateProductReferenceId2'; $nameMethod = $warehouseId == 7 ? 'updateItemName' : 'updateItemName2'; $priceMethod = $warehouseId == 7 ? 'updateItemPrice' : 'updateItemPrice2'; $this->items->$referenceMethod($item['id'], $item['code']); if ($enableQuantitySync == 1) { $this->items->updateProductQuantity2($item['quantity'], $item['id']); } if ($enableSyncItemName == 1) { $this->items->$nameMethod($item['name'], $item['id']); } if ($enableSyncItemPrice == 1) { $this->items->$priceMethod($item['priceWithoutVat'], $item['id']); } } } } catch (B1Exception $e) { $content = $e->getResponse()->getContent(); $this->helper->printPre($content['message']); } catch (B1DuplicateException $e) { $content = $e->getResponse()->getContent(); $this->helper->printPre($content['message']); } catch (B1ValidationException $e) { $content = $e->getResponse()->getContent(); $this->helper->printPre($content['message']); } catch (B1ResourceNotFoundException $e) { $content = $e->getResponse()->getContent(); $this->helper->printPre($content['message']); } catch (B1InternalErrorException $e) { $content = $e->getResponse()->getContent(); $this->helper->printPre($content['message']); } echo "$totalIterations;"; } while (isset($data['pages']) && $page <= $data['pages'] && $totalIterations < self::MAX_ITERATIONS); } echo 'OK'; }
额外说明
- 如果
updateProductReferenceId2等方法是针对仓库2的自定义逻辑,请确保这些方法已在$this->items对应的类中正确实现。 - 原函数中的
MAX_ITERATIONS常量需确保其值足够覆盖两个仓库的所有分页请求,避免提前终止同步。
内容的提问来源于stack exchange,提问作者turbophp
相关产品推荐
相关产品推荐

