繁体   English   中英

使用Drupal Api将图像附加到产品

[英]Attach Image to Product with Drupal Api

我试图以编程方式将产品添加到我的drupal商业商店。 到目前为止,我已经能够添加包含基本信息(例如sku,title和price)的产品。 我如何使用drupal的api添加字段数据,例如图像和其他字段类型。

我用来添加产品的代码来自commerce_examples / product_example模块。

<?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;
}

这些示例使用相同的格式添加价格数据:

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

我能够添加其他字段数据,但是我仍然无法在默认情况下将数据添加到所有商业产品随附的field_productimage字段中。

我可以建议您有关其他领域的数据,例如与产品相关的额外信息。 您可以从UI将这些字段添加到产品类型,或者如果您通过编程方式创建产品类型,则也可以执行相同的操作。

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

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

通过$form_state()在函数product_example_create_product()获取这些数据。

将它们添加到$ 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;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM