【问题标题】:Accessing image field of line items in Drupal 7 module在 Drupal 7 模块中访问行项目的图像字段
【发布时间】:2014-03-12 17:58:35
【问题描述】:

我正在创建自定义电子邮件,以发送给希望与他人共享购物车的客户。到目前为止,我有每个产品的数量、SKU、价格、路径(节点/XYZ)和标题。我在电子邮件中需要的最后一项是产品的图像路径。

我找到了以下所有其他信息:

$order = commerce_cart_order_load($user->uid);
foreach($wrapper->commerce_line_items as $d => $line_item_wrapper) {
    $sku = $line_item_wrapper->line_item_label->value();
    //...

打印出以下内容,我可以看到包装对象的受保护“数据”属性:

print_r($line_item_wrapper->commerce_product);

然后,我尝试使用以下内容查找 field_image 属性的 getter 方法:

print_r($line_item_wrapper->commerce_product->getPropertyInfo('field_image');

我最终得到了一个看似无用的 getter 函数 entity_metadata_field_verbatim_get`,因为我不知道要传递什么参数。此外,在上面的最后一个打印语句中,我没有看到任何其他有价值的东西。

我想知道是否需要查询这些数据,以及要查询哪些表/列?或者也许使用node_load之类的东西?但是,我发现从订单项包装器中找到 nid 并不容易。

【问题讨论】:

  • 如果没记错的话,您应该能够(例如)获得带有$line_item_wrapper->commerce_product->field_image->uri->value()) 的URI。如果图像字段附加了文件,您至少应该有一个文件 ID (fid) $line_item_wrapper->commerce_product->field_image->raw(),然后您可以从那里使用 file_load()
  • 提供的第一个代码示例产生了错误,因为 field_image 没有 ->uri 属性。一旦我从field_image获得了值,我就不得不深入到一个数组中。
  • 没有uri属性,但通常实体模块会自动将图像字段扩展为等效的文件实体,因此可以直接访问其属性/字段。如果这不起作用,则可能表明某些设置不正确

标签: php drupal drupal-7


【解决方案1】:

我能够用这段代码解决上述问题:

$order = commerce_cart_order_load($user->uid);
$wrapper = entity_metadata_wrapper('commerce_order', $order);
$result = array();

foreach($wrapper->commerce_line_items as $d => $line_item_wrapper) {
    $product = array();
    $product['quantity'] = $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'] = $line_item_wrapper->commerce_unit_price->amount->value();
    $product['title'] = $line_item_wrapper->commerce_product->title->value();
    $product['image'] = $line_item_wrapper->commerce_product->field_image->value();
    $product['image'] = $product['image'][0]['filename'];

    array_push($result, $product);
}

我不得不说,找到所有这些离散函数非常困难。我没有找到任何关于 Drupal Commerce metadata_wrappers 的clear文档。如果有人可以提供更多信息,我很乐意看看。

总的来说,我使用了很多print_r(); 来查找这些值。

【讨论】:

  • 安装Devel,使用dpm(),并告别print_r() :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多