亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從 WooCommerce 中的特定產品屬性術語名稱獲取產品

從 WooCommerce 中的特定產品屬性術語名稱獲取產品

PHP
青春有我 2023-07-30 13:54:22
我正在嘗試創建一個 WooCommerce 短代碼來顯示帶有一些信息的一種產品。每天都會有一個具有屬性DAGAANBIEDING和價值的新產品JA或NEE。我只想展示該產品的價值JA。短代碼正在運行,但沒有顯示任何內容。這就是我所擁有的://custom shortcodesif( !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' );}但它不顯示信息。
查看完整描述

1 回答

?
UYOU

TA貢獻1878條經驗 獲得超4個贊

您的代碼中存在一些錯誤和遺漏的內容。嘗試以下操作,使用自定義輕型 SQL 查詢獲取術語名稱具有pa_dagaanbieding產品屬性(分類法)的產品:Ja


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

}

代碼位于活動子主題(或活動主題)的 function.php 文件中。經過測試并有效。


用法: [product_ja]或echo do_shortcode('[product_ja]');


查看完整回答
反對 回復 2023-07-30
  • 1 回答
  • 0 關注
  • 110 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號