【问题标题】:How can I programmatically set the value for a field in a Commerce Product custom Line Item Type?如何以编程方式为 Commerce Product 自定义行项目类型中的字段设置值?
【发布时间】:2014-12-26 12:55:49
【问题描述】:

我设置了一个产品,它使用称为“自定义备注”的自定义订单项类型(通过 UI 中的配置 > 订单项类型创建)。结帐时显示的“自定义注释”中的字段之一是“field_notes”文本区域字段。它按预期工作...在产品显示中,我可以添加一些自定义文本,单击“订购”,然后将注释带到结帐。

但是,我需要以编程方式创建这些订单。我已经到了使用 hook_menu 提交订单的地步,而且效果很好。唯一的问题是我似乎无法为“field_notes”设置值。

// Load whatever product represents the item the customer will be
// paying for and create a line item for it.    
$line_item = commerce_product_line_item_new($product, $quantity, $order->order_id);

// Save the line item to get its ID.
commerce_line_item_save($line_item);


//***this is the part that's not working. trying to set the field_notes field***
$line_item_wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);
$line_item_wrapper->field_notes->set("Does this work??");


// Save the line item to get its ID.
commerce_line_item_save($line_item);

// Add the line item to the order using fago's rockin' wrapper.
$order_wrapper = entity_metadata_wrapper('commerce_order', $order);
$order_wrapper->commerce_line_items[] = $line_item;

// Save the order again to update its line item reference field.
commerce_order_save($order);

我做错了什么?谢谢!

【问题讨论】:

    标签: drupal-7 custom-fields drupal-commerce


    【解决方案1】:

    我认为这可能是因为您没有将正确的订单项类型传递给 commerce_product_line_item_new。例如,我会这样做:

    $line_item = commerce_product_line_item_new(
      $product,
      $quantity = 1,
      $order_id = 0,
      $data = array(),
      $line_item_type = 'my_custom_line_item_type'
    );
    

    【讨论】:

    • 就是这样!非常感谢!
    猜你喜欢
    • 2017-08-08
    • 2020-11-10
    • 1970-01-01
    • 1970-01-01
    • 2011-11-11
    • 1970-01-01
    • 1970-01-01
    • 2016-12-28
    • 1970-01-01
    相关资源
    最近更新 更多