· 6 years ago · Apr 30, 2020, 07:32 PM
1<?php
2/**
3 * Connection API v2 + WSI-Compliance
4 */
5$options = array(
6 'trace' => true,
7 'connection_timeout' => 120,
8 'wsdl_cache' => WSDL_CACHE_NONE,
9);
10$proxy = new SoapClient('http://dev.magento.local/api/v2_soap/?wsdl=1', $options);
11$session = $proxy->login(array(
12 'username' => 'api_user',
13 'apiKey' => 'api_key',
14));
15$sessionId = $session->result;
16
17/**
18 * Simple product #1 (sku: SKU-001)
19 */
20$productData = array(
21 'name' => 'Name of product #1',
22 'description' => 'Description of product #1',
23 'short_description' => 'Short description of product #1',
24 'website_ids' => array('base'), // Id or code of website
25 'status' => 1, // 1 = Enabled, 2 = Disabled
26 'visibility' => 1, // 1 = Not visible, 2 = Catalog, 3 = Search, 4 = Catalog/Search
27 'tax_class_id' => 2, // Default VAT
28 'weight' => 0,
29 'stock_data' => array(
30 'use_config_manage_stock' => 0,
31 'manage_stock' => 0, // We do not manage stock, for example
32 ),
33 'price' => 9.90, // Same price than configurable product, no price change
34 'additional_attributes' => array(
35 array(
36 'key' => 'color',
37 'value' => 'Blue', // Id or label of color, attribute that will be used to configure product
38 ),
39 array(
40 'key' => 'size',
41 'value' => 'Large', // Id or label of size, attribute that will be used to configure product
42 ),
43 ),
44);
45// Creation of product #1
46$proxy->catalogProductCreate(array(
47 'sessionId' => $sessionId,
48 'type' => 'simple',
49 'set' => 'Default',
50 'sku' => 'SKU-001',
51 'productData' => $productData,
52));
53
54/**
55 * Simple product #2 (sku: SKU-002)
56 */
57$productData = array(
58 'name' => 'Name of product #2',
59 'description' => 'Description of product #2',
60 'short_description' => 'Short description of product #2',
61 'website_ids' => array('base'), // Id or code of website
62 'status' => 1, // 1 = Enabled, 2 = Disabled
63 'visibility' => 1, // 1 = Not visible, 2 = Catalog, 3 = Search, 4 = Catalog/Search
64 'tax_class_id' => 2, // Default VAT
65 'weight' => 0,
66 'stock_data' => array(
67 'use_config_manage_stock' => 0,
68 'manage_stock' => 0, // We do not manage stock, for example
69 ),
70 'price' => 8.90, // Red product is $1 less than configurable product
71 'additional_attributes' => array(
72 array(
73 'key' => 'color',
74 'value' => 'Red', // Id or label of color, attribute that will be used to configure product
75 ),
76 array(
77 'key' => 'size',
78 'value' => 'Medium', // Id or label of size, attribute that will be used to configure product
79 ),
80 ),
81);
82// Creation of product #2
83$proxy->catalogProductCreate(array(
84 'sessionId' => $sessionId,
85 'type' => 'simple',
86 'set' => 'Default',
87 'sku' => 'SKU-002',
88 'productData' => $productData,
89));
90
91/**
92 * Configurable product
93 */
94$productData = array(
95 'name' => 'Configurable product',
96 'description' => 'Description of configurable product',
97 'short_description' => 'Short description of configurable product',
98 'website_ids' => array('base'), // Id or code of website
99 'status' => 1, // 1 = Enabled, 2 = Disabled
100 'visibility' => 4, // 1 = Not visible, 2 = Catalog, 3 = Search, 4 = Catalog/Search
101 'tax_class_id' => 2, // Default VAT
102 'weight' => 0,
103 'stock_data' => array(
104 'use_config_manage_stock' => 0,
105 'manage_stock' => 0, // We do not manage stock, for example
106 ),
107 'price' => 9.90,
108 'associated_skus' => array('SKU-001', 'SKU-002'), // Simple products to associate
109 'price_changes' => array(
110 array(
111 'key' => 'color',
112 'value' => array(
113 array(
114 'key' => 'Red',
115 'value' => '2',
116 ),
117 array(
118 'key' => 'Blue',
119 'value' => '-10%',
120 ),
121 ),
122 ),
123 array(
124 'key' => 'size',
125 'value' => array(
126 array(
127 'key' => 'Large',
128 'value' => '+1',
129 ),
130 array(
131 'key' => 'Medium',
132 'value' => '-3',
133 ),
134 ),
135 ),
136 ),
137);
138// Creation of configurable product
139$proxy->catalogProductCreate(array(
140 'sessionId' => $sessionId,
141 'type' => 'configurable',
142 'set' => 'Default',
143 'sku' => 'SKU-TEST',
144 'productData' => $productData,
145));