从 WooCommerce 中的特定产品属性术语名称获取产品

我正在尝试创建一个 WooCommerce 短代码来显示带有一些信息的一种产品。


每天都会有一个具有属性DAGAANBIEDING和价值的新产品JA或NEE。


我只想展示该产品的价值JA。

短代码正在运行,但没有显示任何内容。

这就是我所拥有的:


//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' );

}

但它不显示信息。


青春有我
浏览 82回答 1
1回答

UYOU

您的代码中存在一些错误和遗漏的内容。尝试以下操作,使用自定义轻型 SQL 查询获取术语名称具有pa_dagaanbieding产品属性(分类法)的产品:Jaif( !function_exists('display_product_dagaanbieding_ja') ) {&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; function display_product_dagaanbieding_ja( $atts ) {&nbsp; &nbsp; &nbsp; &nbsp; // Attributes&nbsp; &nbsp; &nbsp; &nbsp; extract( shortcode_atts(&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'taxonomy'&nbsp; => 'pa_dagaanbieding', // The taxonomy of this product attribute&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'term_name' => 'Ja', // The term name for this product attribute&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ),&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $atts&nbsp; &nbsp; &nbsp; &nbsp; ));&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; global $wpdb;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; // SQL query: To get the product ID from defined product attribute term name&nbsp; &nbsp; &nbsp; &nbsp; $product_id = $wpdb->get_var( $wpdb->prepare("&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SELECT tr.object_id&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FROM {$wpdb->prefix}term_relationships tr&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; INNER JOIN {$wpdb->prefix}term_taxonomy tt&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ON tr.term_taxonomy_id = tt.term_taxonomy_id&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; INNER JOIN {$wpdb->prefix}terms t&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ON tt.term_id = t.term_id&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WHERE tt.taxonomy = '%s'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; AND t.name = '%s'&nbsp; &nbsp; &nbsp; &nbsp; ", $taxonomy, $term_name ) );&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; // Exit if there is no product Id&nbsp; &nbsp; &nbsp; &nbsp; if( ! $product_id ) return;&nbsp; &nbsp; &nbsp; &nbsp; // Get an instance of the product object&nbsp; &nbsp; &nbsp; &nbsp; $product = wc_get_product( $product_id );&nbsp; &nbsp; &nbsp; &nbsp; // Exit if the product object is not defined&nbsp; &nbsp; &nbsp; &nbsp; if( ! is_a( $product, 'WC_Product' ) ) return;&nbsp; &nbsp; &nbsp; &nbsp; // Displays go here&nbsp; &nbsp; &nbsp; &nbsp; return '<div class="row">&nbsp; &nbsp; &nbsp; &nbsp; <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>&nbsp; &nbsp; &nbsp; &nbsp; <div class="col-md-6">'.$product->get_price_html().'</div>&nbsp; &nbsp; &nbsp; &nbsp; </div>';&nbsp; &nbsp; }&nbsp; &nbsp; add_shortcode( 'product_ja', 'display_product_dagaanbieding_ja' );}代码位于活动子主题(或活动主题)的 function.php 文件中。经过测试并有效。用法: [product_ja]或echo do_shortcode('[product_ja]');
打开App,查看更多内容
随时随地看视频慕课网APP