0

I am trying to programatically add products to my drupal commerce store. So far I have been able to add products that contain basic information ( such as sku, title, and price ). How would I be able to add field data, such as images and other field types, using drupal's api.

The code I used to add the products is derived from the commerce_examples/product_example module.

<?php
/**
 * @file product_example.module
 * Demonstrates pricing hooks, etc.
 */

/**
 * Implements hook_menu().
 *
 * Simply presents a page that will explain what this module is for.
 * hook_menu() has nothing to do with the checkout page functionality.
 */
function product_example_menu() {
  $items['commerce_examples/product_example'] = array(
    'title' => 'Product Example',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('product_example_info_page'),
    'access callback' => TRUE,
  );
  return $items;
}

/**
 * This form provides a way to interact with some of the example functions
 * provided here.
 */
function product_example_info_page($form, &$form_state) {
  $form['explanation'] = array(
    '#type' => 'item',
    '#markup' => t('This example demonstrates product creation and manipulation.'),
  );

    $form['product_creation'] = array(
      '#type' => 'fieldset',
      '#title' => t('Please create a product for use with this example'),
    );
    $types = commerce_product_types();
    $form['product_creation']['product_type'] = array(
      '#type' => 'select',
      '#title' => t('Product type for product to be created'),
      '#options' => drupal_map_assoc(array_keys($types)),
    );
    $form['product_creation']['title'] = array(
      '#title' => t('Product title'),
      '#type' => 'textfield',
      '#default_value' => t('A dummy product for use with product_example'),
    );
    $form['product_creation']['price'] = array(
      '#title' => t('Product price'),
      '#type' => 'textfield',
      '#description' => t('A price in decimal format, without a currency symbol'),
      '#default_value' => '100.00',
    );
    $form['product_creation']['product_creation_submit'] = array(
      '#type' => 'submit',
      '#value' => t('Create product'),
      '#submit' => array('product_example_product_creation_submit')
    );

  return $form;
}

/**
 * Submit handler for creating a product.
 */
function product_example_product_creation_submit($form, &$form_state) {
  $extras = array(
    'sku' => 'product_example_' . drupal_hash_base64(microtime()),
    'status' => TRUE,
    'uid' => $GLOBALS['user']->uid,
    'title' => $form_state['values']['title'],
  );
  // Use the product example's creation function to create a product.
  $product_id = product_example_create_product($form_state['values']['product_type'], $form_state['values']['price'], $extras);
  drupal_set_message(t('Created sample product with title !title and sku !sku', array('!title' => l($extras['title'], 'admin/commerce/products/' . $product_id), '!sku' => $extras['sku'])));
}

/**
 * Create a product programmatically.
 *
 * This is stolen shamelessly from commerce_bpc. Thanks for the help here!
 *
 * @param $product_type
 *   (string) The name of the product type for which products should be created.
 * @param $price
 *   Decimal amount of price. If additional fields need to be populated they
 *   can be populated in exactly the same way as the commerce_price field.
 * @param $extras
 *   An array for the values of  'extra fields' defined for the product type
 *   entity, or patterns for these. Recognized keys are:
 *   - status
 *   - uid
 *   - sku
 *   - title
 *   Note that the values do NOT come in the form of complex arrays (as they
 *   are not translatable, and can only have single values).
 * @return
 *   The ID of the created product.
 */
function product_example_create_product($product_type, $price, $extras) {
  $form_state = array();
  $form_state['values'] = array();
  $form = array();
  $form['#parents'] = array();

  // Generate a new product object
  $new_product = commerce_product_new($product_type);

  $new_product->status = $extras['status'];
  $new_product->uid = $extras['uid'];

  $new_product->sku = $extras['sku'];
  $new_product->title = $extras['title'];
  $new_product->created = $new_product->changed = time();

  //commerce_price[und][0][amount]
  $price = array(LANGUAGE_NONE => array(0 => array(
    'amount' => $price * 100,
    'currency_code' => commerce_default_currency(),
  )));
  $form_state['values']['commerce_price'] = $price;

  // Notify field widgets to save their field data
  field_attach_submit('commerce_product', $new_product, $form, $form_state);

  commerce_product_save($new_product);
  return $new_product->product_id;
}

Using the same format the examples uses to add price data:

  //commerce_price[und][0][amount]
  $price = array(LANGUAGE_NONE => array(0 => array(
    'amount' => $price * 100,
    'currency_code' => commerce_default_currency(),
  )));

I was able to add other field data, however I am still having trouble adding data to the field_productimage field that all commerce products come with by default.

4

1 回答 1

0

我可以向您推荐其他领域的数据,例如与产品相关的额外信息。您可以从 UI 将这些字段添加到产品类型,或者如果您以编程方式创建产品类型,那么您也可以这样做。

在表单中添加字段以收集数据,例如 -

$form['product_creation']['values'] = array(
      '#title' => t('Some data'),
      '#type' => 'textfield',
      '#description' => t('Dummy data'),
    );

product_example_create_product()通过.在您的函数中获取这些数据$form_state()

将它们添加到 $new_product 对象中,例如

$new_product->field_name['und'][0]['value'] = $extras['values'];

保存产品,就像您正在做的那样。

对于图像,您必须遵循文件附件的方法。喜欢 -

$file_path = drupal_realpath('image/product_image.png');
  $file = (object) array(
    'uid' => 1,
    'uri' => $file_path,
    'filemime' => file_get_mimetype($file_path),
    'status' => 1,
  );

  // We save the file to the root of the files directory.
  $file = file_copy($file, 'public://');

  $new_product->product_image[LANGUAGE_NONE][0] = (array)$file;
于 2013-06-07T13:44:31.160 回答