Skip to content
Advertisement

Get a product from specific product attribute term name in WooCommerce

I’m trying to create a WooCommerce shortcode to display one product with some information.

Every day there is a new product with attribute DAGAANBIEDING and value JA or NEE.

I only want to show that product with value JA.
The shortcode is working but nothing is displaying.
This is what I have:

//custom shortcodes
if( !function_exists('product_snippet') ) {    

    function product_snippet( $atts ) {

        // Attributes
        extract( shortcode_atts( 
            array(
                'taxonomy' => 'pa_dagaanbieding', // You will use $id to get the value of this attribute
                'terms' => 'Ja' // You will use $snippet to get the value of this attribute
            ), 
            $atts
        ));

        // Get an instance of the product object
        $product = wc_get_product( $id );

        // Displays go here
        return '<div class="row">
        <div class="col-md-6"><h3>'.$product->get_title().'</h3>'.$product->get_image().'<br><br><a href="'.get_permalink( $id ).'"><button type="submit" class="single_add_to_cart_button button alt">Bekijk aanbieding</button></a></div>
        <div class="col-md-6">'.$product->get_regular_price().' '.$product->get_regular_price().'</div>
        </div>';
        
        
    }

    add_shortcode( 'product_snippet', 'product_snippet' );
}

But it does not show information.

Advertisement

Answer

There are some mistakes and missing things in your code. Try the following that will get the product that has pa_dagaanbieding product attribute (taxonomy) for Ja term name using a custom light SQL query:

if( !function_exists('display_product_dagaanbieding_ja') ) {    

    function display_product_dagaanbieding_ja( $atts ) {
        // Attributes
        extract( shortcode_atts( 
            array(
                'taxonomy'  => 'pa_dagaanbieding', // The taxonomy of this product attribute
                'term_name' => 'Ja', // The term name for this product attribute
            ), 
            $atts
        ));
        
        global $wpdb;
        
        // SQL query: To get the product ID from defined product attribute term name
        $product_id = $wpdb->get_var( $wpdb->prepare("
            SELECT tr.object_id
            FROM {$wpdb->prefix}term_relationships tr
            INNER JOIN {$wpdb->prefix}term_taxonomy tt
                ON tr.term_taxonomy_id = tt.term_taxonomy_id
            INNER JOIN {$wpdb->prefix}terms t
                ON tt.term_id = t.term_id
            WHERE tt.taxonomy = '%s'
            AND t.name = '%s'
        ", $taxonomy, $term_name ) );
    
        // Exit if there is no product Id
        if( ! $product_id ) return;

        // Get an instance of the product object
        $product = wc_get_product( $product_id );

        // Exit if the product object is not defined
        if( ! is_a( $product, 'WC_Product' ) ) return;

        // Displays go here
        return '<div class="row">
        <div class="col-md-6"><h3>'.$product->get_title().'</h3>'.$product->get_image().'<br><br><a href="'.$product->get_permalink().'"><button type="submit" class="single_add_to_cart_button button alt">Bekijk aanbieding</button></a></div>
        <div class="col-md-6">'.$product->get_price_html().'</div>
        </div>';
    }

    add_shortcode( 'product_ja', 'display_product_dagaanbieding_ja' );
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

USAGE: [product_ja] or echo do_shortcode('[product_ja]');

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement