How to join product collection of magento 2 to a custom table?
-
04-10-2020 - |
سؤال
I have a custom table user_product_table
with a field sku
(this field join to product collection) and user
. How can I join this table with product collection, so that I don't have to loop my custom collection to get product in each row.
My $userCollection
:
$userCollection = $this->_userProductFactory->create()
->getCollection()
->addFieldToFilter('user', 2);
My product Collection:
$productColelction = $this->_productCollectionFactory->create()
->addAttributeToSelect(array('name','image','short_description','sku'))
I would like to have final collection both my custom table fields and selected product attributes.
المحلول
We can use the join like this:
$joinConditions = 'u.sku = e.sku';
$productColelction->getSelect()->join(
['u' => $productColelction->getTable('user_product_table')],
$joinConditions,
[]
);
Multi conditions:
$joinConditions[] = "u.sku = e.sku";
$joinConditions[] = "e.type_id= 'simple' ";
$joinConditions = implode(
' AND ', $joinConditions
);
لا تنتمي إلى magento.stackexchange