Function Shortcode obfuscation lien Woocomerce

				
					function custom_external_product_button_shortcode($atts) {
    // Extraire les attributs du shortcode
    $atts = shortcode_atts(array(
        'id' => '', // Optionnel : ID du produit passé manuellement
    ), $atts, 'external_product_button');

    // Détecter automatiquement l'ID du produit si aucun ID n'est fourni
    if (empty($atts['id'])) {
        if (is_product()) { // Vérifie si on est sur une page produit
            global $post;
            $product_id = $post->ID; // Récupère l'ID du produit en cours
        } else {
            return '<p class="error">Aucun produit n’est spécifié pour ce bouton.</p>';
        }
    } else {
        $product_id = intval($atts['id']); // Utiliser l'ID fourni par l'utilisateur
    }

    // Récupérer le produit WooCommerce
    $product = wc_get_product($product_id);

    // Vérifier si le produit existe
    if (!$product) {
        return '<p class="error">Le produit spécifié est introuvable.</p>';
    }

    // Vérifier si le produit est de type "external" (externe/affilié)
    if ($product->get_type() !== 'external') {
        return '<p class="error">Ce produit n’est pas de type externe/affilié.</p>';
    }

    // Récupérer l'URL d'achat externe
    $external_url = $product->get_product_url(); // URL externe
    $button_text = $product->get_button_text(); // Texte du bouton (défini dans WooCommerce)

    // Générer le bouton HTML
    $button_html = '<button class="fake-btn" onclick="window.open(\'' . esc_url($external_url) . '\')">' . esc_html($button_text) . '</button>';

    return $button_html;
}
add_shortcode('external_product_button', 'custom_external_product_button_shortcode');