Saturday, September 9, 2023

Laragon - mysql issue during upgrading mysql V5 to V8

1. Stop Laragon.

2. Remove all files and folders from mysql-8 in laragon -> data -> mysql-8.

3. Open terminal and go to this directory: laragon -> bin -> mysql -> mysql-8 -> bin

4. Run this command: mysqld --initialize --console

5. Run this command: start mysqld

6. Enter in to mysql: mysql -u root -p

7. Password has given in step 42

8. Run this command: ALTER USER 'root'@'localhost' IDENTIFIED BY '';

9. Close mysqld window and start laragon.

Enjoy mysql 8 in laragon!

Tuesday, May 9, 2023

Mysql import from a SQL file by command line way

Mysql import from a SQL file by command line way:

Open command line and login to your mysql database by typing below command:

mysql -u root -p password then press enter.

mysql > source path_of_your_sql_file.sql Eg: if your file in D drive then path will be D:\mysql.sql

Then press enter all your tables will be imported soon.

Tuesday, April 4, 2023

gitignore is not ignoring already committed files or folders

When gitignore is not working for already committed files or folders.

In that time you need to run the below comment first:

git rm --cached file_name

Or

git rm --cached folder_name

Or 

git rm --cached -r folder_name

It will clear committed stuff from git.

Then you can commit and push your branch.

Tuesday, March 21, 2023

Set local timezone in wordpress programatically

Set local timezone in wordpress programatically:

add_action('wp_loaded', 'animed_time_zone_function');
function animed_time_zone_function() {
    $timezone_identifier = 'Asia/Dhaka';
    date_default_timezone_set($timezone_identifier);
}

Happy coding!

Tuesday, February 14, 2023

Get completed orders count for a specific user with a specific product in Woocommerce

Get completed orders count for a specific user with a specific product in Woocommerce:

Code:

function get_total_product_completed_order_for_a_specific_user() {
    $product_slug = 'product-slug';
    $product_obj = get_page_by_path($product_slug, OBJECT, 'product');

    $pid = 0;
    if (!empty($product_obj)) {
        $pid = $product_obj->ID;
    }

    // Get all customer orders
    $customer_orders = get_posts(
        array(
            'numberposts' => -1,
            'meta_key'    => '_customer_user',
            'orderby'     => 'date',
            'order'       => 'DESC',
            'meta_value'  => get_current_user_id(),
            'post_type'   => wc_get_order_types(),
            'post_status' => array_keys(wc_get_order_statuses()),
            'post_status' => array('wc-completed'),
        )
    );

    $Order_Array = [];
    $apid_counter = 0;
    foreach ($customer_orders as $customer_order) {
        $orderq        = wc_get_order($customer_order);

        $items = $orderq->get_items();

        foreach ( $items as $item ) {
            $product_id = $item->get_product_id();
            if ($pid == $product_id) {
                $apid_counter++;
            }
            break;
        }

        $Order_Array[] = [
            "ID"    => $orderq->get_id(),
            "Value" => $orderq->get_total(),
            "Date"  => $orderq->get_date_created()->date_i18n('Y-m-d'),
        ];
    }

    // return count($Order_Array);
    return $apid_counter;
}

Happy coding...



Monday, February 6, 2023

Laravel - raw query with pagination

Laravel - raw query with pagination:

class InventoryController extends Controller

{

    public function index(Request $request)

    {

        $sql = "SELECT *,products.name as product_name, product_categories.name as category_name, products.id as productid,

              ( SELECT SUM(buy_products.qty) as purcahse_qty

                FROM buy_products

                WHERE buy_products.product_id=products.id GROUP BY buy_products.product_id ) as bqty,

              ( SELECT SUM(sell_products.qty) as order_qty

                FROM sell_products

                WHERE sell_products.product_id=products.id GROUP BY sell_products.product_id ) as sqty 

                FROM products LEFT JOIN product_categories ON products.product_category_id=product_categories.id

                ";

        $results = DB::select($sql);

        $productInventoryData = $this->arrayPaginator($results, $request);

        return view('auth.inventory.inventory_list')->with('productInventoryData', $productInventoryData);

    }


    public function arrayPaginator($array, $request)

    {

        $page = is_null($request->get('page')) ? 1 : $request->get('page');

        $perPage = 2;

        $offset = ($page * $perPage) - $perPage;


        return new LengthAwarePaginator(array_slice($array, $offset, $perPage, true), count($array), $perPage, $page,

            ['path' => $request->url(), 'query' => $request->query()]);

    }

}

And in views you can use links as like DB query build we use.
Thanks and happy coding.

Thursday, November 10, 2022

Detect current URL and match with home page in wordpres

Detect current URL and match with home page in wordpres:

Sometimes you may select "Your latest posts" in backend settings->reading.

In this case your home page will be your theme's index.php file.

In this case you might need to detect what is your front page / home page and match to your current page.

In this case you can use below code to differentiate home page to other pages/posts:

$home_url = get_option('home') . '/';
$actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on'
? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

if ($home_url === $actual_link) {
    // Code goes here
}

// Happy coding...

Wordpress debug log function

Wordpress error log code: if ( ! function_exists ( 'write_log' ) ) { function write_log ( $data ) { if ( defined ( '...