Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.04 KB | None | 0 0
  1. public function batchImportAllProductsFromFile($productsToBeInserted){
  2. foreach ($productsToBeInserted as $key => $customProduct ) {
  3.  
  4. $productIDs = $this->getProductIDsByReference($customProduct->MS_CODMAG);
  5. if (sizeof($productIDs) == 0) {
  6. $product = new Product();
  7. } else if (sizeof($productIDs) == 1) {
  8. $product = new Product($productIDs[0]);
  9. } else {
  10. continue;
  11. }
  12.  
  13. $product->reference = $customProduct->MS_CODMAG;
  14. $product->name = trim($customProduct->MS_DESCRIZIONE);
  15. $product->price = $customProduct->MS_PREZZO_1;
  16. $product->out_of_stock = ($customProduct ->MS_ESAURITO === "S" ? true : false);
  17.  
  18. $category = null;
  19.  
  20. $msGruppoConverted = $this->buildSubGroupCode($customProduct->MS_GRUPPO, $customProduct->MS_SGRUPPO);
  21.  
  22. if ($customProduct->MS_GRUPPO !== 0 && $msGruppoConverted !== 0) {
  23. $product->id_category = [$customProduct->MS_GRUPPO, $msGruppoConverted];
  24. } else if ($customProduct->MS_GRUPPO === 0 && $msGruppoConverted !== 0) {
  25. $product->id_category = [$msGruppoConverted];
  26. } else if ($customProduct ->MS_GRUPPO !== 0 && $msGruppoConverted === 0) {
  27. $product->id_category = [$customProduct->MS_GRUPPO];
  28. }
  29. try {
  30. if (sizeof($productIDs) == 0) {
  31. if ($product->add()) {
  32. $product->updateCategories($product->category);
  33. $product->addFeatureProductImport($product->id, 1, $customProduct->MS_FAM);
  34. //StockAvailable::setQuantity((int)$product->id, 0, $product->quantity, Context::getContext()->shop->id);
  35. }
  36. } else if (sizeof($productIDs) == 1) {
  37. if ($product->update()) {
  38. $product->updateCategories($product->category);
  39. $alreadySavedFeatures = $product->getFeaturesStatic($productIDs[0]);
  40. if (sizeof($alreadySavedFeatures) != 1 || $alreadySavedFeatures[0] != $customProduct->MS_FAM) {
  41. $product->deleteProductFeatures();
  42. $product->addFeatureProductImport($product->id, 1, $customProduct->MS_FAM);
  43. }
  44. }
  45. }
  46. } catch (Exception $e) {
  47. var_dump("Errore: ", $e, $product);
  48. }
  49. }
  50. }
  51.  
  52. <?php
  53.  
  54. class myProductImporter {
  55. protected $products;
  56. protected $products_checksum;
  57.  
  58. public function __construct($products) {
  59. // Your products from the csv file
  60. $this->products = $products;
  61. // Here you get an associative array of products references and checksums
  62. // ex: array('REF01158' => '489f9ze4f4ze9f49ze8', 'REF15616' => '48949844561233132')
  63. $this->products_checksum = getProductsChecksum();
  64. }
  65.  
  66. public function run() {
  67. foreach ($this->products as $product) {
  68. // If the product ref is present in my checksum list, then its an update
  69. if (isset($this->products_checksum[$product['reference']])) {
  70. // If the checksum is different, the product needs an update
  71. if ($this->products_checksum[$product['reference']] != $this->getChecksum($product)) {
  72. $this->updateProduct($product);
  73. }
  74. // Else it's a new product
  75. } else {
  76. $this->addProduct($product);
  77. }
  78. }
  79. }
  80.  
  81. protected function updateProduct($product) {
  82. $PSProduct = getProductByReferebce($product['reference']);
  83. // Update your product and save its new checksum
  84. }
  85.  
  86. protected function addProduct($product) {
  87. $PSProduct = new Product();
  88. // Create the product and save its checksum
  89. }
  90.  
  91. protected function getChecksum($product) {
  92. // Create a string containing all your product properties
  93. $checksum = $product['reference'];
  94. $checksum .= $product['name'];
  95. $checksum .= $product['description'];
  96. $checksum .= $product['id_category'];
  97. return md5($checksum);
  98. }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement