Create a function that insert two custom hook and match with custom post name

34 Views Asked by At

I try to make a two functions. The first function that add a two custom hook to add a custom section inside a product page. The Hook in woocommerce is woocommerce_after_single_product_summary and the code is:

 if (is_product()) {
     add_action('woocommerce_after_single_product_summary', function () {
         add_action('woocommerce_after_single_product_summary', 'display_produtor_post_content', 5);
         add_action('woocommerce_after_single_product_summary', 'display_conheca_seu_cafe_content', 8);
         add_action('woocommerce_after_single_product_summary', 'woocommerce_output_product_data_tab', 10);
         add_action('woocommerce_after_single_product_summary', 'woocommerce_upsell_display', 15);
         add_action('woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20);
     });
 }

The second part is try to make the the attribute matches a custom post name to show dinamically inside the product page. This is the code: Functions.php:

 function display_produtor_post_content() {
    global $product;

    // Check if it's a single product page
    if (is_product()) {
        $product_id = get_the_ID();

        // Fetch the custom post content associated with the product
        $args = array(
            'post_type' => 'Produtores', // Replace with your custom post type
            'meta_query' => array(
                array(
                    'key' => 'Produtor', // Replace with your custom field key
                    'value' => $product_id,
                    'compare' => '=',
                ),
            ),
        );

        $custom_query = new WP_Query($args);

        // Check if a custom post was found
        if ($custom_query->have_posts()) {
            while ($custom_query->have_posts()) {
                $custom_query->the_post();
                
                // Include the template file for Produtor
                include 'part-produtor.php';
            }
        }

        // Reset post data
        wp_reset_postdata();
    }
}

function display_conheca_seu_cafe_content() {
    global $product;

    // Check if it's a single product page
    if (is_product()) {
        $product_id = get_the_ID();

        // Fetch the custom post content associated with the product
        $args = array(
            'post_type' => 'Conheça seu café', // Replace with your custom post type
            'meta_query' => array(
                array(
                    'key' => 'Tipo de Café', // Replace with your custom field key
                    'value' => $product_id,
                    'compare' => '=',
                ),
            ),
        );

        $custom_query = new WP_Query($args);

        // Check if a custom post was found
        if ($custom_query->have_posts()) {
            while ($custom_query->have_posts()) {
                $custom_query->the_post();
                
                // Include the template file for Conheça seu café
                include 'part-conheca-seu-cafe.php';
            }
        }

        // Reset post data
        wp_reset_postdata();
    }
}

// Hook into the additional information section
add_action('woocommerce_after_single_product_summary', 'display_produtor_post_content', 5);
add_action('woocommerce_after_single_product_summary', 'display_conheca_seu_cafe_content', 8);

part-produtor.php:

<?php
defined('ABSPATH') || exit;

global $product, $product_id;

?>

<?php
if (is_product()) {
    // Get the product ID
    $product_id = get_the_ID();

    // Check if the product has a specific custom field
    $custom_field_value = get_post_meta($product_id, 'Produtor', true);

    if ($custom_field_value) {
        // Sanitize the custom field value before using it
        $sanitized_value = sanitize_text_field($custom_field_value);

        // Use the value in your logic or query
        $args = array(
            'post_type' => 'Produtores',
            'title'     => $sanitized_value, // Replace with the actual field you want to compare
        );

        $custom_query = new WP_Query($args);

        // Check if a post was found
        if ($custom_query->have_posts()) {
            while ($custom_query->have_posts()) {
                $custom_query->the_post();

                // Include a specific template file and pass any required data
                include 'parts-woocommerce/seu-cafe/part-template-seu-cafe.php';

                // Exit after including the file
                exit;
            }
        }

        // Reset post data
        wp_reset_postdata();
    } else {
        // Do something if the product doesn't have the specified custom field
    }
}
?>

But is not working.

Can someone show me error in this code to correct and show properly on my woocommerce?

0

There are 0 best solutions below