<?php

namespace Webnestors\Wsn\Snippets;

class SingleCartButtonOnbackorder extends \Webnestors\Wsn\Snippet {
    public $button_text = "Pre Order";

    public function tab(): string {
        return "product";
    }

    public function title(): string {
        return esc_html__("Button Text on Backorder", "enhancements-for-woocommerce");
    }

    public function description(): string {
        return esc_html__("Change the add to cart button text when the product is on backorder.", "enhancements-for-woocommerce");
    }

    public function meta(): array {
        return [
            [
                "label" => esc_html__("Backorder Text", "enhancements-for-woocommerce"),
                "name" => "button_text",
                "type" => "text",
            ],
        ];
    }

    public function tooltip(): ?string {
        ob_start(); ?>
        <h1><strong><?php echo esc_html($this->title()); ?></strong></h1>
        
        <h3>Description</h3>
        <p>The Button Text on Backorder module allows you to customize the "Add to Cart" button text specifically for products that are on backorder. Instead of displaying the usual "Add to Cart" message, you can set a custom label that clearly informs customers of the backorder status, such as "Pre-Order Now" or "Available on Backorder." This feature enhances transparency, improves customer expectations, and encourages customers to proceed with their purchase despite the delayed availability.</p>
        <p>This module is particularly beneficial for stores that frequently handle backordered items, as it can help reduce customer confusion and improve the likelihood of completing sales on out-of-stock items.</p>
        <h3>Settings</h3>
        <p>Apply the text you want to display on the button.</p>
        <h3>Appearance</h3>
        <p>There are no customization options for this module.</p>
        <?php return ob_get_clean();
    }

    public function run() {
        add_filter(
            "woocommerce_product_single_add_to_cart_text",
            [$this, "wn_change_backorder_button"],
            10,
            2
        );
    }

    function wn_change_backorder_button($text, $product) {
        if ($product->is_on_backorder()) {
            $text = $this->button_text;
        }
        return $text;
    }
}
