<?php

/**
 * Plugin Name: WooCommerce Product Alternatives
 * Description: A plugin to add the ability for customers to see alternative products if the main product is out of stock, replacing the "Out of Stock" message with a custom list.
 * Version: 1.0
 * Author: Your Name
 * Text Domain: codewp
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly
}

if ( ! class_exists( 'WC_Product_Alternatives' ) ) :

class WC_Product_Alternatives {

    public function __construct() {
        add_filter( 'woocommerce_get_availability', array( $this, 'custom_override_get_availability' ), 10, 2 );
        add_action( 'woocommerce_product_options_general_product_data', array( $this, 'add_alternative_products_field' ) );
        add_action( 'woocommerce_process_product_meta', array( $this, 'save_alternative_products_field' ), 10, 2 );
    }

    public function add_alternative_products_field() {
        global $post;
        $product_ids = get_post_meta( $post->ID, 'alternative_products', true );

        echo '<div class="options_group">';
        woocommerce_wp_text_input( array(
            'id' => 'alternative_products',
            'label' => __('Alternative Products', 'codewp'),
            'desc_tip' => 'true',
            'description' => __('Enter the IDs of alternative products, separated by commas.', 'codewp'),
            'type' => 'text'
        ));
        echo '</div>';
    }

    public function save_alternative_products_field( $post_id ) {
        $alternative_products = isset( $_POST['alternative_products'] ) ? $_POST['alternative_products'] : '';
        update_post_meta( $post_id, 'alternative_products', $alternative_products );
    }

    public function custom_override_get_availability( $availability, $product ) {
        if ( ! $product->is_in_stock() ) {
            $alternative_products_ids = get_post_meta( $product->get_id(), 'alternative_products', true );

            if ( ! empty( $alternative_products_ids ) ) {
                $alternative_products_ids = explode( ',', $alternative_products_ids );
                $alternative_products_html = '<ul class="alternative-products-list">';
                foreach ( $alternative_products_ids as $product_id ) {
                    $alternative_product = wc_get_product( $product_id );
                    if ( $alternative_product && $alternative_product->is_in_stock() ) {
                        $alternative_products_html .= '<li><a href="' . $alternative_product->get_permalink() . '">' . $alternative_product->get_name() . ' - ' . $alternative_product->get_price_html() . '</a></li>';
                    }
                }
                $alternative_products_html .= '</ul>';
                $availability['availability'] = __('Out of stock. Check out these alternatives:', 'codewp') . $alternative_products_html;
                $availability['class'] = 'out-of-stock';
            } else {
                $availability['availability'] = __('Out of stock', 'codewp');
            }
        }
        return $availability;
    }

}

new WC_Product_Alternatives();

endif;
?>