How to add Order Comments History by order id programmatically in Magento 2?

Add Magento Order History Comment using the programmatic approach for the orders.

You need to use the interface, Magento\Sales\Api\OrderStatusHistoryRepositoryInterface to add a comment. You can also set the order status for the comment.

You can check to add sales order status history data programmatically in Magento 2.

You have to Retrieve the Order ID to add a comment, load the order object by the ID, add a status history comment for the given sales order with some code snippet, Continue reading “How to add Order Comments History by order id programmatically in Magento 2?”

How to Get Configurable product children ids Magento 2?

Get all the children product ids from the Configurable product in Magento 2. You can retrieve the list of child item ids by the configurable product id.

Get all Children items of Bundle Products Magento 2.

To fetch all the children items from the configurable product, get Configurable product id and pass it to the method in given code,

<?php
namespace Jesadiya\ChildIds\Model;

use Magento\ConfigurableProduct\Model\Product\Type\Configurable;

class ConfigurableChildrenIds
{
    /**
     * @var Configurable
     */
    private $configurable;

    public function __construct(
        Configurable $configurable
    ) {
        $this->configurable = $configurable;
    }

    /**
     * Get Children items id by the parent id
     *
     * @param int $id
     *
     * @return array
     */
    public function getChildrenIds(int $id)
    {
        $childItemId = $this->configurable->getChildrenIds($id);
        return $childItemId;
    }
}

Call from the PHP class,

$configurableId = 2046;
$superAttributeByChild = $this->getChildrenIds($configurableId);

List of all the children items id for a configurable product.

Output:
Array

How to Write Select Query in Magento 2 with Standard way?

You can write a select query in Magento 2 to retrieve records from the table.

The select query can be written with best practices to avoid security breaches.

For example, You are searching for records from the core_config_data table,

Row query:

SELECT * FROM `core_config_data` WHERE `scope` = 'default' AND `path` = 'general/locale/code';

You can directly use the above string as a query and pass it to the query() method but it’s not a secure way to write a select query.

I will show you the standard Magento way to write database queries with the best secure standard Practice. Continue reading “How to Write Select Query in Magento 2 with Standard way?”