【问题标题】:Showing all prices for variable products listed instead of range of prices显示列出的可变产品的所有价格而不是价格范围
【发布时间】:2020-08-09 17:49:46
【问题描述】:

有没有办法在商店页面上替换 woocommerce 中可变产品的范围价格,而不是显示变体列表及其每个产品的价格,以便向客户显示变体并为所有类型显示“添加到购物车”的产品不为可变产品“选择一个选项”。

我测试了很多代码sn-ps都没有成功。

下面的代码最接近我的需要,我使用这里建议的代码Get specific product attribute name and value for each WooCommerce variation of a variable product 并进行了更改。但问题仍然是部分属性没有显示。

add_filter( 'woocommerce_variable_price_html', 'custom_variable_price_html', 10, 2 );
function custom_variable_price_html( $price, $product ) {
    
    if(!is_admin()){
        
        if(is_shop()){

            $product_attribute_slug = 'ابعاد-فرش';

            $targeted_taxonomy = 'pa_' . $product_attribute_slug;

            $available_variations = $product->get_available_variations();

            $output = '<div>';

            foreach( $available_variations as $variation_data ){

                if( $variation_data['display_price'] === $variation_data['display_regular_price'] ) {
                    $price_to_display = wc_price( $variation_data['display_price'] );
                } else {
                    $variation = wc_get_product($variation_data['variation_id']); // Get the WC_Product_Variation Object
                    $price_to_display = wc_get_price_to_display( $variation, array('price' => $variation->get_sale_price() ) );
                }

                foreach ( $variation_data['attributes'] as $variation_attribute => $term_slug ) {
                    // Get the taxonomy slug
                    $taxonomy = str_replace( 'attribute_', '', $variation_attribute );

                    // Get the correct product attribute term value for this variation
                    if( $taxonomy === $targeted_taxonomy ) {
                        // Get the attribute value term name
                        $term_name = get_term_by( 'slug', $term_slug, $taxonomy )->name;
                    }
                }

                $output .= '<span>' . $term_name . ':' . wc_attribute_label( $targeted_taxonomy ) . ':' . strip_tags($price_to_display) . '</span><br>';
            }

            $output .= '</div>';

            $price = $output;

            return $price;
        } else {
            
            return $price;
        }
    
    } else {
        
        return $price;
    }
}

我得到的是:

但我需要这样的东西:

我注意到了

$term_name

代码中的变量为空。

感谢任何帮助。

【问题讨论】:

  • 我投票结束这个问题,因为问题中没有提供真正的代码尝试。您要问的是一个真正的发展,其中一次包括许多问题,所以太宽泛了。另请注意,StackOverFlow 不是免费的编码服务。
  • @LoicTheAztec 我为我的问题添加了更多细节。谢谢。

标签: php wordpress woocommerce plugins


【解决方案1】:

解决方法:如下设置分类并在get_term_by()函数中使用:

$taxonomy = 'pa_ابعاد-فرش';

【讨论】:

    猜你喜欢
    • 2013-04-20
    • 1970-01-01
    • 2011-06-26
    • 2017-11-24
    • 2022-07-07
    • 2020-11-13
    • 2019-02-16
    • 1970-01-01
    相关资源
    最近更新 更多