php – 在Woocommerce存档页面上显示特定的产品属性

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – 在Woocommerce存档页面上显示特定的产品属性脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我想在每个产品的商店商店页面显示我选择的一些特定产品属性.必须显示属性名称并与其值相反.我开始编写代码,我想打印至少名称,但我只显示最后一个属性名称

add_action('woocommerce_after_shop_loop_ITem','add_attribute');
function add_attribute() {
    global $PRoduct;
    $weigth_val = $product->get_attribute('weight');
    $quant_val = $product->get_attribute('quantity');
    $length_val = $product->get_attribute('length');
    echo $weigth_val;
    echo $quant_val;
    echo $length_val;
}

解决方法

在woocommerce中,每个产品属性都是一个自定义分类,并记录在数据库中,将pa_添加到其slugs的开头…

分类名称将与WC_Product get_attribute()方法一起使用.

所以你的代码应该是这样的:

add_action('woocommerce_after_shop_loop_item','displaying_product_attributes');
function displaying_product_attributes() {
    global $product;

    $weigth_val = $product->get_attribute('pa_weight');
    $quant_val  = $product->get_attribute('pa_quantity');
    $length_val = $product->get_attribute('pa_length');

    echo $weigth_val;
    echo $quant_val;
    echo $length_val;
}

它现在应该工作……

获取产品属性名称标签以及您将使用的产品的相应名称值:

add_action('woocommerce_after_shop_loop_item','add_attribute');
function add_attribute() {
    global $product;

    $product_attributes = array( 'pa_weight','pa_quantity','pa_length','pa_color' );
    $attr_output = array();

    // Loop through the array of product attributes
    foreach( $product_attributes as $taxonomy ){
        if( taxonomy_exists($taxonomy) ){
            $label_name = get_taxonomy( $taxonomy )->labels->singular_name;
            $value = $product->get_attribute('pa_weight');

            if( ! empty($value) ){
                // Storing attributes for output
                $attr_output[] = '<span class="'.$taxonomy.'">'.$label_name.': '.$value.'</span>';
            }
        }
    }

    // Output attribute name / value pairs separate by a "<br>"
    echo '<div class="product-attributes">'.implode( '<br>',$attr_output ).'</div>';
}

代码位于活动主题(或活动主题)的function.PHP文件中.经过测试和工作.

脚本宝典总结

以上是脚本宝典为你收集整理的php – 在Woocommerce存档页面上显示特定的产品属性全部内容,希望文章能够帮你解决php – 在Woocommerce存档页面上显示特定的产品属性所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。