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

标签 php sql wordpress woocommerce shortcode

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

每天都会有一个新产品,其属性为 DAGAANBIEDING 且值为 JANEE

我只想展示具有值(value) 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' );
}

但它不显示信息。

最佳答案

您的代码中存在一些错误和缺失的内容。请尝试以下操作,使用自定义轻型 SQL 查询获取具有 Ja 术语名称的 pa_dagaanbieding 产品属性(分类)的产品:

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

关于php - 从 WooCommerce 中的特定产品属性术语名称获取产品,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62877351/

相关文章:

php - setlocale 函数的范围?

php - 是否有智能的 PHP Emacs 标记解决方案?

c# - 可以在 SQL FROM 语句中使用 SQLParameter 吗?

php - 在 Woocommerce 的管理员订单列表中添加一列优惠券

php - Woocommerce 3 商店页面上“添加到购物车”下的“添加额外”按钮

php - PHP解析/语法错误;以及如何解决它们

php - mysqli_fetch_array() 期望参数 1 为 mysqli_result,给出 PHP 论坛尝试的数组

sql - Hadoop:为什么即使在很小的 table 上,Hive的工作速度仍然如此缓慢?

sql - 了解 SQL 中索引的基础知识

php - 在 Woocommerce 的管理编辑订单页面上显示客户来源国家/地区