0

I have a requirement to create a cross sales report(Which combination of the product is most purchased by a customer with it order count), But i want to avoid those orders contains parents and child,Suppose if an order contains P1 ,P2 and P3 products(P2 is the parent of P1) then i want to show the combination like P2 and P3(excluded the child),likewise i have to show the combination of products (excluding parent and child and showing parents only with other products).

So for this i have created a table where i'm updating the order , product details and parent details.But facing an issue to identify the correct parent of product(same child is having multiple parents),So how can i tackle this situation and find the exact and correct parent for each products.

i'm using the table catalog_product_relation for identifying the parent ,child relation(but in this for same child have multiple parents).

Any suggestions/recommendations will be of great help.

Need SQl code for this. Version Magento 1.9, Will be updating to magento 2 soon.

0

use this code

     $childProductIds = array('1','5'); //suppose child product ids given statically (or) dynamically we can do by using for loop
     $childProductId = implode(',',$childProductIds);
     $parentIds = Mage::getModel('catalog/product_type_configurable')->getParentIdsByChild($childProductId);

//For Magento2

          $productId = 4; //child product id
          $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
          $product = $objectManager->create('Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable')->getParentIdsByChild($productId);
         if(isset($product[0])){
          //parent product id..
         echo $product[0];
        }
  • For the Magento 2 example, instead of using Object Manager. You should either inject that Configurable class within either the block or into a viewmodel. – Sam Jun 12 at 12:38
  • @Sam Do you have the SQL code for this,Means how to get the parent id – Sandeep Salim 2 days ago
  • @Nadh Same question, It is for SQL reporting.Do you have the SQL code for it? – Sandeep Salim 2 days ago
  • Is the product your looking up also in a bundle / grouped product? catalog_product_relation contains bundle/group/config relations. Whereas catalog_product_super_link contains only config relation. | Otherwise can you edit your question and include, what your statement looks like so far. So we know where you are getting your data. @SandeepSalim – Sam 2 days ago
  • @Sam Yes,The products includes bundles/grouped products also. I was referring the table catalog_product_relation to find the parent -child relationship, since a child has multiple parents i was unable to find the exact parent. I haven't used the catalog_product_super_link table, i will check and update you. – Sandeep Salim yesterday

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.