Skip to content

Instantly share code, notes, and snippets.

View mehrshaddarzi's full-sized avatar

Mehrshad Darzi mehrshaddarzi

View GitHub Profile
@mehrshaddarzi
mehrshaddarzi / wc_bundle_product.php
Created November 24, 2024 11:54
Get WooCommerce bundles Product
<?php
if ( $product->is_type('bundle') ) {
$hasqty = 5;
$bundleditems = $product->get_bundled_items();
foreach ( $bundled_items as $bundleditem ) {
if ( $bundleditem->is_optional('yes')) {
$hasqty = 1;
echo "<h2> 1 product optional </h2>";
} else {
@mehrshaddarzi
mehrshaddarzi / disable_user_agnet_wp_comment.php
Created October 25, 2024 13:04
disable User Agent in WordPress insert comment
<?php
// Disable User Agent For WordPress Comment
// https://developer.wordpress.org/reference/functions/wp_new_comment/
add_filter( 'preprocess_comment', 'prepare_wp_insert_comment_data', 90, 1 );
function prepare_wp_insert_comment_data($commentdata) {
if(isset($commentdata['comment_agent']) and $commentdata['comment_agent'] != 'WooCommerce') {
$commentdata['comment_agent'] = '';
}
@mehrshaddarzi
mehrshaddarzi / remove_all_order_notes.php
Last active April 25, 2025 09:49
remove all order notes after completed wocommerce
<?php
// Delete Change Status Order Note For Completed
add_action( 'woocommerce_order_note_added', 'action_remove_order_note_for_completed', 20, 2);
function action_remove_order_note_for_completed($comment_id, $order) {
if ( $order->has_status('completed') ) {
wc_delete_order_note($comment_id);
}
}
@mehrshaddarzi
mehrshaddarzi / remove_before_order_checkout.php
Last active October 22, 2024 11:59
remove all before WooCommerce order pending in checkout
<?php
// Remove Another Order Pending When create New Order in Checkout
// @see https://wp-kama.com/plugin/woocommerce/function/WC_Checkout::create_order
// @see https://wp-kama.com/plugin/woocommerce/function/WC_Checkout::create_order
add_action('woocommerce_checkout_order_created', 'checkout_order_created_delete_before_pending_customer_order', 20, 1);
function checkout_order_created_delete_before_pending_customer_order($order)
{
if (is_admin()) {
return null;
@mehrshaddarzi
mehrshaddarzi / WP_Terms_Nested.php
Last active June 26, 2024 14:53
Get WordPress Terms Nested Array
<?php
if (!class_exists('WP_Terms_Nested')) {
class WP_Terms_Nested
{
public static function get($arg = []): array
{
@mehrshaddarzi
mehrshaddarzi / product_attributes_filter.php
Created June 23, 2024 17:24
Get All Product Attributes by Category Slug WooCommerce
<?php
function get_attributes_by_product_cat($category_slug = null, $cacheHour = 6)
{
global $wpdb;
// Check Product Cat Slug
if (is_null($category_slug)) {
$query_object = get_queried_object();
$category_slug = $query_object->slug ?? '';
<style>
.modal-window {
position: fixed;
background-color: rgba(255, 255, 255, 0.25);
top: 0;
right: 0;
bottom: 0;
left: 0;
z-index: 999;
visibility: hidden;
@mehrshaddarzi
mehrshaddarzi / run_time_microtime.php
Created April 6, 2024 08:47
Get Period Execute Run Time Script PHP (microtime)
// place this before any script you want to calculate time
$time_start = microtime(true);
// Check Time Spend
$time_end = microtime(true);
$execution_time = ($time_end - $time_start);
// Echo Date
echo date("H:i:s",$endtime-$starttime);
@mehrshaddarzi
mehrshaddarzi / option_cache_wordpress.php
Created January 31, 2024 03:58
Option Cache WordPress
// Check From Cache Option
if ($cache) {
$option = get_option('wp_hami_products');
if (!empty($option) and is_array($option) and isset($option['list']) and isset($option['expire']) and is_numeric($option['expire']) and $option['expire'] >= current_time('timestamp')) {
return [
'status' => true,
'list' => $option['list'],
'code' => $option['code'],
@mehrshaddarzi
mehrshaddarzi / Cronjob WordPress
Last active March 11, 2025 03:59
WordPress CronJob
$log_path = dirname(__FILE__).'/cron.txt';
$default_timezone = 'Asia/Tehran';
Date_default_timezone_set($default_timezone);
$now = date("Ymd @ H:i:s");
file_put_contents($log_path , $now.PHP_EOL, FILE_APPEND);
curl -s -o /dev/null -w "%{http_code}\n" "https://xxxxxx/?_sepidar_cron=product" >> /home/jaklin/public_html/status_code.txt 2>/dev/null