0

I have added cross-sells products for my product in the Admin panel of Magento 2.

Still, on UI its shows different product.

The helper function for getting cross-sell products is

 public function getCrossSellProducts(){
    $currentProduct = $this->_registry->registry('current_product');
    $collection =  $currentProduct->getCrossSellProductCollection();
    $collection->addAttributeToSelect('*');
    $collection->setPageSize(8);
    $this->_crossSellProducts =  $collection->load();
    return $this->_crossSellProducts;
}

Still, it shows different products than my admin panel products which I added.

What's the issue?

Where cross-sell products are stored in database/ tables

2 Answers 2

1

Please check the catalog_product_link table and link_type_id field equal to 5 is cross_sell product type, for other related types you can refer to catalog_product_link_type table.

public function getCrossSellProducts(){
        /*$currentProduct = $this->_registry->registry('current_product');
        $collection =  $currentProduct->getCrossSellProductCollection();
        $collection->addAttributeToSelect('*');
        $collection->setPageSize(8);
        $this->_crossSellProducts =  $collection->load();
        return $this->_crossSellProducts;*/
        // debugg code sample
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        // $id is debugged current product id
        $currentProduct = $objectManager->create(\Magento\Catalog\Model\Product::class)->load($id);
        $crossSellIds = $currentProduct->getCrossSellProductIds();
//        $collection =  $currentProduct->getCrossSellProductCollection();
//        $sqlString = $collection->getSelectSql()->__toString();
//        echo $sqlString;die;
        var_dump($crossSellIds);
    }
4
  • In catalog_product_link table, there are 8 products with link_type_id = 5 but there are only 2 products in my cross-sells admin panel, none of this matches with catalog_product_link table list
    – summu
    Aug 22, 2019 at 7:22
  • This is not possible, it should be noted that the catalog_product_linl table product_id is row_id, you can use getCrossSellProductIds function for debugging?
    – Tokey Tu
    Aug 22, 2019 at 7:31
  • What SQL query does getCrossSellProductIds internally uses, how can I print SQL query which getCrossSellProductIds function uses?
    – summu
    Aug 22, 2019 at 7:36
  • Refer to my edited code for debugging.
    – Tokey Tu
    Aug 22, 2019 at 7:50
0

As far as I know, there is a store configuration in Admin > Stores > Configuration > Catalog > Catalog > Rule-based Product Relations. Please review your configuration for Show Cross-Sell Products in it.

There are three options like Selected, Rule-based and Both. Selected means it will show you products which are selected from the admin catalog product edit page. Rule-based means catalog rule is created in Promotions to display cross-sell products. So, the products from rule-based will be shown by this option. And the third option is Both Selected and Rule-based.

So, I suggest you to review this configuration.

4
  • it is Both Selected and rule based
    – summu
    Aug 22, 2019 at 7:09
  • So, this might be the reason it is showing different products. Aug 22, 2019 at 7:10
  • What should be the option to be selected to match it from admin panel
    – summu
    Aug 22, 2019 at 7:16
  • Select the option Selected. Aug 22, 2019 at 7:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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