Magento 2 get Store Id of a coupon code programmatically
-
22-03-2021 - |
Вопрос
I am using multiple stores, I wanted to get all the coupon code of a particular store.
In Magento 2 database, salesrule_website
table have stores
related information, but their is no model/collection to get the data from this table.
Please guide me how to get store information of a coupon code, or I need to create my own model for salesrule_website
table.
Решение
As we know, Magento is not providing any Model or Collection for salesrule_website
table.
So what I did to solve this issue is, I used mysql to get collection from that table.
Code:
$stringId = '10';
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
$connection = $resource->getConnection();
$tableName = $resource->getTableName('salesrule_website');
$sql = "Select website_id FROM " . $tableName ." Where rule_id IN ('".$stringId."')";
$result = $connection->fetchAll($sql);
if($result)
{
// Your code goes here
}
Не связан с magento.stackexchange