<?php
add_action('wp', 'pzy_wc_modify');
function pzy_wc_modify()
{
    if(pzy_is_otp_enabled()){
		add_action('wp_logout',function (){
			wp_redirect( home_url() );
			exit();
		});
	}

    if(!pzy_breadcrumb_enabled()){
        remove_action('woocommerce_before_main_content','woocommerce_breadcrumb',20);
    }

    add_action('woocommerce_before_main_content', 'pzy_open_container', 1);
    function pzy_open_container()
    {
        echo "<div class='container " . (pzy_breadcrumb_enabled()?'':'pzy-mt-40') . "'>";
    }

    remove_action('woocommerce_before_shop_loop', 'woocommerce_result_count', 20);
    remove_action('woocommerce_before_shop_loop', 'woocommerce_catalog_ordering', 30);

    add_action('woocommerce_shop_loop_header', function () {
        echo "<div class='pzy-shop-loop-header'>";
    }, 9);
    add_action('woocommerce_shop_loop_header', function () {
        if (is_shop()) {
            $shop_title = pzy_get_option('shop_title');
        } elseif (is_archive()) {
            $shop_title = get_the_archive_title();
        } else {
            $shop_title = get_the_title();
        }
        echo "<div class='pzy-header-title'>$shop_title</div>";
        echo "<div class='pzy-header-divider'></div>";
    }, 11);

    add_action('woocommerce_shop_loop_header', function () {
        echo "<div class='pzy_catalog_ordering'><i data-feather='filter' class='pzy_catalog_ordering_icon'></i><span class='pzy_catalog_ordering_title'>" . __('مرتب سازی بر اساس: ', "pezeshkyar") . "</span>";
    }, 11);
    add_action('woocommerce_shop_loop_header', 'woocommerce_catalog_ordering', 11);
    add_action('woocommerce_shop_loop_header', function () {
        echo "</div>";
    }, 12);

    add_action('woocommerce_shop_loop_header', function () {
        echo "</div>";
    }, 12);

    remove_action('woocommerce_sidebar', 'woocommerce_get_sidebar', 10);
    if (!is_product()) {
        add_action('woocommerce_before_shop_loop', function () {
            echo "<div class='pzy-product-archive'>";
        }, 11);
        add_action('woocommerce_before_shop_loop', 'woocommerce_get_sidebar', 11);
        add_action('woocommerce_after_shop_loop', function () {
            echo '</div>';
        }, 11);
    }

    function custom_woocommerce_catalog_orderby($orderby)
    {
        $orderby['menu_order'] = __('پیش فرض', "pezeshkyar");
        $orderby['popularity'] = __('پربازدیدها', "pezeshkyar");
        $orderby['rating'] = __('رتبه بندی', "pezeshkyar");
        $orderby['date'] = __('جدیدترین', "pezeshkyar");
        $orderby['price'] = __('ارزان ترین', "pezeshkyar");
        $orderby['price-desc'] = __('گرانی', "pezeshkyar");
        return $orderby;
    }

    add_filter('woocommerce_catalog_orderby', 'custom_woocommerce_catalog_orderby');

    add_action('woocommerce_after_main_content', 'pzy_close_container', 20);
    function pzy_close_container()
    {
        echo "</div>";
    }

    add_action('woocommerce_before_single_product_summary', function () {
        echo "<div class='pzy-wc-before_single_product_summary'>";
    });

    add_action('woocommerce_after_single_product_summary', function () {
        echo "</div>";
    }, 1);

    add_action('woocommerce_product_additional_information', function () {
        $footer = get_pzy_footer_of_woocommerce_tabs();
        echo $footer ?? '';
    }, 20);

    add_filter('the_content', function ($content) {
        global $product;
        if (is_product()) {
            $content = "<div class='pzy-product-content'>$content</div>" . get_pzy_footer_of_woocommerce_tabs();
        }
        return $content;
    });

    function get_pzy_footer_of_woocommerce_tabs()
    {
        if (is_product()) {
            ob_start();
            ?>
            <div class="pzy-post-footer-1">
                <div class="pzy-post-categories">
                    <span class="pzy-post-categories-title"><?php echo __('دسته بندی:', "pezeshkyar") ?></span>
                    <div class="pzy-post-categories-list"><?php echo wc_get_product_category_list(get_the_ID(), '، '); ?></div>
                </div>
            </div>
            <div class="pzy-post-footer-2">
                <div class="pzy-post-tags">
                    <span class="pzy-post-tags-title"><?php echo __('برچسب ها:', "pezeshkyar") ?></span>
                    <div class="pzy-post-tags-list">
                        <?php echo wc_get_product_tag_list(get_the_ID(), ' '); ?>
                    </div>
                </div>
            </div>
            <?php
            return ob_get_clean();
        }
    }

    add_filter('woocommerce_product_description_heading', function ($heading) {
        return '';
    });

    add_filter('woocommerce_product_additional_information_heading', function ($heading) {
        return '';
    });

    function add_pzy_footer_to_woocommerce_tabs()
    {
        echo 'content';
    }


    add_action('woocommerce_single_product_summary', 'pzy_open_introduction', 6);
    function pzy_open_introduction()
    {
        echo "<div class='pzy-product-introduction'><h4 class='pzy-product-introduction-title'>" . __('مشخصات :', "pezeshkyar") . "</h4>";
    }

    remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_excerpt', 20);
    add_action('woocommerce_single_product_summary', 'woocommerce_template_single_excerpt', 7);

    remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_meta', 40);
    add_action('woocommerce_single_product_summary', 'woocommerce_template_single_meta', 8);

    remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_sharing', 50);

    add_action('woocommerce_after_single_product_summary', function () {
        echo "<div class='pzy-tabs-wrapper'>";
    }, 9);

    add_action('woocommerce_after_single_product_summary', 'woocommerce_template_single_sharing', 9);


    add_action('woocommerce_after_single_product_summary', function () {
        echo "</div>";
    }, 11);

    add_action('woocommerce_share', function () {
        echo do_shortcode('[pzy_share_article_dropdown transparent="1"]');
    });

    add_action('woocommerce_single_product_summary', 'pzy_close_introduction', 9);
    function pzy_close_introduction()
    {
        echo "</div>";
    }

    add_action('woocommerce_single_product_summary', 'pzy_show_sku', 9);


    remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_rating', 10);
    remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30);

    add_action('woocommerce_single_product_summary', 'pzy_open_product_after_introduction', 10);
    function pzy_open_product_after_introduction()
    {
        echo "<div class='pzy_product_after_introduction'>";
    }


    add_action('woocommerce_single_product_summary', 'pzy_show_stock_status', 11);

    add_action('woocommerce_single_product_summary', 'pzy_show_comments_count', 11);

    if (is_product()) {
        remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_price', 10);
        add_action('woocommerce_single_product_summary', 'pzy_remove_single_price_from_variable_product', 1);
        function pzy_remove_single_price_from_variable_product()
        {
            global $product;
            // if ( $product && $product->is_type( 'simple' ) ) {
            add_action('woocommerce_single_product_summary', 'pzy_wc_template_single_price', 11);
            // }
        }
    }

    function pzy_wc_display_variation_price($variation)
    {
        $decimals = wc_get_price_decimals();
        $decimal_separator = wc_get_price_decimal_separator();
        $thousand_separator = wc_get_price_thousand_separator();

        echo '<div class="price">';

        $regular_price = floatval($variation->get_regular_price());
        $price = floatval($variation->get_price());

        if ($variation->is_on_sale() && $regular_price > 0) {
            $percentage = '% ' . round(100 - ($price / $regular_price * 100));
            echo "<div class='pzy-product-discount'><p class='pzy-product-discount-percentage'>" . $percentage . "</p></div>";
        }

        echo "<div class='pzy-product-price'>";
        if ($variation->is_on_sale()) {
            $pzy_regular_price = '';
            $pzy_regular_price = number_format($regular_price, $decimals, $decimal_separator, $thousand_separator);
            echo '<p class="pzy-regular-price">' . $pzy_regular_price . '</p>';
        }

        $zero_price_text = pzy_get_option('zero_price_text');
        $no_price_text = pzy_get_option('no_price_text');
        if(!empty($no_price_text)){
            $no_price_template = pzy_get_option('no_price_template');
            if($no_price_template){
                $no_price_text = "<span class='pzy-open-modal' data-target='#pzy-product-contact-modal'>$no_price_text</span>";
            }
        }
        $display_price = $variation->get_price();
        $product_price_text = '';

        if($display_price === '0' && $zero_price_text){
            $product_price_text = $zero_price_text;
        }elseif(empty($display_price) && $display_price !== '0' && $no_price_text){
            $product_price_text = $no_price_text;
        }else{
			$product_price_text = wc_price($display_price);
		}

        echo '<p class="pzy-price">' . $product_price_text . '</p>';
        echo "</div>";

        echo "</div>";
    }

    add_action('woocommerce_single_product_summary', 'pzy_close_product_after_introduction', 12);
    function pzy_close_product_after_introduction()
    {
        echo "</div>";
    }

    add_action('woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 13);

    add_action('woocommerce_before_add_to_cart_form', function () {
        echo "<div class='pzy-add-to-cart-form'>";
    });

    add_action('woocommerce_after_add_to_cart_form', function () {
        echo "</div>";
    });

    add_filter('woocommerce_dropdown_variation_attribute_options_args', function ($args) {
        $args['class'] = ($args['class'] ?? '') . ' pzy-variations-dropdown';
        return $args;
    });


    add_filter('woocommerce_available_variation', 'pzy_variation', 10, 3);
    function pzy_variation($data, $product, $variation)
    {
        ob_start();
        pzy_wc_display_variation_price($variation);
        $data['price_html'] = ob_get_clean();
        return $data;
    }

    //add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 11 );

    // remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation', 10 );


    // Remove the default gallery
    remove_action('woocommerce_before_single_product_summary', 'woocommerce_show_product_sale_flash');
//    remove_action('woocommerce_before_single_product_summary', 'woocommerce_show_product_images', 20);

    // Add your custom gallery function
//    add_action('woocommerce_before_single_product_summary', 'pzy_product_gallery', 20);

    function pzy_product_gallery()
    {
        global $product;

        if (!$product) {
            return;
        }

        // Get the main product image
        $main_image_url_full = $product->get_image('full');
        $main_image_url_medium = $product->get_image('medium');
        $main_image_url_thumbnail = $product->get_image('thumbnail');

        // Get gallery images
        $attachment_ids = $product->get_gallery_image_ids();

        echo '<div class="pzy-wc-gallery">';

        /* thumb gallery */
        echo "<div class='pzy-thumbnail-gallery swiper custom-init'>";
        echo "<div class='swiper-wrapper'>";

        echo '<div class="swiper-slide">';
        echo '<div class="pzy-slide-content">' . $main_image_url_thumbnail . '</div>';
        echo '</div>';

        foreach ($attachment_ids as $attachment_id) {
            $gallery_image_url_thumbnail = wp_get_attachment_image_url($attachment_id, 'thumbnail');

            echo '<div class="swiper-slide">';
            echo '<div class="pzy-slide-content"><img src="' . esc_url($gallery_image_url_thumbnail) . '"></div>';
            echo '</div>';
        }

        echo "</div>";
        echo '<div class="swiper-pagination"></div>';

        echo "</div>";
        /* thumb gallery */

        /* main gallery */
        echo "<div class='pzy-main-gallery swiper custom-init'>";
        echo "<div class='swiper-wrapper'>";

        echo '<div class="swiper-slide">';
        echo '<div class="pzy-slide-content">' . $main_image_url_medium . '</div>';
        echo '</div>';

        foreach ($attachment_ids as $attachment_id) {
            $gallery_image_url_medium = wp_get_attachment_image_url($attachment_id, 'medium');

            echo '<div class="swiper-slide">';
            echo '<div class="pzy-slide-content"><img src="' . esc_url($gallery_image_url_medium) . '" loading="lazy"/><div class="swiper-lazy-preloader"></div></div>';
            echo '</div>';
        }

        echo "</div>";

        echo '<div class="swiper-button-next"></div>';
        echo '<div class="swiper-button-prev"></div>';

        if(pzy_yith_wc_wishlist_enabled()){
            echo do_shortcode("[yith_wcwl_add_to_wishlist link_classes='pzy_wishlist']");
        }

        echo "</div>";
        /* main gallery */


        /* large gallery */
        echo "<div class='pzy-wc-lightbox pzy-hide'>";
        echo "<div class='pzy-wc-lightbox-content'>";
        echo "<div class='pzy-large-gallery swiper custom-init'>";
        echo "<div class='swiper-wrapper'>";

        echo '<div class="swiper-slide">';
        echo '<div class="pzy-slide-content">' . $main_image_url_full . '</div>';
        echo '</div>';

        foreach ($attachment_ids as $attachment_id) {
            $gallery_image_url_full = wp_get_attachment_image_url($attachment_id, 'full');

            echo '<div class="swiper-slide">';
            echo '<div class="pzy-slide-content"><img src="' . esc_url($gallery_image_url_full) . '" loading="lazy"/><div class="swiper-lazy-preloader"></div></div>';
            echo '</div>';
        }

        echo "</div>";

        echo '<div class="swiper-button-next"></div>';
        echo '<div class="swiper-button-prev"></div>';

        echo "</div>";

        echo '<button class="pzy-close-wc-lightbox"><i data-feather="plus"></i></button>';


        echo "</div>";


        echo "</div>";
        /* large gallery */

        echo "</div>";


    }


    add_filter('woocommerce_product_review_comment_form_args', 'pzy_customize_wc_comment_form_args');

    function pzy_customize_wc_comment_form_args($comment_form)
    {
        $commenter = wp_get_current_commenter();

        $fields = $comment_form['fields'];

        $fields ['author'] = '<div class="comment-form-row comment-form-author"><div class="input-append"><input placeholder="' . __('نام و نام خانوادگی:', "pezeshkyar") . '" id="author" name="author" type="text" value="' . esc_attr($commenter['comment_author']) . '" size="30"' . 'aria-required="true"' . ' required /><i data-feather="user"></i></div></div>';
        $fields ['email'] = '<div class="comment-form-row comment-form-email"><div class="input-append"><input placeholder="' . __('پست الکترونیکی:', "pezeshkyar") . '" id="email" name="email" type="text" value="' . esc_attr($commenter['comment_author_email']) . '" size="30"' . 'aria-required="true"' . ' required /><i data-feather="mail"></i></div></div>';

        $comment_form['fields'] = $fields;
        $comment_form ['comment_field'] = '<div class="comment-form-comment"><textarea placeholder="' . esc_html__('متن دیدگاه', 'woocommerce') . '" id="comment" name="comment" class="span12" rows="5" aria-required="true"></textarea></div>';

        $rating_text = '';

        if (wc_review_ratings_enabled()) {

            $rating_required = wc_review_ratings_required() ? 'required' : '';

            $rating_text = '<div class="comment-form-rating"><label for="rating">' . esc_html__('Your rating', 'woocommerce') . (wc_review_ratings_required() ? '&nbsp;<span class="required">*</span>' : '') . '</label><select name="rating" id="rating" ' . $rating_required . '>
                            <option value="">' . esc_html__('Rate&hellip;', 'woocommerce') . '</option>
                            <option value="5">' . esc_html__('Perfect', 'woocommerce') . '</option>
                            <option value="4">' . esc_html__('Good', 'woocommerce') . '</option>
                            <option value="3">' . esc_html__('Average', 'woocommerce') . '</option>
                            <option value="2">' . esc_html__('Not that bad', 'woocommerce') . '</option>
                            <option value="1">' . esc_html__('Very poor', 'woocommerce') . '</option>
                        </select></div>';
        }

        $comment_form['submit_field'] = '<div class="comment-form-submit-box"><div class="pzy-rate-box">' . $rating_text . '</div><p class="form-submit">%1$s %2$s</p></div>';
        $comment_form['label_submit'] = esc_html__('ارسال دیدگاه', 'pezeshkyar');

        $comment_form['title_reply'] = __('دیدگاه شما', "pezeshkyar");
        $comment_form['title_reply_before'] = '<h3 id="reply-title" class="comment-reply-title">';
        $comment_form['title_reply_after'] = '<p class="title_reply_after"></p></h3>';

        return $comment_form;
    }

    // remove_action('woocommerce_review_comment_text','woocommerce_review_display_comment_text',10);
    remove_action('woocommerce_review_meta', 'woocommerce_review_display_meta', 10);
    remove_action('woocommerce_review_before_comment_meta', 'woocommerce_review_display_rating');


    add_action('woocommerce_review_meta', 'pzy_woocommerce_review_meta_action_start', 10);
    add_action('woocommerce_review_meta', 'pzy_woocommerce_review_meta_action_show_author', 10);
    add_action('woocommerce_review_meta', 'pzy_woocommerce_review_meta_action_show_date', 10);
    add_action('woocommerce_review_meta', 'pzy_woocommerce_review_meta_action_end', 10);

    //add_action( 'woocommerce_review_meta', 'pzy_woocommerce_review_display_comment_text',10 );


    function pzy_woocommerce_review_meta_action_start()
    {
        ?>
        <div class="pzy-wc-comment-info">
        <?php
    }

    function pzy_woocommerce_review_meta_action_show_author($comment)
    {
        $comment_id = $comment->comment_ID;
        ?>
        <cite class="fn">
            <?php echo get_comment_author_link($comment_id); ?>
        </cite>
        <?php

    }

    function pzy_woocommerce_review_meta_action_show_date($comment)
    {
        $comment_id = $comment->comment_ID;
        ?>
        <div class="comment-meta commentmetadata">
            <?php echo '<span class="comment-meta-title">' . __('تاریخ: ', "pezeshkyar") . '</span>' . get_comment_date('Y/m/d', $comment_id) ?>
        </div>
        <?php

    }

    function pzy_woocommerce_review_meta_action_end()
    {
        ?>
        </div>
        <?php

    }

    add_filter('woocommerce_reviews_title', function () {
        $title = __('دیدگاه ها', 'pezeshkyar');
        return "<p class='reviews-title'>$title</p><p class='review-title-divider'></p>";
    });

    add_filter('woocommerce_product_tabs', 'remove_reviews_tab', 98);
    function remove_reviews_tab($tabs)
    {
        unset($tabs['reviews']); // Remove the reviews tab
        return $tabs;
    }

    add_action('woocommerce_after_single_product_summary', 'pzy_product_reviews_section', 21);
    function pzy_product_reviews_section()
    {
        if (comments_open() || get_comments_number()) {
            comments_template('single-product-reviews.php');
        }
    }

    remove_action('woocommerce_before_shop_loop_item_title', 'woocommerce_show_product_loop_sale_flash');
    remove_action('woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail');

//    add_action('woocommerce_before_shop_loop_item_title','pzy_wc_template_loop_product_thumbnail',10);
    add_action('woocommerce_before_shop_loop_item_title', function () {
        echo '<div class="pzy-product-img">';
    }, 9);

    add_action('woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 9);

    add_action('woocommerce_before_shop_loop_item_title', function () {
        echo '</div>';
    }, 9);

    add_filter('post_class', function ($classes, $class, $product_id) {
        $classes = array_merge(['pzy-loop-item'], $classes);
        return $classes;
    }, 10, 3);


    add_action('woocommerce_after_shop_loop_item_title', 'pzy_show_loop_item_category', 4);

    function pzy_show_loop_item_category()
    {
        global $product;
        echo "<div class='pzy-loop-product-categories'>" . pzy_wc_the_category($product->get_id()) . "</div>";
    }

    remove_action('woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10);
    add_action('woocommerce_after_shop_loop_item_title', 'pzy_wc_template_loop_price', 10);

    function pzy_wc_template_loop_price()
    {
        global $product;
        echo "<div class='pzy-card-bottom'>";
        echo get_pzy_price_html($product);
    }

    remove_action('woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10);
    add_action('woocommerce_after_shop_loop_item', 'pzy_wc_template_loop_add_to_cart', 10);

    remove_action('woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_rating', 5);

    function pzy_wc_template_loop_add_to_cart($args = array())
    {
        global $product;

        $defaults = array(
            'quantity'              => 1,
            'class'                 => implode(
                ' ',
                array_filter(
                    array(
                        'pzy-add-to-cart',
                        'button',
                        wc_wp_theme_get_element_class_name( 'button' ), // escaped in the template.
                        'product_type_' . $product->get_type(),
                        $product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button' : '',
                        $product->supports( 'ajax_add_to_cart' ) && $product->is_purchasable() && $product->is_in_stock() ? 'ajax_add_to_cart' : '',
                    )
                )
            ),
            'aria-describedby_text' => $product->add_to_cart_aria_describedby(),
            'attributes'            => array(
                'data-product_id'  => $product->get_id(),
                'data-product_sku' => $product->get_sku(),
                'aria-label'       => $product->add_to_cart_description(),
                'rel'              => 'nofollow',
            ),
        );

        $args = apply_filters( 'woocommerce_loop_add_to_cart_args', wp_parse_args( $args, $defaults ), $product );

        echo apply_filters(
            'woocommerce_loop_add_to_cart_link', // WPCS: XSS ok.
            sprintf(
                '<a href="%s" aria-describedby="woocommerce_loop_add_to_cart_link_describedby_%s" data-quantity="%s" class="%s" %s>%s</a>',
                esc_url( $product->add_to_cart_url() ),
                esc_attr( $product->get_id() ),
                esc_attr( isset( $args['quantity'] ) ? $args['quantity'] : 1 ),
                esc_attr( isset( $args['class'] ) ? $args['class'] : 'button' ),
                isset( $args['attributes'] ) ? wc_implode_html_attributes( $args['attributes'] ) : '',
                '<i class="pzy-cart-icon" data-feather="shopping-bag"></i>'
            ),
            $product,
            $args
        );

        echo "</div>";
    }

    add_action('woocommerce_before_shop_loop_item', function () {
        if (pzy_yith_wc_wishlist_enabled()) {
            echo do_shortcode("[yith_wcwl_add_to_wishlist link_classes='pzy_wishlist']");
        }
    }, 9);

    add_action('woocommerce_before_shop_loop_item', function () {
        global $product;
        if ($product->get_rating_count() > 0)
            echo "<div class='pzy-loop-item-rating'><i class='pzy-loop-item-rating-icon' data-feather='star'></i><p class='pzy-loop-item-rating-text'>" . $product->get_average_rating() . "</p></div>";
    }, 9);

    /* checkout hooks */
    add_action('woocommerce_before_checkout_form', function () {
        echo "<div class='pzy-checkout-coupon-box'>";
    }, 9);
    // remove_action( 'woocommerce_before_checkout_form', 'woocommerce_checkout_coupon_form', 10 );
    add_action('woocommerce_before_checkout_form', function () {
        echo "</div>";
    }, 11);

    add_action('woocommerce_checkout_before_order_review_heading', function () {
        echo "<div class='pzy-order-review'>";
    }, 10);

    remove_action('woocommerce_checkout_order_review', 'woocommerce_checkout_payment', 20);

    add_action('woocommerce_checkout_before_order_review_heading', function () {
        echo "<div class='pzy-checkout-payment'><h3 class='pzy-checkout-payment-heading-title'>" . __('انتخاب روش پرداخت', 'pezeshkyar') . "</h3>";
    }, 8);
    add_action('woocommerce_checkout_before_order_review_heading', 'woocommerce_checkout_payment', 8);
    add_action('woocommerce_checkout_before_order_review_heading', function () {
        echo "</div>";
    }, 8);

    add_action('woocommerce_review_order_before_submit', 'remove_place_order_button', 1);
    function remove_place_order_button()
    {
        remove_action('woocommerce_review_order_before_submit', 'woocommerce_order_button_html', 10);
    }


    add_action('woocommerce_checkout_after_order_review', function () {
        echo "</div>";
    }, 10);


//    add_action('woocommerce_checkout_after_order_review', 'woocommerce_checkout_payment', 12);


    add_filter('woocommerce_checkout_show_terms', function ($show_terms) {
        return false;
    });

    /* checkout hooks */

    /* MyAccount Hook */
    add_action('woocommerce_before_account_navigation', function () {
        $current_user = wp_get_current_user();
        $is_staff = false;
        $user_id = $current_user->ID;
        $account_edit_url = wc_customer_edit_account_url();
        if(function_exists('pzy_is_staff') && function_exists('pzy_get_current_staff')){
            $is_staff = pzy_is_staff($current_user);
            $account_page_id = get_option('woocommerce_myaccount_page_id'); // Get the My Account page ID
            $account_url = get_permalink($account_page_id); // Get the correct My Account page URL
            $account_edit_url = trailingslashit($account_url . 'staff-edit'); // Append 'staff-edit' dynamically
            if($is_staff){
                list($staff, $staff_id) = pzy_get_current_staff();
                $user_id = $staff_id;
            }
        }
        echo "<div class='pzy-navigation-card'>";
        echo "<div class='pzy-navigation-card-header'>";
        echo get_avatar($current_user->ID, 90);
        echo "<div class='pzy-navigation-card-header-content'>";
        echo "<div class='pzy-user-information'>";
        if($is_staff){
            echo "<div class='pzy-user-meta'><span class='pzy-user-meta-title'>" . __('کد متخصص:', "pezeshkyar") . "</span><span class='pzy-user-meta-value'>#" . $staff_id . "</span></div>";
        }else{
            echo "<div class='pzy-user-meta'><span class='pzy-user-meta-title'>" . __('کد کاربر:', "pezeshkyar") . "</span><span class='pzy-user-meta-value'>#" . $current_user->ID . "</span></div>";
        }
        if($is_staff){
            echo "<div class='pzy-user-title'>" . $staff->post_title . "</div>";
        }else{
            echo "<div class='pzy-user-title'>" . $current_user->display_name . "</div>";
        }
        if(!$is_staff && pzy_get_option('register_staff') && isset($account_edit_url)){
            echo "<a class='button pzy-register-staff' href='$account_edit_url'>".esc_html__('ثبت نام متخصص','pezeshkyar-core')."</a>";
        }
        echo "</div>";
        echo "<a class='pzy-edit-profile-link' href='" . $account_edit_url . "'><i data-feather='edit'></i></a>";
        echo "</div>";
        echo "</div>";
    });

    add_action('woocommerce_after_account_navigation', function () {
        echo "<button class='pzy-close-navigation-modal'><i data-feather='plus'></i></button>";
        echo "</div>";
    });

    add_action('woocommerce_account_content', function () {
        echo "<button class='pzy-toggle-navigation'><i data-feather='chevron-right'></i></button>";
    }, 9);

    // add_filter('woocommerce_my_account_my_address_formatted_address',function($args, $customer_id, $name){
    //     return $args;
    // },10,3);


    function pzy_add_phone_number_field()
    {
        $user_id = get_current_user_id();
        $phone = get_user_meta($user_id, 'mobile_number', true);
        ?>
        <p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide"
           id="edit_account_mobile_number_row">
            <label for="mobile_number"><?php esc_html_e('Phone Number', 'woocommerce'); ?>&nbsp;<span
                        class="required">*</span></label>
            <input type="tel" class="woocommerce-Input woocommerce-Input--tel input-text" name="mobile_number"
                   id="mobile_number" value="<?php echo esc_attr($phone); ?>"/>
        </p>
        <?php
    }

    add_action('woocommerce_edit_account_form_start', 'pzy_add_phone_number_field');

    function pzy_validate_phone_number_field($args)
    {
        if (!isset($_POST['mobile_number']) || empty($_POST['mobile_number'])) {
            wc_add_notice(__('وارد کردم فیلد تلفن الزامی است.', 'pezeshkyar'), 'error');
        }
    }

    add_action('woocommerce_save_account_details_errors', 'pzy_validate_phone_number_field', 10, 1);

    function pzy_save_phone_number_field($user_id)
    {
        if (isset($_POST['mobile_number'])) {
            update_user_meta($user_id, 'mobile_number', sanitize_text_field($_POST['mobile_number']));
        }
    }

    add_action('woocommerce_save_account_details', 'pzy_save_phone_number_field');

    add_action('woocommerce_before_account_downloads', function ($has_downloads) {
        if (!$has_downloads) {
            echo "<div class='pzy-empty-items'>" . '<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100" viewBox="0 0 100 100" fill="none">
                <path d="M70.8333 70.8334H70.875M72.5 58.3334H75C78.8828 58.3334 80.8243 58.3334 82.3557 58.9678C84.3976 59.8135 86.0199 61.4358 86.8657 63.4777C87.5 65.0092 87.5 66.9506 87.5 70.8334C87.5 74.7163 87.5 76.6577 86.8657 78.1891C86.0199 80.231 84.3976 81.8533 82.3557 82.6991C80.8243 83.3334 78.8828 83.3334 75 83.3334H25C21.1172 83.3334 19.1757 83.3334 17.6443 82.6991C15.6024 81.8533 13.9801 80.231 13.1343 78.1891C12.5 76.6577 12.5 74.7163 12.5 70.8334C12.5 66.9506 12.5 65.0092 13.1343 63.4777C13.9801 61.4358 15.6024 59.8135 17.6443 58.9678C19.1757 58.3334 21.1172 58.3334 25 58.3334H27.5M50 62.5001V16.6667M50 62.5001L37.5 50.0001M50 62.5001L62.5 50.0001" stroke="#D7DBDF" stroke-width="5" stroke-linecap="round" stroke-linejoin="round"/>
                </svg>';
        }
    }, 9);

    add_action('woocommerce_after_account_downloads', function ($has_downloads) {
        if (!$has_downloads) {
            echo "</div>";
        }
    }, 9);

    add_action('woocommerce_before_account_orders', function ($has_orders) {
        if (!$has_orders) {
            echo "<div class='pzy-empty-items'>" . '<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100" viewBox="0 0 100 100" fill="none">
                <path d="M70.8333 70.8334H70.875M72.5 58.3334H75C78.8828 58.3334 80.8243 58.3334 82.3557 58.9678C84.3976 59.8135 86.0199 61.4358 86.8657 63.4777C87.5 65.0092 87.5 66.9506 87.5 70.8334C87.5 74.7163 87.5 76.6577 86.8657 78.1891C86.0199 80.231 84.3976 81.8533 82.3557 82.6991C80.8243 83.3334 78.8828 83.3334 75 83.3334H25C21.1172 83.3334 19.1757 83.3334 17.6443 82.6991C15.6024 81.8533 13.9801 80.231 13.1343 78.1891C12.5 76.6577 12.5 74.7163 12.5 70.8334C12.5 66.9506 12.5 65.0092 13.1343 63.4777C13.9801 61.4358 15.6024 59.8135 17.6443 58.9678C19.1757 58.3334 21.1172 58.3334 25 58.3334H27.5M50 62.5001V16.6667M50 62.5001L37.5 50.0001M50 62.5001L62.5 50.0001" stroke="#D7DBDF" stroke-width="5" stroke-linecap="round" stroke-linejoin="round"/>
                </svg>';
        }
    }, 9);

    add_action('woocommerce_after_account_orders', function ($has_orders) {
        if (!$has_orders) {
            echo "</div>";
        }
    }, 9);

    add_action('woocommerce_order_details_after_customer_details', function () {
        echo "<div class='pzy-end-buttons'><a class='button' href='" . home_url() . "'>" . __("بازگشت به صفحه اصلی", "pezeshkyar") . "</a></div>";
    });


    remove_action('woocommerce_available_downloads', 'woocommerce_order_downloads_table');
    add_action('woocommerce_available_downloads', 'pzy_custom_account_downloads');

    function pzy_custom_account_downloads()
    {
        $downloads = WC()->customer->get_downloadable_products();

        if ($downloads) {
            $downloads_per_page = 10; // Set the number of downloads per page
            $current_page = max(1, get_query_var('downloads_page', 1)); // Get the current page
            $total_downloads = count($downloads);
            $total_pages = ceil($total_downloads / $downloads_per_page);
            $offset = ($current_page - 1) * $downloads_per_page;
            $downloads_paginated = array_slice($downloads, $offset, $downloads_per_page);

            ?>
            <table class="woocommerce-table woocommerce-table--order-downloads shop_table shop_table_responsive order_details">
                <tbody>
                <?php foreach ($downloads_paginated as $download) : ?>
                    <?php
                    $product = wc_get_product($download['product_id']);
                    $product_image = $product ? $product->get_image('thumbnail') : ''; // Get the product image
                    $order = wc_get_order($download['order_id']); // Get the product URL
                    ?>
                    <tr>
                        <td>
                            <div class="pzy-product-information">
                                <?php echo $product_image; ?>
                                <p><?php echo esc_html($download['product_name']); ?></p>
                            </div>
                        </td>
                        <td data-title="<?php esc_attr_e('Downloads remaining', 'woocommerce'); ?>">
                            <?php echo is_numeric($download['downloads_remaining']) ? esc_html($download['downloads_remaining']) : esc_html__('Unlimited', 'woocommerce'); ?>
                        </td>
                        <td data-title="<?php esc_attr_e('Expires', 'woocommerce'); ?>">
                            <?php echo (!empty($download['access_expires'])) ? date_i18n(get_option('date_format'), strtotime($download['access_expires'])) : esc_html__('Never', 'woocommerce'); ?>
                        </td>
                        <td>
                            <div class="actions">
                                <?php
                                $actions = array(
                                    'view' => array(
                                        'url' => esc_url($order->get_view_order_url()),
                                        'name' => __('View', 'woocommerce'),
                                    ),
                                    'download' => array(
                                        'url' => esc_url($download['download_url']),
                                        'name' => __('Download', 'woocommerce'),
                                    )
                                );

                                if ($actions) {
                                    foreach ($actions as $key => $action) {
                                        printf('<a href="%s" class="woocommerce-Button button %s">%s</a>', esc_url($action['url']), sanitize_html_class($key), esc_html($action['name']));
                                    }
                                }
                                ?>
                            </div>
                        </td>
                    </tr>
                <?php endforeach; ?>
                </tbody>
            </table>
            <?php
            if ($total_pages > 1) {
                echo '<nav class="woocommerce-pagination">';
                echo paginate_links(array(
                    'base' => add_query_arg('downloads_page', '%#%'),
                    'format' => '?downloads_page=%#%',
                    'current' => $current_page,
                    'total' => $total_pages,
                    'add_args' => false,
                ));
                echo '</nav>';
            }
        } else {
            ?>
            <div class="woocommerce-Message woocommerce-Message--info woocommerce-info">
                <?php esc_html_e('No downloads available yet.', 'woocommerce'); ?>
            </div>
            <?php
        };
    }

//    add_action('woocommerce_before_customer_login_form', 'pzy_before_my_account_forms');
//    add_action('woocommerce_before_lost_password_form', 'pzy_before_my_account_forms');
//    add_action('woocommerce_before_reset_password_form', 'pzy_before_my_account_forms');
//    add_action('woocommerce_before_lost_password_confirmation_message', 'pzy_before_my_account_forms');


    function pzy_before_my_account_forms()
    {
        echo "<div class='pzy-account-form-box'>";
        echo "<div class='pzy-img-wrapper'>";
        echo "<img src='" . Theme_ASSETS_URL . "/images/form-bg.png' class='pzy-account-form-bg-img'/>";
        echo "</div>";
        echo "<div class='pzy-logo-wrapper'>";
        echo "<img src='" . Theme_ASSETS_URL . "/images/auth-Logo.png' class='pzy-account-form-logo-img'/>";
        echo "</div>";
    }

//    add_action('woocommerce_after_customer_login_form', 'pzy_after_my_account_forms');
//    add_action('woocommerce_after_lost_password_form', 'pzy_after_my_account_forms');
//    add_action('woocommerce_after_reset_password_form', 'pzy_after_my_account_forms');
//    add_action('woocommerce_after_lost_password_confirmation_message', 'pzy_after_my_account_forms');


    function pzy_after_my_account_forms()
    {
        echo "</div>";
    }

    add_action('template_redirect', 'redirect_logged_out_users_from_my_account');

    function redirect_logged_out_users_from_my_account()
    {
        if (is_page()) {
            if (is_account_page() && !is_user_logged_in()) {
                if ($auth_page = pzy_get_auth()) {
                    $auth_page_link = get_permalink($auth_page);

                    if ($auth_page_link) {
                        if (!function_exists('is_wc_endpoint_url') || !is_wc_endpoint_url('lost-password')) {
                            wp_redirect($auth_page_link);
                            exit;
                        }
                    }
                }
            } elseif (is_user_logged_in() && $auth_page = pzy_get_auth()) {
                if (is_page($auth_page)) {
                    if (function_exists('wc_get_account_endpoint_url') && $dashboard = wc_get_account_endpoint_url('dashboard')) {
                        wp_redirect($dashboard);
                        exit;
                    }
                }
            }
        }
    }

    /* My Account Hook */

    add_filter('woocommerce_product_related_products_heading', function ($heading) {
        return "<i data-feather='box'></i>" . esc_html($heading);
    });

}

add_action('init', 'pzy_add_endpoint');

function pzy_add_endpoint()
{
    $my_account_extra_tabs = pzy_get_option('my_account_extra_tabs');
    if ($my_account_extra_tabs) {
        if (isset($my_account_extra_tabs['tab_slug']) && is_array($my_account_extra_tabs['tab_slug'])) {
            foreach ($my_account_extra_tabs['tab_slug'] as $slug) {
                add_rewrite_endpoint($slug, EP_ROOT | EP_PAGES);
            }
        }
    }
}


add_action('init', 'pzy_register_custom_content_loaders');
function pzy_register_custom_content_loaders()
{
    $my_account_extra_tabs = pzy_get_option('my_account_extra_tabs');
    if (isset($my_account_extra_tabs['tab_content']) && is_array($my_account_extra_tabs['tab_content'])) {
        foreach ($my_account_extra_tabs['tab_content'] as $index => $tab_content) {
            if (isset($my_account_extra_tabs['tab_slug'][$index])) {
                $tab_slug = $my_account_extra_tabs['tab_slug'][$index];
                if ($tab_slug) {
                    add_action('woocommerce_account_' . $tab_slug . '_endpoint', function () use ($tab_content) {
                        pzy_load_element_template($tab_content);
                    });
                }
            }
        }
    }
}


add_filter('woocommerce_account_menu_items', 'add_favorites_my_account_menu_item');
function add_favorites_my_account_menu_item($menu_items)
{
    if ($customize_my_account_dashboard_label = pzy_get_option('customize_my_account_dashboard_label')) {
        $menu_items['dashboard'] = $customize_my_account_dashboard_label;
    }
    if ($customize_my_account_orders_label = pzy_get_option('customize_my_account_orders_label')) {
        $menu_items['orders'] = $customize_my_account_orders_label;
    }
    if ($customize_my_account_downloads_label = pzy_get_option('customize_my_account_downloads_label')) {
        $menu_items['downloads'] = $customize_my_account_downloads_label;
    }
    if ($customize_my_account_edit_address_label = pzy_get_option('customize_my_account_edit_address_label')) {
        $menu_items['edit-address'] = $customize_my_account_edit_address_label;
    }
    if ($customize_my_account_edit_account_label = pzy_get_option('customize_my_account_edit_account_label')) {
        $menu_items['edit-account'] = $customize_my_account_edit_account_label;
    }
    if ($customize_my_account_customer_logout_label = pzy_get_option('customize_my_account_customer_logout_label')) {
        $menu_items['customer-logout'] = $customize_my_account_customer_logout_label;
    }

    $my_account_extra_tabs = pzy_get_option('my_account_extra_tabs');
    if (isset($my_account_extra_tabs['tab_title']) && is_array($my_account_extra_tabs['tab_title']) && isset($my_account_extra_tabs['tab_slug']) && is_array($my_account_extra_tabs['tab_slug'])) {
        $extra_items = array_combine($my_account_extra_tabs['tab_slug'], $my_account_extra_tabs['tab_title']);
        $menu_items = array_unique(array_merge($menu_items, $extra_items));
    }

    $customer_logout = $menu_items['customer-logout'];
    unset($menu_items['customer-logout']);
    $menu_items['customer-logout'] = $customer_logout;

    return $menu_items;
}

//add_action('woocommerce_account_favorites_endpoint', 'pzy_favorites_content');

function pzy_favorites_content()
{
    // Call the function to display the paginated list
    echo do_shortcode('[yith_wcwl_wishlist]');
}

add_filter('woocommerce_registration_error_email_exists', function ($message, $email) {
    // Get the current page URL without query arguments
    $clean_url = home_url(add_query_arg([
        'action' => 'login'
    ]));

    // Replace the placeholder link in the message with the cleaned URL
    $custom_message = str_replace('href="#"', 'href="' . esc_url($clean_url) . '"', $message);

    return $custom_message;
}, 10, 2);


add_filter('woocommerce_default_address_fields', function ($fields) {
    if ($checkout_billing_first_name_label = pzy_get_option('checkout_billing_first_name_label')) {
        $fields['first_name']['label'] = $checkout_billing_first_name_label;
    }
    if ($checkout_billing_first_name_priority = pzy_get_option('checkout_billing_first_name_priority')) {
        $fields['first_name']['priority'] = $checkout_billing_first_name_priority;
    }
    if ($checkout_billing_last_name_label = pzy_get_option('checkout_billing_last_name_label')) {
        $fields['last_name']['label'] = $checkout_billing_last_name_label;
    }
    if ($checkout_billing_last_name_priority = pzy_get_option('checkout_billing_last_name_priority')) {
        $fields['last_name']['priority'] = $checkout_billing_last_name_priority;
    }
    if ($checkout_billing_company_label = pzy_get_option('checkout_billing_company_label')) {
        $fields['company']['label'] = $checkout_billing_company_label;
    }
    if ($checkout_billing_company_priority = pzy_get_option('checkout_billing_company_priority')) {
        $fields['company']['priority'] = $checkout_billing_company_priority;
    }
    if ($checkout_billing_country_label = pzy_get_option('checkout_billing_country_label')) {
        $fields['country']['label'] = $checkout_billing_country_label;
    }
    if ($checkout_billing_country_priority = pzy_get_option('checkout_billing_country_priority')) {
        $fields['country']['priority'] = $checkout_billing_country_priority;
    }
    if ($checkout_billing_address_1_label = pzy_get_option('checkout_billing_address_1_label')) {
        $fields['address_1']['label'] = $checkout_billing_address_1_label;
    }
    if ($checkout_billing_address_1_placeholder = pzy_get_option('checkout_billing_address_1_placeholder')) {
        $fields['address_1']['placeholder'] = $checkout_billing_address_1_placeholder;
    }
    if ($checkout_billing_address_1_priority = pzy_get_option('checkout_billing_address_1_priority')) {
        $fields['address_1']['priority'] = $checkout_billing_address_1_priority;
    }
    if ($checkout_billing_city_label = pzy_get_option('checkout_billing_city_label')) {
        $fields['city']['label'] = $checkout_billing_city_label;
    }
    if ($checkout_billing_city_priority = pzy_get_option('checkout_billing_city_priority')) {
        $fields['city']['priority'] = $checkout_billing_city_priority;
    }
    if ($checkout_billing_state_label = pzy_get_option('checkout_billing_state_label')) {
        $fields['state']['label'] = $checkout_billing_state_label;
    }
    if ($checkout_billing_state_priority = pzy_get_option('checkout_billing_state_priority')) {
        $fields['state']['priority'] = $checkout_billing_state_priority;
    }
    if ($checkout_billing_postcode_label = pzy_get_option('checkout_billing_postcode_label')) {
        $fields['postcode']['label'] = $checkout_billing_postcode_label;
    }
    if ($checkout_billing_postcode_priority = pzy_get_option('checkout_billing_postcode_priority')) {
        $fields['postcode']['priority'] = $checkout_billing_postcode_priority;
    }

    if (!pzy_get_option('checkout_billing_show_address_2')) {
        unset($fields['address_2']);
    } else {
        if ($checkout_billing_address_2_placeholder = pzy_get_option('checkout_billing_address_2_placeholder')) {
            $fields['address_2']['placeholder'] = $checkout_billing_address_2_placeholder;
        }
        if ($checkout_billing_address_2_priority = pzy_get_option('checkout_billing_address_2_priority')) {
            $fields['address_2']['priority'] = $checkout_billing_address_2_priority;
        }
    }

    return $fields;
});

add_filter('woocommerce_billing_fields', function ($fields) {
    if (isset($fields['billing_email']) && $checkout_billing_email_label = pzy_get_option('checkout_billing_email_label')) {
        $fields['billing_email']['label'] = $checkout_billing_email_label;
    }
    if (isset($fields['billing_phone']) && $checkout_billing_phone_label = pzy_get_option('checkout_billing_phone_label')) {
        $fields['billing_phone']['label'] = $checkout_billing_phone_label;
    }

    if (isset($fields['billing_email']) && $checkout_billing_email_priority = pzy_get_option('checkout_billing_email_priority')) {
        $fields['billing_email']['priority'] = $checkout_billing_email_priority;
    }
    if (isset($fields['billing_phone']) && $checkout_billing_phone_priority = pzy_get_option('checkout_billing_phone_priority')) {
        $fields['billing_phone']['priority'] = $checkout_billing_phone_priority;
    }
    return $fields;
});

add_filter('woocommerce_get_country_locale', function ($locales) {
    $locale = WC()->countries->get_base_country();

    if ($checkout_billing_state_label = pzy_get_option('checkout_billing_state_label')) {
        $locales[$locale]['state']['label'] = $checkout_billing_state_label;
    }
    if ($checkout_billing_postcode_label = pzy_get_option('checkout_billing_postcode_label')) {
        $locales[$locale]['postcode']['label'] = $checkout_billing_postcode_label;
    }

    return $locales;
});

function pzy_show_stock_status()
{
    global $product;

    $stock_status = '';

    if ($product->is_in_stock()) {
        echo "<p class='pzy-product-availability pzy-product-in-stock'>" . __('موجود در انبار', 'pezeshkyar') . "</p>";
    }

}

function pzy_show_comments_count()
{
    global $post;
    echo "<div class='pzy-review-count'><i data-feather='message-square'></i>";
    comments_number();
    echo "</div>";
}

function pzy_wc_template_single_price()
{
    global $product;

    if (!$product) {
        return;
    }

    pzy_wc_display_price($product);

}

function pzy_wc_display_price($product)
{
    $decimals = wc_get_price_decimals();
    $decimal_separator = wc_get_price_decimal_separator();
    $thousand_separator = wc_get_price_thousand_separator();

    $zero_price_text = pzy_get_option('zero_price_text');
    $no_price_text = pzy_get_option('no_price_text');
    if(!empty($no_price_text)){
        $no_price_template = pzy_get_option('no_price_template');
        if($no_price_template){
            $no_price_text = "<span class='pzy-open-modal' data-target='#pzy-product-contact-modal'>$no_price_text</span>";
        }
    }

    $price_additional_class = $product->is_type('variable') ? 'pzy-variation-price' : '';
    echo '<div class="price ' . $price_additional_class . '">';

    if ($product->is_type('simple')) {
        if ($product->is_on_sale()) {
            echo "<div class='pzy-product-discount'><p class='pzy-product-discount-percentage'>" . get_discount_percentage($product) . "</p></div>";
        }

        echo "<div class='pzy-product-price'>";
        if ($product->is_on_sale()) {
            $pzy_regular_price = number_format(floatval($product->get_regular_price()), $decimals, $decimal_separator, $thousand_separator);
            echo '<p class="pzy-regular-price">' . $pzy_regular_price . '</p>';
        }
        $product_price = $product->get_price();
        if(empty($product_price) && $product_price !== '0' && $no_price_text){
            $product_price_text = $no_price_text;
        }elseif($product_price === '0' && $zero_price_text){
            $product_price_text = $zero_price_text;
        }else{
            $product_price_text = wc_price($product_price);
        }
        echo '<p class="pzy-price">' . $product_price_text . '</p>';
        echo "</div>";
    }

    echo "</div>";
}

function pzy_show_sku()
{
    global $product;

    if (wc_product_sku_enabled() && ($product->get_sku() || $product->is_type('variable'))) :
        echo '<div class="pzy-sku-wrapper"><span class="pzy-sku-title">' . esc_html('شناسه محصول:', 'pezeshkyar') . '</span><span class="pzy-sku">' . (($sku = $product->get_sku()) ? $sku : esc_html__('N/A', '"pezeshkyar"')) . '</span></div>';
    endif;
}

add_filter('woocommerce_gateway_icon', 'custom_gateway_icon', 10, 2);
function custom_gateway_icon($icon, $gateway_id)
{
    $payment_gateways = WC()->payment_gateways()->payment_gateways();
    if (isset($payment_gateways[$gateway_id])) {
        $payment_gateway = $payment_gateways[$gateway_id];
        $gateway_icon_id = $payment_gateway->get_option('icon');
        $gateway_active_icon_id = $payment_gateway->get_option('active_icon');

        if ($gateway_icon_id) {
            $gateway_icon_url = wp_get_attachment_image_url($gateway_icon_id);
            $gateway__active_icon_url = wp_get_attachment_image_url($gateway_active_icon_id);
            $icon = "<img class='" . ($gateway_icon_url ? 'gateway-icon-change-selected' : '') . "' src='$gateway_icon_url' alt='$gateway_id'/>";
            if ($gateway__active_icon_url) {
                $icon .= "<img class='gateway-selected-icon' src='$gateway__active_icon_url' alt='$gateway_id'/>";
            }
        }
    }


    return $icon;

}

add_action('init', 'add_icon_field_to_all_gateways');

function add_icon_field_to_all_gateways()
{
    foreach (WC()->payment_gateways()->payment_gateways() as $gateway) {
        $id = $gateway->id;
        add_filter('woocommerce_settings_api_form_fields_' . $id, function ($settings) use ($id) {
            return add_icon_field_to_gateway_settings($settings, $id);
        });
    }
}

function add_icon_field_to_gateway_settings($settings, $gateway_id)
{
    $settings['icon'] = array(
        'title' => __('شناسه آیکون', 'pezeshkyar'),
        'id' => 'woocommerce_' . $gateway_id . '_icon',
        'type' => 'text', // You can use 'media' type if you want to use a media uploader
        'css' => 'min-width:300px;',
        'default' => '',
        'desc_tip' => true,
    );
    $settings['active_icon'] = array(
        'title' => __('شناسه آیکون در حالت انتخاب شده', 'pezeshkyar'),
        'id' => 'woocommerce_' . $gateway_id . '_active_icon',
        'type' => 'text', // You can use 'media' type if you want to use a media uploader
        'css' => 'min-width:300px;',
        'default' => '',
        'desc_tip' => true,
    );
    return $settings;
}


function remove_woocommerce_column_classes($classes) {
    $classes = array_diff($classes, array('first', 'last', 'columns-1', 'columns-2', 'columns-3', 'columns-4', 'columns-5', 'columns-6'));
    return $classes;
}
add_filter('post_class', 'remove_woocommerce_column_classes', 21);

// Add custom column classes
//function add_custom_column_classes($classes) {
//    global $redux_options; // Assuming this is how you access your Redux options
//
//    $desktop = 4;
//    $tablet = 4;
//    $mobile = 2;
//
//    $classes[] = 'custom-col-desktop-' . $desktop;
//    $classes[] = 'custom-col-tablet-' . $tablet;
//    $classes[] = 'custom-col-mobile-' . $mobile;
//
//    return $classes;
//}
//add_filter('post_class', 'add_custom_column_classes', 22);


function enable_price_filter_on_archives($instance, $widget, $args) {
    // Check if the widget is WooCommerce's price filter
    if ($widget->id_base === 'woocommerce_price_filter') {
        // Show the widget on Shop and Product Archives (categories, tags, attributes)
        if (is_shop() || is_product_taxonomy()) {
            return $instance;
        }
    }

    return $instance; // Do not affect other widgets
}
add_filter('widget_display_callback', 'enable_price_filter_on_archives', 10, 3);

add_action('woocommerce_after_main_content',function(){
	/**
	 * Hook: woocommerce_archive_description.
	 *
	 * @since 1.6.2.
	 * @hooked woocommerce_taxonomy_archive_description - 10
	 * @hooked woocommerce_product_archive_description - 10
	 */
	do_action( 'woocommerce_archive_description' );
},20);

