PageRenderTime 48ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/magmi/integration/samples/sample.php

https://bitbucket.org/jit_bec/shopifine
PHP | 117 lines | 61 code | 6 blank | 50 comment | 8 complexity | 89df4a819a4396ab981092cae130eaa9 MD5 | raw file
Possible License(s): LGPL-3.0
  1. <?php
  2. require_once("../../inc/magmi_defs.php");
  3. require_once("../inc/magmi_datapump.php");
  4. /** Define a logger class that will receive all magmi logs **/
  5. class TestLogger
  6. {
  7. /**
  8. * logging methos
  9. * @param string $data : log content
  10. * @param string $type : log type
  11. */
  12. public function log($data,$type)
  13. {
  14. echo "$type:$data\n";
  15. }
  16. }
  17. /**
  18. * create a Product import Datapump using Magmi_DatapumpFactory
  19. */
  20. $dp=Magmi_DataPumpFactory::getDataPumpInstance("productimport");
  21. /**
  22. * Start import session
  23. * with :
  24. * - profile : test_ptj
  25. * - mode : create
  26. * - logger : an instance of the class defined above
  27. */
  28. /**
  29. * FOR THE SAMPLE TO WORK CORRECTLY , YOU HAVE TO DEFINE A test_ptj profile with :
  30. * UPSELL/CROSS SELL, ITEM RELATER, CATEGORIES IMPORTER/CREATOR selected
  31. * ON THE FLY INDEXER IS RECOMMENDED (better endimport performance)
  32. * Reindexer needed also to have products show up on front : select all but "catalog_category_product" & "url_rewrite" (both are handled by on the fly indexer)
  33. */
  34. $dp->beginImportSession("test_ptj","create",new TestLogger());
  35. /* Create 5000 items , with every 100 :
  36. *
  37. * upsell on last 100 even
  38. * cross sell on last 100 odd
  39. * related on last 100 every 5
  40. * cross sell on last 100 every 10
  41. * categories named catX/even or catX/odd with X is thousand of item (using categories plugin) */
  42. for($sku=0;$sku<5000;$sku++)
  43. {
  44. //create item category path array
  45. //catX/even or catX/odd, X being the 1000's of the item
  46. $cats=array("cat".strval(intval($sku/1000)));
  47. if($sku%2==0)
  48. {
  49. $cats[]="even";
  50. }
  51. else
  52. {
  53. $cats[]="odd";
  54. }
  55. //create item to import
  56. // sku : XXXXX , 5 numbers , padded left with current loop counter as sku
  57. // name : itemXXXXX
  58. // description : testXXXXX
  59. // price : random between $1 & $500
  60. // categories : the ones built above
  61. $item=array("sku"=>str_pad($sku,5,"0",STR_PAD_LEFT),"name"=>"item".$sku,"description"=>"test".$sku,"price"=>rand(1,500),"categories"=>implode("/",$cats));
  62. //now some fun, every 100 items, create some relations
  63. if($sku>99 && $sku%100==0)
  64. {
  65. //first, we'll remove all existing relations (upsell/cross sell / related)
  66. $upsell=array("-re::.*");
  67. $csell=array("-re::.*");
  68. $re=array("-re::.*");
  69. $xre=array();
  70. for($i=$sku-99;$i<$sku;$i++)
  71. {
  72. //related item sku
  73. $rsku=str_pad($i,5,"0",STR_PAD_LEFT);
  74. //add upselling on each odd item in the 100 before the current
  75. if($i%2==1)
  76. {
  77. $upsell[]=$rsku;
  78. }
  79. else
  80. //add cross sell on each even item in the 100 before the current
  81. {
  82. $csell[]=$rsku;
  83. }
  84. //on each 10 before, cross relate
  85. if($i%10==0)
  86. {
  87. $xre[]="-$rsku";
  88. }
  89. else
  90. {
  91. //on each 5 before , single relate
  92. if($i%5==0)
  93. {
  94. $re[]=$rsku;
  95. }
  96. }
  97. }
  98. //fill upsell with the computed skus from rules above
  99. $item["us_skus"]=implode(",",$upsell);
  100. //fill cross sell with the computed skus from rules above
  101. $item["cs_skus"]=implode(",",$csell);
  102. //fill single related with the computed skus from rules above
  103. $item["re_skus"]=implode(",",$re);
  104. //fill cross related with the computed skus from rules above
  105. $item["xre_skus"]=implode(",",$xre);
  106. }
  107. /* import current item */
  108. $dp->ingest($item);
  109. }
  110. /* end import session, will run post import plugins */
  111. $dp->endImportSession();