Drupal глазами разработчика

Блог музицирующего веб-девелопера: Drupal: Программно создать товар в Commerce

Пример программного создания Product и Product Display:
<span class="co1">// Product</span>
<span class="re0">$product</span> <span class="sy0">=</span> commerce_product_new<span class="br0">(</span><span class="st_h">'product'</span><span class="br0">)</span><span class="sy0">;</span>
<span class="re0">$product</span><span class="sy0">-></span><span class="me1">sku</span> <span class="sy0">=</span> <span class="st_h">'PROD-SKU'</span><span class="sy0">;</span>
<span class="re0">$product</span><span class="sy0">-></span><span class="me1">title</span> <span class="sy0">=</span> <span class="st_h">'Product title'</span><span class="sy0">;</span>
<span class="re0">$product</span><span class="sy0">-></span><span class="me1">language</span> <span class="sy0">=</span> LANGUAGE_NONE<span class="sy0">;</span>
<span class="re0">$product</span><span class="sy0">-></span><span class="me1">uid</span> <span class="sy0">=</span> <span class="nu0">1</span><span class="sy0">;</span>
<span class="re0">$product</span><span class="sy0">-></span><span class="me1">commerce_price</span><span class="br0">[</span>LANGUAGE_NONE<span class="br0">]</span><span class="br0">[</span><span class="nu0">0</span><span class="br0">]</span><span class="br0">[</span><span class="st_h">'amount'</span><span class="br0">]</span> <span class="sy0">=</span> <span class="nu0">10</span> <span class="sy0">*</span> <span class="nu0">100</span><span class="sy0">;</span> <span class="co1">// $10</span>
<span class="re0">$product</span><span class="sy0">-></span><span class="me1">commerce_price</span><span class="br0">[</span>LANGUAGE_NONE<span class="br0">]</span><span class="br0">[</span><span class="nu0">0</span><span class="br0">]</span><span class="br0">[</span><span class="st_h">'currency_code'</span><span class="br0">]</span> <span class="sy0">=</span> <span class="st_h">'USD'</span><span class="sy0">;</span>
commerce_product_save<span class="br0">(</span><span class="re0">$product</span><span class="br0">)</span><span class="sy0">;</span>
 
<span class="co1">// Product display</span>
<span class="re0">$node</span> <span class="sy0">=</span> <span class="br0">(</span>object<span class="br0">)</span><span class="kw3">array</span><span class="br0">(</span><span class="st_h">'type'</span> <span class="sy0">=></span> <span class="st_h">'product_display'</span><span class="br0">)</span><span class="sy0">;</span>
node_object_prepare<span class="br0">(</span><span class="re0">$node</span><span class="br0">)</span><span class="sy0">;</span>
<span class="re0">$node</span><span class="sy0">-></span><span class="me1">title</span> <span class="sy0">=</span> <span class="st_h">'Product display title'</span><span class="sy0">;</span>
<span class="re0">$node</span><span class="sy0">-></span><span class="me1">uid</span> <span class="sy0">=</span> <span class="nu0">1</span><span class="sy0">;</span>

Читать дальше →
Похожие записи:
  • нет
  • 0
  • 0

0 комментариев

Только зарегистрированные и авторизованные пользователи могут оставлять комментарии.