Importing products into shop from csv/excel

  • Does anyone have a solution I could use for importing products into perch shop?


    I’m not a programmer so ideally would be looking for something I could copy.


    Ideal scenario is to import from a csv/excel as we want to quickly import 100s of products for testing into trial categories.

  • Here is some sample code for you to try (and edit to suit your needs)



    Do you also need to import images?

  • Many thanks I'll give this a go.


    Importing images isn't high on the priority list at the moment, as I get Perch to crunch various sizes on save for responsive images, it's probably not workable as we would have to save 5 different images per product?

  • Hello MxKert,


    I've tried the importer and got an error:


    Warning: Invalid argument supplied for foreach() in /path-to/Test_Shop/site-folder/perch/core/lib/api/PerchAPI_ContentImporter.class.php on line 76


    It's a fresh install of Perch Shop, any ideas what has gone wrong?


    Importer page:


    CSV:

    Code
    1. sku,title,slug,price
    2. 100001,Product 01,product-01,100
    3. 100002,Product 02,product-02,101
    4. 100003,Product 03,product-03,102
    5. 100004,Product 04,product-04,103


    In answer to your question about images - I could prep the various sizes and populate the css with the paths to the images.

  • What do you see if you run the following code:


  • I see an series of arrays, but they look malformed:


    Code
    1. Array ( [0] => uct-30 [1] => 129 100031 [2] => Product 31 [3] => product-31 [4] => 130 100032 [5] => Product 32 [6] => product-32 [7] => 131 100033 [8] => Product 33 [9] => product-33 [10] => 132 100034 [11] => Product 34 [12] => product-34 [13] => 133 100035 [14] => Product 35 [15] => product-35 [16] => 134 100036 [17] => Product 36 [18] => product-36 [19] => 135 100037 [20] => Product 37 [21] => product-37 [22] => 136 100038 [23] => Product 38 [24] => product-38 [25] => 137 100039 [26] => Product 39 [27] => product-39 [28] => 138 100040 [29] => Product 40 [30] => product-40 [31] => 139 100041 [32] => Product 41 [33] => product-41 [34] => 140 100042 [35] => Product 42 [36] => product-42 [37] => 141 100043 [38] => Product 43 [39] => product-43 [40] => 142 100044 [41] => Product 44 [42] => product-44 [43] => 143 100045 [44] => Product 45 [45] => product-45 [46] => 144 100046 [47] => Product 46 [48] => product-46 [49] => 145 100047 [50] => Product 47 [51] => product-47 [52] => 146 100048 [53] => Product 48 [54] => product-48 [55] => 147 100049 [56] => Product 49 [57] => product-49 [58] => 148 100050 [59] => Product 50 [60] => product-50 [61] => 149 100051 [62] => Product 51 [63] => product-51 [64] => 150 100052 [65] => Product 52 [66] => product-52 [67] => 151 100053 [68] => Product 53 [69] => product-53 [70] => 152 100054 [71] => Product 54 [72] => product-54 [73] => 153 100055 [74] => Product 55 [75] => product-55 [76] => 154 100056 [77] => Product 56 [78] => product-56 [79] => 155 100057 [80] => Product 57 [81] => product-57 [82] => 156 100058 [83] => Product 58 [84] => product-58 [85] => 157 100059 [86] => Product 59 [87] => product-59 [88] => 158 100060 [89] => Product 60 [90] => product-60 [91] => 159 )
    2. Array ( [0] => 100061 [1] => Product 61 [2] => product-61 [3] => 160 100062 [4] => Product 62 [5] => product-62 [6] => 161 100063 [7] => Product 63 [8] => product-63 [9] => 162 100064 [10] => Product 64 [11] => product-64 [12] => 163 100065 [13] => Product 65 [14] => product-65 [15] => 164 100066 [16] => Product 66 [17] => product-66 [18] => 165 100067 [19] => Product 67 [20] => product-67 [21] => 166 100068 [22] => Product 68 [23] => product-68 [24] => 167 100069 [25] => Product 69 [26] => product-69 [27] => 168 100070 [28] => Product 70 [29] => product-70 [30] => 169 100071 [31] => Product 71 [32] => product-71 [33] => 170 100072 [34] => Product 72 [35] => product-72 [36] => 171 100073 [37] => Product 73 [38] => product-73 [39] => 172 100074 [40] => Product 74 [41] => product-74 [42] => 173 100075 [43] => Product 75 [44] => product-75 [45] => 174 100076 [46] => Product 76 [47] => product-76 [48] => 175 100077 [49] => Product 77 [50] => product-77 [51] => 176 100078 [52] => Product 78 [53] => product-78 [54] => 177 100079 [55] => Product 79 [56] => product-79 [57] => 178 100080 [58] => Product 80 [59] => product-80 [60] => 179 100081 [61] => Product 81 [62] => product-81 [63] => 180 100082 [64] => Product 82 [65] => product-82 [66] => 181 100083 [67] => Product 83 [68] => product-83 [69] => 182 100084 [70] => Product 84 [71] => product-84 [72] => 183 100085 [73] => Product 85 [74] => product-85 [75] => 184 100086 [76] => Product 86 [77] => product-86 [78] => 185 100087 [79] => Product 87 [80] => product-87 [81] => 186 100088 [82] => Product 88 [83] => product-88 [84] => 187 100089 [85] => Product 89 [86] => product-89 [87] => 188 100090 [88] => Product 90 [89] => product-90 [90] => 189 100091 [91] => Pr )
    3. Array ( [0] => oduct 91 [1] => product-91 [2] => 190 100092 [3] => Product 92 [4] => product-92 [5] => 191 100093 [6] => Product 93 [7] => product-93 [8] => 192 100094 [9] => Product 94 [10] => product-94 [11] => 193 100095 [12] => Product 95 [13] => product-95 [14] => 194 100096 [15] => Product 96 [16] => product-96 [17] => 195 100097 [18] => Product 97 [19] => product-97 [20] => 196 100098 [21] => Product 98 [22] => product-98 [23] => 197 100099 [24] => Product 99 [25] => product-99 [26] => 198 100100 [27] => Product 100 [28] => product-100 [29] => 199 100101 [30] => Product 101 [31] => product-101 [32] => 200 100102 [33] => Product 102 [34] => product-102 [35] => 201 100103 [36] => Product 103 [37] => product-103 [38] => 202 100104 [39] => Product 104 [40] => product-104 [41] => 203 100105 [42] => Product 105 [43] => product-105 [44] => 204 100106 [45] => Product 106 [46] => product-106 [47] => 205 100107 [48] => Product 107 [49] => product-107 [50] => 206 100108 [51] => Product 108 [52] => product-108 [53] => 207 100109 [54] => Product 109 [55] => product-109 [56] => 208 100110 [57] => Product 110 [58] => product-110 [59] => 209 100111 [60] => Product 111 [61] => product-111 [62] => 210 100112 [63] => Product 112 [64] => product-112 [65] => 211 100113 [66] => Product 113 [67] => product-113 [68] => 212 100114 [69] => Product 114 [70] => product-114 [71] => 213 100115 [72] => Product 115 [73] => product-115 [74] => 214 100116 [75] => Product 116 [76] => product-116 [77] => 215 100117 [78] => Product 117 [79] => product-117 [80] => 216 100118 [81] => Product 118 [82] => product-118 [83] => 217 100119 [84] => Product 119 [85] => product-119 [86] => 218 100120 [87] => Produ )
    4. Array ( [0] => ct 120 [1] => product-120 [2] => 219 100121 [3] => Product 121 [4] => product-121 [5] => 220 100122 [6] => Product 122 [7] => product-122 [8] => 221 100123 [9] => Product 123 [10] => product-123 [11] => 222 100124 [12] => Product 124 [13] => product-124 [14] => 223 100125 [15] => Product 125 [16] => product-125 [17] => 224 100126 [18] => Product 126 [19] => product-126 [20] => 225 100127 [21] => Product 127 [22] => product-127 [23] => 226 100128 [24] => Product 128 [25] => product-128 [26] => 227 100129 [27] => Product 129 [28] => product-129 [29] => 228 100130 [30] => Product 130 [31] => product-130 [32] => 229 100131 [33] => Product 131 [34] => product-131 [35] => 230 100132 [36] => Product 132 [37] => product-132 [38] => 231 100133 [39] => Product 133 [40] => product-133 [41] => 232 100134 [42] => Product 134 [43] => product-134 [44] => 233 100135 [45] => Product 135 [46] => product-135 [47] => 234 100136 [48] => Product 136 [49] => product-136 [50] => 235 100137 [51] => Product 137 [52] => product-137 [53] => 236 100138 [54] => Product 138 [55] => product-138 [56] => 237 100139 [57] => Product 139 [58] => product-139 [59] => 238 100140 [60] => Product 140 [61] => product-140 [62] => 239 100141 [63] => Product 141 [64] => product-141 [65] => 240 100142 [66] => Product 142 [67] => product-142 [68] => 241 100143 [69] => Product 143 [70] => product-143 [71] => 242 100144 [72] => Product 144 [73] => product-144 [74] => 243 100145 [75] => Product 145 [76] => product-145 [77] => 244 100146 [78] => Product 146 [79] => product-146 [80] => 245 100147 [81] => Product 147 [82] => product-147 [83] => 246 100148 [84] => Product 148 [85] => product-148 [86] => 2 )
  • I suspected Excel had not formatted the csv properly so I changed the line endings and got the following result:



    However when switching back to the original importer, the same error message appears about line 76 in the importer class, this is the area of that file it refers to:


    Not sure if that helps?


    FYI - the product importer from the support site throws the same error: https://docs.grabaperch.com/api/import/shop/products/

  • My guess is that the fields you are importing are not matching the fields in your product.html template.


    So change this piece of code to suite your product template:


    Code
    1. $product = $ProductImporter->add_item([
    2. 'status' => true,
    3. 'stock_status' => '0',
    4. 'on_sale' => false,
    5. 'tax_group' => 1,
    6. 'sku' => $data[0],
    7. 'title' => $data[2],
    8. 'slug' => $data[1],
    9. 'price' => ['gbp' => $data[3]],
    10. ]);


    You can find more options here

  • That didn't help unfortunately.


    drewm would you be able to advise:


    I'm using the standard product template in a fresh install:



    Importer script:


    And the CSV:

  • Spot on Byron,


    The template was OK, it just wasn't in the right place. Once a set of the shop templates was copied into the normal templates folder the importer worked.


    Oddly, it always seems to loop back around to the start then throw an error for a duplicate SKU - but that doesn't bother me.


    Thank you so much, we can start testing the limits of the system now.