/**
Theme Name: Astra Child
Author: Brainstorm Force
Author URI: http://wpastra.com/about/
Description: Astra is the fastest, fully customizable & beautiful theme suitable for blogs, personal portfolios and business websites. It is very lightweight (less than 50KB on frontend) and offers unparalleled speed. Built with SEO in mind, Astra comes with schema.org code integrated so search engines will love your site. Astra offers plenty of sidebar options and widget areas giving you a full control for customizations. Furthermore, we have included special features and templates so feel free to choose any of your favorite page builder plugin to create pages flexibly. Some of the other features: # WooCommerce Ready # Responsive # Compatible with major plugins # Translation Ready # Extendible with premium addons # Regularly updated # Designed, Developed, Maintained & Supported by Brainstorm Force. Looking for a perfect base theme? Look no further. Astra is fast, fully customizable and beautiful theme!
Version: 1.0.0
License: GNU General Public License v2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Text Domain: astra-child
Template: astra
*/


// 1. 在庫切れ（SOLD OUT）商品の価格を非表示にする
add_filter( 'woocommerce_get_price_html', 'astra_hide_price_for_sold_out', 10, 2 );
function astra_hide_price_for_sold_out( $price, $product ) {
    if ( ! $product->is_in_stock() ) {
        return ''; // 金額を非表示
    }
    return $price;
}

// 2. カテゴリーページ（一覧）の金額があった場所に赤文字で「SOLD OUT」を表示する
add_action( 'woocommerce_after_shop_loop_item_title', 'astra_display_sold_out_loop', 15 );
function astra_display_sold_out_loop() {
    global $product;
    if ( ! $product->is_in_stock() ) {
        // 色を鮮やかな赤（#e60012）に設定しています
        echo '<span class="sold-out-text" style="color: #e60012; font-weight: bold; display: block; margin-top: 5px; font-size: 0.9em;">SOLD OUT</span>';
    }
}

// 3. 商品詳細ページの「在庫切れ」というテキストを「SOLD OUT」に変更する
add_filter( 'woocommerce_get_availability_text', 'astra_custom_availability_text', 10, 2 );
function astra_custom_availability_text( $availability, $product ) {
    if ( ! $product->is_in_stock() ) {
        return 'SOLD OUT';
    }
    return $availability;
}

// 4. WooCommerceの外部商品URLの末尾に自動で商品名を追加する設定
add_filter( 'woocommerce_product_add_to_cart_url', 'add_custom_product_name_to_url', 10, 2 );
function add_custom_product_name_to_url( $url, $product ) {
    if ( $product->is_type( 'external' ) ) {
        // お問い合わせページのURLだった場合のみ処理
        if ( strpos( $url, 'contact-product' ) !== false ) {
            $url = add_query_arg( 'product_name', $product->get_name(), $url );
        }
    }
    return $url;
}
