/** * Generate a new commerce product object * @return integer, $new_product->product_id; */ function _save_product($param_array = NULL) { $output = NULL; if (!$param_array) { drupal_set_message(t('Please check your parameter')); return $output; } if (!is_array($param_array)) { drupal_set_message(t('your parameter is not array')); return $output; } global $user; $new_product = commerce_product_new($param_array['product_type']); // product type $new_product->sku = $param_array['time_key'] . '-' . $user->uid; $new_product->title = $param_array['title']; $new_product->language = LANGUAGE_NONE; $new_product->uid = $user->uid; $new_product->status = 1; // $new_product->created = time (); // $new_product->changed = time (); $new_product->commerce_price[LANGUAGE_NONE][0] = array('amount' => $param_array['amount'] * 100, 'currency_code' => "CNY"); $product->field_product_brand[LANGUAGE_NONE][0]['target_id'] = $param_array['brand_tid']; // $product->field_product_description[LANGUAGE_NONE][0]['value'] = 22; commerce_product_save($new_product); // $output = $new_product->product_id; $output = $new_product; return $output; }
/** * Default constructor for the Commerce Product object. Do not call this * class directly. Create a separate class for each product type and use its * constructor. * * @param int $product_id * Product id if an existing product is to be loaded. */ public function __construct($product_id = NULL) { $class = new \ReflectionClass(get_called_class()); $type = Utils::makeSnakeCase($class->getShortName()); if (!is_null($product_id)) { $product = NULL; if (is_numeric($product_id)) { $product = commerce_product_load($product_id); } if ($product && $product->type == $type) { parent::__construct($product); return; } // SKU might have been passed instead. $product = commerce_product_load_by_sku($product_id); if ($product && $product->type == $type) { parent::__construct($product); return; } if (!$product) { $this->setErrors("Product with id or sku {$product_id} and type {$type} does not exist."); $this->setInitialized(FALSE); return; } } else { $product = commerce_product_new($type); parent::__construct($product); } }
<section class="<?php print $class; ?> "> <div> <p>Let's try to add a field</p> </div> <?php require_once drupal_get_path('module', 'commerce_product') . '/includes/commerce_product.forms.inc'; require_once drupal_get_path('module', 'commerce_product') . '/includes/commerce_product_ui.products.inc'; $form = commerce_product_ui_product_form_wrapper(commerce_product_new('product')); dpr($form); print render($form); ?> </section>
$tovar = commerce_product_load($product_id->product_id); $form_state = array(); $form_state['values'] = array(); $form = array(); $form['#parents'] = array(); $price = array(LANGUAGE_NONE => array(0 => array('amount' => $item->Цены->Цена->ЦенаЗаЕдиницу * 100, 'currency_code' => 'RUB'))); $form_state['values']['commerce_price'] = $price; field_attach_submit('product', $tovar, $form, $form_state); commerce_product_save($tovar); } else { //добавляем товар если его нет $form_state = array(); $form_state['values'] = ''; $form = array(); $form['#parents'] = array(); $new_product = commerce_product_new('product'); $new_product->status = 1; $new_product->uid = 1; $new_product->sku = $item->Ид; $new_product->title = $item->Наименование; $new_product->type = 'product'; $new_product->created = $new_product->changed = time(); $new_product->language = LANGUAGE_NONE; $new_product->commerce_price['und'][0]['amount'] = $item->Цены->Цена->ЦенаЗаЕдиницу * 100; $new_product->commerce_price['und'][0]['currency_code'] = 'RUB'; $new_product->commerce_price['und'][0]['data']['component'] = array(); field_attach_submit('product', $new_product, $form, $form_state); commerce_product_save($new_product); $node = new stdClass(); $node->type = 'product'; node_object_prepare($node);
/** * Asserts that a given commerce_product type is editable. * * @Then /^I should be able to edit (?:a|an) "([^"]*)" product$/ */ public function assertEditProductOfType($type) { $product = commerce_product_new($type); $random = new Random(); $product->title = $random->name(); $product->sku = $random->name(); commerce_product_save($product); $this->products[] = $product; // Set internal browser on the node edit page. $this->getSession()->visit($this->locatePath('/admin/commerce/products/' . $product->product_id . '/edit')); }