1

我正在使用此示例代码在 prestashop 中添加产品,但我不知道如何使用此产品添加产品属性,如数量、颜色、尺寸。如果未添加该属性,我有我想与该产品一起添加的颜色和尺寸数组,首先添加这些属性,然后与该产品链接。

<?php
define('DEBUG', false);
define('_PS_DEBUG_SQL_', false);
define('PS_SHOP_PATH', 'http://localhost/prestashop_demo');
define('PS_WS_AUTH_KEY', '2LM2T4H56352K1L4FZ40V9H6UPPQD4WE');
require_once ('PSWebServiceLibrary.php');
//include_once ('core_config.php');


$webService = new PrestaShopWebservice(PS_SHOP_PATH, PS_WS_AUTH_KEY, DEBUG);

function PS_new_product($n_id_category_default, $n_id_category, $n_price, $n_active,$n_avail4order,$n_show_price, $n_id_stock_availables ,$n_id_id_product_attribute, $n_l_id, $n_name, $n_desc, $n_desc_short, $n_link_rewrite, $n_meta_title, $n_meta_description, $n_meta_keywords,$n_available_now,$n_available_later) {

    global $webService;

    $xml = $webService -> get(array('url' => PS_SHOP_PATH . '/api/products?schema=blank'));
    $resources = $xml -> children() -> children();
    unset($resources -> id);
    unset($resources -> position);
    unset($resources -> id_shop_default);
    unset($resources -> date_add);
    unset($resources -> date_upd);

    unset($resources->associations->combinations);
    unset($resources->associations->product_options_values);
    unset($resources->associations->product_features);
    unset($resources->associations->stock_availables->stock_available->id_product_attribute);

    //unset($resources->associations->categories->category->id);

    //unset($resources-> id_category_default);

    //$resources->position_in_category = '0';
    //unset($resources->position_in_category);

    //$resources -> position = '0';
    //$resources -> id;
    $resources-> id_manufacturer = '1';
    $resources-> id_supplier = '1';
    $resources-> id_category_default = $n_id_category_default;
    $resources-> new = '0'; ; //condition, new is also a php keyword!!
    $resources-> cache_default_attribute;
    $resources-> id_default_image;
    $resources-> id_default_combination = '0';
    $resources-> id_tax_rules_group ='1';
    //$resources-> id_shop_default;
    //$resources-> quantity = '50';
    $resources-> reference;
    $resources-> supplier_reference;
    $resources-> location;
    $resources-> width;
    $resources-> height;
    $resources-> depth;
    $resources-> weight;
    $resources-> quantity_discount;
    $resources-> ean13;
    $resources-> upc;
    $resources-> cache_is_pack;
    $resources-> cache_has_attachments;
    $resources-> is_virtual;
    $resources-> on_sale;
    $resources-> online_only;
    $resources-> ecotax;
    $resources-> minimal_quantity = 10;
    $resources-> price = $n_price;
    $resources-> wholesale_price;
    $resources-> unity;
    $resources-> unit_price_ratio;
    $resources-> additional_shipping_cost;
    $resources-> customizable;
    $resources-> text_fields;
    $resources-> uploadable_files;
    $resources-> active = $n_active;
    $resources-> available_for_order = $n_avail4order;
    $resources-> available_date;
    $resources-> condition;
    $resources-> show_price = $n_show_price;
    $resources-> indexed = '1';
    $resources-> visibility = 'both';
    $resources-> advanced_stock_management='0';
    $resources-> date_add;
    $resources-> date_upd;

    $resources->associations->categories->addChild('category')->addChild('id', $n_id_category);

    $node = dom_import_simplexml($resources -> name -> language[0][0]);
    $no = $node -> ownerDocument;
    $node -> appendChild($no -> createCDATASection($n_name));
    $resources -> name -> language[0][0] = $n_name;
    $resources -> name -> language[0][0]['id'] = $n_l_id;
    $resources -> name -> language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/' . $n_l_id;

    $node = dom_import_simplexml($resources -> description -> language[0][0]);
    $no = $node -> ownerDocument;
    $node -> appendChild($no -> createCDATASection($n_desc));
    $resources -> description -> language[0][0] = $n_desc;
    $resources -> description -> language[0][0]['id'] = $n_l_id;
    $resources -> description -> language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/' . $n_l_id;

    $node = dom_import_simplexml($resources -> description_short -> language[0][0]);
    $no = $node -> ownerDocument;
    $node -> appendChild($no -> createCDATASection($n_desc_short));
    $resources -> description_short -> language[0][0] = $n_desc_short;
    $resources -> description_short -> language[0][0]['id'] = $n_l_id;
    $resources -> description_short -> language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/' . $n_l_id;

    $node = dom_import_simplexml($resources -> link_rewrite -> language[0][0]);
    $no = $node -> ownerDocument;
    $node -> appendChild($no -> createCDATASection($n_link_rewrite));
    $resources -> link_rewrite -> language[0][0] = $n_link_rewrite;
    $resources -> link_rewrite -> language[0][0]['id'] = $n_l_id;
    $resources -> link_rewrite -> language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/' . $n_l_id;

    $node = dom_import_simplexml($resources -> meta_title -> language[0][0]);
    $no = $node -> ownerDocument;
    $node -> appendChild($no -> createCDATASection($n_meta_title));
    $resources -> meta_title -> language[0][0] = $n_meta_title;
    $resources -> meta_title -> language[0][0]['id'] = $n_l_id;
    $resources -> meta_title -> language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/' . $n_l_id;

    $node = dom_import_simplexml($resources -> meta_description -> language[0][0]);
    $no = $node -> ownerDocument;
    $node -> appendChild($no -> createCDATASection($n_meta_description));
    $resources -> meta_description -> language[0][0] = $n_meta_description;
    $resources -> meta_description -> language[0][0]['id'] = $n_l_id;
    $resources -> meta_description -> language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/' . $n_l_id;

    $node = dom_import_simplexml($resources -> meta_keywords -> language[0][0]);
    $no = $node -> ownerDocument;
    $node -> appendChild($no -> createCDATASection($n_meta_keywords));
    $resources -> meta_keywords -> language[0][0] = $n_meta_keywords;
    $resources -> meta_keywords -> language[0][0]['id'] = $n_l_id;
    $resources -> meta_keywords -> language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/' . $n_l_id;

    $node = dom_import_simplexml($resources -> available_now -> language[0][0]);
    $no = $node -> ownerDocument;
    $node -> appendChild($no -> createCDATASection($n_available_now));
    $resources -> available_now -> language[0][0] = $n_available_now;
    $resources -> available_now -> language[0][0]['id'] = $n_l_id;
    $resources -> available_now -> language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/' . $n_l_id;

    $node = dom_import_simplexml($resources -> available_later -> language[0][0]);
    $no = $node -> ownerDocument;
    $node -> appendChild($no -> createCDATASection($n_available_later));
    $resources -> available_later -> language[0][0] = $n_available_later;
    $resources -> available_later -> language[0][0]['id'] = $n_l_id;
    $resources -> available_later -> language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/' . $n_l_id;

    try {
        $opt = array('resource' => 'products');
        $opt['postXml'] = $xml -> asXML();
        $xml = $webService -> add($opt);
    } catch (PrestaShopWebserviceException $ex) {
        echo '<b>Error : '.$ex->getMessage().'</b>';
        $trace = $ex->getTrace();

        print_r($trace);
    // my log function
    }
}

// simple use

$n_name = 'New product name 5';
$n_desc = 'New product description LONG';
$n_desc_short = 'New product description SHORT';
$n_link_rewrite = 'someone_rewrite_5';
$n_meta_title = 'meta-title';
$n_meta_description = 'meta desc';
$n_meta_keywords = 'some,one,keywords';
$n_available_now = 'Available for order';
$n_available_later = 'Available from gg/mm/yy';

$n_id_category_default = '2';
$n_id_category = '2';
$n_price = '144.65';
$n_active = '1';
$n_avail4order = '1';
$n_show_price ='1';

$n_l_id = '1';

$n_id_stock_availables = '22';
$n_id_id_product_attribute = '0';


// run
PS_new_product($n_id_category_default, $n_id_category, $n_price, $n_active,$n_avail4order,$n_show_price, $n_id_stock_availables ,$n_id_id_product_attribute, $n_l_id, $n_name, $n_desc, $n_desc_short, $n_link_rewrite, $n_meta_title, $n_meta_description, $n_meta_keywords,$n_available_now,$n_available_later);
4

1 回答 1

0

这纯粹是链接数据方法。你需要这个来创建资源颜色和大小到 Prestashop 有或没有服务 Web API。只有在那之后,您才能将它们引用到代码中,以使用 Web 服务 API 创建产品。

于 2014-01-20T18:18:17.167 回答