【问题标题】:Get product custom attributes to display them in WooCommerce product loop获取产品自定义属性以在 WooCommerce 产品循环中显示它们
【发布时间】:2021-01-31 00:25:55
【问题描述】:

我在 woocommerce 网站(房地产)工作,我无法在我的首页上显示产品自定义属性(例如平方米、房间、厕所等)。我正在尝试使用下面的代码,但我可以让它工作。使用我拥有的代码,我只能显示产品 ID,但是当涉及到属性时,当我将属性 ID 放入代码中时,它只显示单词“数组”或仅显示“无”。

这是我的代码:

add_action('woocommerce_after_shop_loop_item_title', 'cstm_display_product_category', 5);

function cstm_display_product_category()
{
    global $product;

    $productAttribute = $product->get_id();

   //if(isset($productAttribute)){
       echo '<div class="items" style="color: #fff;"><p>Output: ' . $productAttribute . '</p></div>';
   //}
}

你可以看到实时输出here

无论您能给我什么帮助或指导以实现这一目标,我们都将不胜感激。我是这方面的菜鸟。

P.D.我将此代码插入到我的 functions.php 文件中。

Wordpress 是最新的。 Woocommerce 是最新的。

【问题讨论】:

    标签: php wordpress woocommerce attributes product


    【解决方案1】:

    更新 3

    使用产品属性(自定义或非自定义),您可以使用WC_Product 方法get_attribute(),您可以在其中输入属性名称、slug 或分类。所以在你的代码中:

    add_action('woocommerce_after_shop_loop_item_title', 'display_custom_product_attributes_on_loop', 5 );
    function display_custom_product_attributes_on_loop() {
        global $product;
    
        $value1 = $product->get_attribute('Square meters');
        $value2 = $product->get_attribute('Rooms');
        $value3 = $product->get_attribute('Toilets');
    
        if ( ! empty($value1) || ! empty($value2) || ! empty($value3) ) {
    
            echo '<div class="items" style="color: red;"><p>';
    
            $attributes = array(); // Initializing
    
            if ( ! empty($value1) ) {
                $attributes[] = __("Square meters:") . ' ' . $value1;
            }
    
            if ( ! empty($value2) ) {
                $attributes[] = __("Rooms:") . ' ' . $value2;
            }
    
            if ( ! empty($value3) ) {
                $attributes[] = __("Toilets:") . ' ' . $value3;
            }
    
            echo implode( '<br>', $attributes ) . '</p></div>';
        }
    }
    

    代码位于活动子主题(或活动主题)的functions.php 文件中。经过测试并且可以工作。


    从一个简单的产品属性名称数组中自动化代码:

    下面的代码将提供相同的输出,但它经过压缩、优化并且只需要一组您想要的产品属性:

    add_action('woocommerce_after_shop_loop_item_title', 'display_custom_product_attributes_on_loop', 5 );
    function display_custom_product_attributes_on_loop() {
        global $product;
    
        // Settings: Here below set your product attribute label names
        $attributes_names = array('Square meters', 'Rooms', 'Toilets');
    
        $attributes_data  = array(); // Initializing
    
        // Loop through product attribute settings array
        foreach ( $attributes_names as $attribute_name ) {
            if ( $value = $product->get_attribute($attribute_name) ) {
                $attributes_data[] = $attribute_name . ': ' . $value;
            }
        }
    
        if ( ! empty($attributes_data) ) {
            echo '<div class="items" style="color: red;"><p>' . implode( '<br>', $attributes_data ) . '</p></div>';
        }
    }
    

    代码位于活动子主题(或活动主题)的functions.php 文件中。经过测试并且可以工作。

    【讨论】:

    • 这个显示了除平方米以外的所有自定义属性,但我认为这与我的分类法有关。我会检查并看看发生了什么。谢谢您的帮助。我很高兴。
    猜你喜欢
    • 2016-11-24
    • 2021-06-13
    • 2012-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-13
    • 2019-06-26
    相关资源
    最近更新 更多