Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public function batchImportAllProductsFromFile($productsToBeInserted){
- foreach ($productsToBeInserted as $key => $customProduct ) {
- $productIDs = $this->getProductIDsByReference($customProduct->MS_CODMAG);
- if (sizeof($productIDs) == 0) {
- $product = new Product();
- } else if (sizeof($productIDs) == 1) {
- $product = new Product($productIDs[0]);
- } else {
- continue;
- }
- $product->reference = $customProduct->MS_CODMAG;
- $product->name = trim($customProduct->MS_DESCRIZIONE);
- $product->price = $customProduct->MS_PREZZO_1;
- $product->out_of_stock = ($customProduct ->MS_ESAURITO === "S" ? true : false);
- $category = null;
- $msGruppoConverted = $this->buildSubGroupCode($customProduct->MS_GRUPPO, $customProduct->MS_SGRUPPO);
- if ($customProduct->MS_GRUPPO !== 0 && $msGruppoConverted !== 0) {
- $product->id_category = [$customProduct->MS_GRUPPO, $msGruppoConverted];
- } else if ($customProduct->MS_GRUPPO === 0 && $msGruppoConverted !== 0) {
- $product->id_category = [$msGruppoConverted];
- } else if ($customProduct ->MS_GRUPPO !== 0 && $msGruppoConverted === 0) {
- $product->id_category = [$customProduct->MS_GRUPPO];
- }
- try {
- if (sizeof($productIDs) == 0) {
- if ($product->add()) {
- $product->updateCategories($product->category);
- $product->addFeatureProductImport($product->id, 1, $customProduct->MS_FAM);
- //StockAvailable::setQuantity((int)$product->id, 0, $product->quantity, Context::getContext()->shop->id);
- }
- } else if (sizeof($productIDs) == 1) {
- if ($product->update()) {
- $product->updateCategories($product->category);
- $alreadySavedFeatures = $product->getFeaturesStatic($productIDs[0]);
- if (sizeof($alreadySavedFeatures) != 1 || $alreadySavedFeatures[0] != $customProduct->MS_FAM) {
- $product->deleteProductFeatures();
- $product->addFeatureProductImport($product->id, 1, $customProduct->MS_FAM);
- }
- }
- }
- } catch (Exception $e) {
- var_dump("Errore: ", $e, $product);
- }
- }
- }
- <?php
- class myProductImporter {
- protected $products;
- protected $products_checksum;
- public function __construct($products) {
- // Your products from the csv file
- $this->products = $products;
- // Here you get an associative array of products references and checksums
- // ex: array('REF01158' => '489f9ze4f4ze9f49ze8', 'REF15616' => '48949844561233132')
- $this->products_checksum = getProductsChecksum();
- }
- public function run() {
- foreach ($this->products as $product) {
- // If the product ref is present in my checksum list, then its an update
- if (isset($this->products_checksum[$product['reference']])) {
- // If the checksum is different, the product needs an update
- if ($this->products_checksum[$product['reference']] != $this->getChecksum($product)) {
- $this->updateProduct($product);
- }
- // Else it's a new product
- } else {
- $this->addProduct($product);
- }
- }
- }
- protected function updateProduct($product) {
- $PSProduct = getProductByReferebce($product['reference']);
- // Update your product and save its new checksum
- }
- protected function addProduct($product) {
- $PSProduct = new Product();
- // Create the product and save its checksum
- }
- protected function getChecksum($product) {
- // Create a string containing all your product properties
- $checksum = $product['reference'];
- $checksum .= $product['name'];
- $checksum .= $product['description'];
- $checksum .= $product['id_category'];
- return md5($checksum);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement