<?php

if (!defined('ABSPATH')) {
    exit;
}

// Hook to initialize filters for hiding prices
add_action('init', 'sapmfly_maybe_hide_woocommerce_prices');

function sapmfly_maybe_hide_woocommerce_prices()
{
    if (!function_exists('is_woocommerce')) {
        return;
    }

    $product_price_hide = get_option('sapmfly_hide_product_price', 'no');

    if ($product_price_hide === 'yes') {
        add_filter('woocommerce_get_price_html', '__return_empty_string');
        add_filter('woocommerce_cart_item_price', '__return_empty_string');
        add_filter('woocommerce_cart_item_subtotal', '__return_empty_string');
        add_filter('woocommerce_cart_subtotal', '__return_empty_string');
        add_filter('woocommerce_cart_total', '__return_empty_string');
        add_filter('woocommerce_order_amount_total', '__return_empty_string');
        add_filter('woocommerce_order_formatted_line_subtotal', '__return_empty_string');
    }
}

// Change 'Add to Cart' text related code
add_filter('woocommerce_product_add_to_cart_text', 'sapmfly_change_add_to_cart_text', 999);
add_filter('woocommerce_product_single_add_to_cart_text', 'sapmfly_change_add_to_cart_text', 999);

function sapmfly_change_add_to_cart_text($text)
{
    $option = get_option('sapmfly_change_cart_text', 'no');

    if ($option === 'yes') {
        $custom_text = get_option('sapmfly_cac_text', 'Add to cart');

        if (!empty($custom_text)) {
            return esc_html($custom_text);
        }
    }

    return $text;
}