magento-2-blog icon indicating copy to clipboard operation
magento-2-blog copied to clipboard

Create/edit post blank page if more post contains same product

Open giacomopiva opened this issue 5 years ago • 8 comments

If more posts contains the same related products, the admin crate/edit post is blank.

Preconditions

  1. Magento: 2.3.5-p1
  2. PHP: 7.2
  3. MySQL: 5.7

Steps to reproduce

  1. create a blog post with a related product
  2. create a second blog post with the same related product
  3. click on "Add New Post", the page don't load properly and you'll get the CRITICAL error on the sysyem.log

Expected result

  1. The new post page should load properly

Actual result

  1. in the system.log main.CRITICAL: Item (Magento\Catalog\Model\Product\Interceptor) with the same ID "1" already exists. [] []

giacomopiva avatar Aug 06 '20 08:08 giacomopiva

Did you find a solution for this? We're facing the same problem.

Edit: I think I found it.

We're on Magento 2.3.4, Mageplaza_Blog 3.1.3.

In Mageplaza\Blog\Block\Adminhtml\Post\Edit\Tab\Product::_prepareCollection(), the join condition has to be changed when we're editing a post.

Old code:

$collection->getSelect()->joinLeft(
    ['mp_p' => $collection->getTable('mageplaza_blog_post_product')],
     'e.entity_id = mp_p.entity_id',
    ['position']
);

New code:

$postId = $this->getRequest()->getParam('id');
$cond = $postId ? 'e.entity_id = mp_p.entity_id AND mp_p.post_id = ' . (int)$postId : 'e.entity_id = mp_p.entity_id';
$collection->getSelect()->joinLeft(
    ['mp_p' => $collection->getTable('mageplaza_blog_post_product')],
    $cond,
    ['position']
);

simonrl avatar Aug 19 '20 11:08 simonrl

Nope, I'm waiting for a solution or a temporary workaround. I'll try the solution you posted as soon as I can.

For the moment a very big "thank you" :)

giacomopiva avatar Aug 19 '20 13:08 giacomopiva

@thaimageplaza can my fix be integrated in the next release please? Or are there side cases where it doesn't work?

simonrl avatar Aug 26 '20 09:08 simonrl

The code fixed the problem for me but... it seems a new problem is facing. While search for related products, a JS error occures in grid.js at line 474

Uncaught TypeError: Cannot read property 'down' of null at g.bindFieldsChange (grid.js:474) at g.initGrid (grid.js:113) at g.initGridAjax (grid.js:132) at fire (jquery.js:3238) at Object.fireWith (jquery.js:3368) at done (jquery.js:9861) at XMLHttpRequest.callback (jquery.js:10317)

giacomopiva avatar Aug 31 '20 11:08 giacomopiva

Hi @giacomopiva @simonrl I see that group by statement was removed in commit eaee6ccefb12630db3804fae121cd36ba53bd6e0 and bringing it back solves both above problems for me. This way I can open edit page and i do not have that grid.js error. I also kept extended left join by @simonrl

Above is PR so feel free to review. Hope it helps.

I did't check it in M2.4

nkajic avatar Oct 26 '20 11:10 nkajic

I would recommend to merge the above PR from @nkajic . This is still an issue which has a big impact on webshops with a lot of blog posts. the fix with adding distinct(true) (#307 ) isn't enough to fix the problem.

@phutx Would you please look at this issue and PR?

Thanks

Alainvdz99 avatar May 18 '21 13:05 Alainvdz99

Hi @Alainvdz99,

Have you upgraded your Blog module to the latest version? I am pleased to let you know that the related product issue has been resolved in the newest version.

Thanks!

phutx avatar May 20 '21 07:05 phutx

Hi @phutx,

I did upgrade the module to version 3.2 and we are using Magento 2.3. It did fix some blog posts but there were still a lot of blog post that gave a blank page if there were more posts with the same related product. The PR from @nkajic did fix it for me

Alainvdz99 avatar May 20 '21 09:05 Alainvdz99