Php 签出后在Drupal Commerce中获取行项目
我正在编写一些代码,以便在签出后访问行项目信息,特别是在Php 签出后在Drupal Commerce中获取行项目,php,drupal,Php,Drupal,我正在编写一些代码,以便在签出后访问行项目信息,特别是在hook\u commerce\u checkout\u complete()中 当我在Devel(/Devel/php)中运行以下代码时,它工作得非常好,将订单中的所有行项目加载到$products数组中。但是在钩子内部,它在第一次尝试访问行项目数据时抛出一个错误 $wrapper = entity_metadata_wrapper('commerce_order', $order); $products = array();
hook\u commerce\u checkout\u complete()
中
当我在Devel(/Devel/php)中运行以下代码时,它工作得非常好,将订单中的所有行项目加载到$products
数组中。但是在钩子内部,它在第一次尝试访问行项目数据时抛出一个错误
$wrapper = entity_metadata_wrapper('commerce_order', $order);
$products = array();
foreach ($wrapper->commerce_line_items as $delta => $line_item_wrapper) {
$product = array();
$product['quantity'] = number_format($line_item_wrapper->quantity->value());
$product['sku'] = $line_item_wrapper->line_item_label->value();
$product['path'] = $line_item_wrapper->commerce_display_path->value();
$product['price'] = "$".number_format($line_item_wrapper->commerce_unit_price->amount->value() / 100, 2);
$product['title'] = $line_item_wrapper->commerce_product->title->value();
$product['image'] = $line_item_wrapper->commerce_product->field_image->value();
array_push($products, $product);
}
我尝试用commerce\u order\u load($order->order\u id)
加载订单,但返回的对象与$order参数没有什么不同。此外,钩子中订单的包装器对象没有我使用Devel获得的包装器对象大
更多信息:我正在使用Commerce Kickstart 2.0。另外,对于缓存,我们使用Varnish。我得到的确切错误是:
请参阅accuair_checkout_commerce_checkout_complete()
0/srv/bindings/41e74e13d874402496da2dafa0aea98b/code/profiles/commerce\u kickstart/modules/contrib/entity/includes/entity.wrapper.inc(392):EntityStructureWrapper->getPropertyInfo('commerce\u produc…)
1/srv/bindings/41e74e13d874402496da2dafa0aea98b/code/profiles/commerce\u kickstart/modules/contrib/entity/includes/entity.wrapper.inc(413):entitystructurewapper->get('commerce\u produc…)
2/srv/bindings/41e74e13d874402496da2dafa0aea98b/code/sites/all/modules/accuair\u checkout/accuair\u checkout.module(35):EntityStructureWrapper->\u get('commerce\u produc…)
编辑:我已尝试从$order行项目数组中按ID加载行项目。下面的代码不会产生任何有用的结果(上面我想要的信息)
这可能是因为您在那里有航运公司项目(没有
commerce\u product
字段),因此尝试访问它将引发异常。在访问该成员之前,请尝试检查$line\u item\u wrapper->type->value()
。我想这只是产品项目的“产品”谢谢你的评论。我关心的不是错误,而是那些不存在的产品的数据/字段。我是否需要使用EntityQueries来完成类似的操作?请尝试使用:$line\u item\u wrapper->getBundle()
而不是$line\u item\u wrapper->type->value()
,因为这可能会导致一些问题,例如无法获取数据属性类型,因为父数据结构未设置。
$wrapper = entity_metadata_wrapper('commerce_order', $order);
$order_lead_time = 0;
$products = array();
foreach ($order->commerce_line_items as $delta => $line_item) {
$line_item = commerce_line_item_load($line_item[0]['line_item_id']);
$line_item_wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);
// no data here