0

I want to create a method (in helper) that get increment_id value by order_id as parameter. So it will look like:

public function getIncrementId($orderId)
{    
    $sql = "SELECT increment_id FROM ".$tableName." WHERE order_id = '$orderId'";
    return increment_id;
}

In this case maybe $tableName will be sales_order table. Or maybe there is a resource model that can i use?

1 Answer 1

1

Try following way:

public function __construct(
    \Magento\Framework\App\ResourceConnection $resourceConnection
) {
    $this->resourceConnection = $resourceConnection;
}

Then your function looks like:

public function getIncrementId($orderId)
{
    $connection = $this->resourceConnection->getConnection();
    $sql = "SELECT increment_id FROM sales_shipment WHERE order_id = '$orderId'";
    $result = $connection->fetchAll($sql);
    foreach ($result as $item) {
        return $item['increment_id'];
    }
    return 0;
}

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.