以编程方式添加新产品包Drupal 7 + drupal_commerce + commerce_bundle模块

问题描述

我正在将drupal 7与commerce和commerce_bundle模块一起使用

我正在尝试以编程方式向商业产品中添加新的捆绑产品,但我找不到方法。我可以添加产品本身,但是捆绑中的商品没有添加,捆绑产品中没有捆绑商品。

有人可以帮助我吗?

这是我当前编写的代码,但失败了。

      $new_product = commerce_product_new('commerce_bundle_group');
      $form_state = array();
      $form_state['values'] = $values;
      $form = array();
      $form['#parents'] = array();
      $new_product->sku = 'asdasd';
      $new_product->title = 'asdasd';
      $form_state['values']['commerce_bundle_items']['und']['form']['commerce_bundle_product']['und'][0]['target_id'] = 'XXX';
      $form_state['values']['commerce_bundle_items']['und']['form']['commerce_bundle_price']['und'][0]['amount'] = 12;
      $form_state['values']['commerce_bundle_items']['und']['form']['commerce_bundle_price']['und'][0]['currency_code'] = 'USD';

      field_attach_submit('commerce_product',$new_product,$form,$form_state);
      commerce_product_save($new_product);

请注意,commerce_bundle模块正在使用entityreference模块来引用将要添加到commerce_product中的产品。

这是在commerce_bundle模块中设置字段的方式,可以帮助您确定解决方案:

$entity_type = 'commerce_bundle_item';

  // Fields
  $fields = array(
    'commerce_bundle_product' => array(
      'type' => 'entityreference','cardinality' => 1,'translatable' => FALSE,'locked' => TRUE,'settings' => array(
        'target_type' => 'commerce_product','handler' => 'base','handler_settings' => array(),),'commerce_bundle_price' => array(
      'type' => 'commerce_price',);

  // Field instances.
  $instances = array(
    'commerce_bundle_product' => array(
      'label' => t('Product'),'required' => TRUE,'settings' => array(),'widget' => array(
        'type' => 'entityreference_autocomplete','module' => 'entityreference','weight' => -5,'display' => array(
        'default' => array(
          'label' => 'hidden','type' => 'entityreference_label','settings' => array(
            'link' => TRUE,'commerce_bundle_price' => array(
      'label' => t('Price'),'required' => FALSE,// Because this widget is locked,we need it to use the full price widget
      // since the currency option can't be adjusted at the moment.
      'widget' => array(
        'type' => 'commerce_price_full','weight' => -4,'settings' => array(
          'currency_code' => 'default','type' => 'commerce_price_formatted_amount','settings' => array(
            'calculation' => FALSE,);

如果有人知道我该怎么做,请分享

谢谢。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)