You can get the category product table name dynamically using the Magento 2 Catalog module.
catalog_category_product table used for category-product relation.
The table contains entity_id, category_id, product_id, and position fields to manage relationships with products.
The class responsible for getting the category table name is Magento\Catalog\Model\ResourceModel\Category.
<?php
declare(strict_types=1);
namespace Rbj\Catalog\Model;
use Magento\Catalog\Model\ResourceModel\Category;
class CategoryProductTable
{
public function __construct(
private readonly Category $category
) {
}
/**
* category product table full name with prefix
*
* @return string
*/
public function getCategoryProductTable()
{
$categoryProduct = $this->category->getCategoryProductTable();
return $categoryProduct;
}
}
Call from a template or other class,
echo $categoryProductTable = $this->getCategoryProductTable();
Output:
catalog_category_product (if prefix available, the output is with prefix table name)
