Assign Related Products and Upsells to Simple Products Based on Grouping

Last modified by fuzzybabybunny on Fri, June 25, 2010 09:18
Source|Old Revisions  

This is an old revision of the document!


The first heading will show up as page title.

<code php> <pre> <?php $hostname=’127.0.0.1’; $username=’yourdbusername’; $password=’yourdbpassword’; $database=’yourdb’; $start_time=microtime(true); $link=mysql_connect($hostname,$username,$password); if (!$link) {

  die("nCould not connect to server $hostname: " . mysql_error());

} echo “nConnected successfully to server $hostname.”; $result=mysql_query(”USE `$database`;”); if (!$result) {

  die("nCould not connect to database $database: " . mysql_error());

} echo “nConnected successfully to database $database.”; $result = mysql_query(”SELECT link_type_id,code FROM catalog_product_link_type;”); $link_type=array(); while ($row = mysql_fetch_array($result)) {

  $link_type[$row['code']]=$row['link_type_id'];

} $grouped_result = mysql_query(”SELECT entity_id,type_id FROM catalog_product_entity WHERE type_id=’grouped’;”); while ($grouped = mysql_fetch_array($grouped_result)) {

  $grouped_product_id=$grouped['entity_id'];
  echo "nFound grouped product $grouped_product_id";
  $super_result = mysql_query("SELECT linked_product_id FROM catalog_product_link WHERE (product_id='$grouped_product_id' && link_type_id='{$link_type['super']}');");
  $linked_products=array();
  while ($super = mysql_fetch_array($super_result)) {
      $super_product_id=$super['linked_product_id'];
      echo "nFound linked product $super_product_id";
      $linked_products[]=$super_product_id;
  }
  foreach($linked_products as $linked_product){ //product_id     linked_product_id     link_type_id
      echo "nChecking linked product $linked_product";
      foreach($linked_products as $linked_target_product){
          if($linked_target_product==$linked_product){
              continue;
          }
          $linked_target_product_result = mysql_query("SELECT linked_product_id FROM catalog_product_link WHERE (product_id='$linked_product' && linked_product_id='$linked_target_product' && link_type_id='{$link_type['cross_sell']}');");
          if(mysql_num_rows($linked_target_product_result) > 0){
              echo "nFound cross-sell link for product $linked_target_product";
          } else {
              mysql_query("INSERT INTO catalog_product_link VALUES (NULL,'$linked_product','$linked_target_product','{$link_type['cross_sell']}');");
              echo "nAdded cross-sell link for product $linked_target_product";
          }
          $linked_target_product_result = mysql_query("SELECT linked_product_id FROM catalog_product_link WHERE (product_id='$linked_product' && linked_product_id='$linked_target_product' && link_type_id='{$link_type['relation']}');");
          if(mysql_num_rows($linked_target_product_result) > 0){
              echo "nFound relation link for product $linked_target_product";
          } else {
              mysql_query("INSERT INTO catalog_product_link VALUES (NULL,'$linked_product','$linked_target_product','{$link_type['relation']}');");
              echo "nAdded relation link for product $linked_target_product";
          }
      }
      $upsell_product_result = mysql_query("SELECT linked_product_id FROM catalog_product_link WHERE (product_id='$linked_product' && linked_product_id='$grouped_product_id' && link_type_id='{$link_type['up_sell']}');");
      if(mysql_num_rows($upsell_product_result) > 0){
          echo "nFound up-sell link for product $grouped_product_id";
      } else {
          mysql_query("INSERT INTO catalog_product_link VALUES (NULL,'$linked_product','$grouped_product_id','{$link_type['up_sell']}');");
          echo "nAdded up-sell link for product $grouped_product_id";
      }
      $cross_sell_product_result = mysql_query("SELECT linked_product_id FROM catalog_product_link WHERE (product_id='$linked_product' && linked_product_id='$grouped_product_id' && link_type_id='{$link_type['cross_sell']}');");
      if(mysql_num_rows($cross_sell_product_result) > 0){
          echo "nFound cross-sell link for product $grouped_product_id";
      } else {
          mysql_query("INSERT INTO catalog_product_link VALUES (NULL,'$linked_product','$grouped_product_id','{$link_type['cross_sell']}');");
          echo "nAdded cross-sell link for product $grouped_product_id";
      }
  }

} mysql_close($link); $end_time=microtime(true); $elapsed_time = sprintf(”%0.6f”, ($end_time - $start_time)); echo “nCompleted in $elapsed_time seconds.”; ?> </code php>




 

Magento 2 GitHub Repository

Magento Job Board - Some sort of tag line goes here

Latest Posts| View all Jobs