【问题标题】:Display specific the selected product attribute on the WooCommerce variation price在 WooCommerce 变体价格上显示特定的选定产品属性
【发布时间】:2019-08-28 06:25:03
【问题描述】:

我正在尝试在 .

我发现了许多代码,它们甚至都在工作,但所有这些代码都返回了所有可用属性的列表。

add_action( 'woocommerce_before_variations_form', 'display_attribute', 5 );
function display_attribute(){
    global $product;
    $output = array();
    if( $verpackung = $product->get_attribute('pa_verpackung') ){
        echo $verpackung;
        $output[] = $verpackung;
    }
}

这是一个可变产品,我想显示包装的大小,这是一个属性。例如:39 欧元,一盒 300 克。

【问题讨论】:

    标签: php wordpress woocommerce price variations


    【解决方案1】:

    以下将在选择的变化价格(可变产品)之后显示相应的包装类型,例如:“€ 39,- for a box of 300g”...

    add_filter( 'woocommerce_available_variation', 'set_packaging_type_after_variation_price', 10, 3 );
    function set_packaging_type_after_variation_price( $data, $product, $variation ) {
        $targeted_taxonomy  = 'pa_verpackung'; // <== Define here the product attribute taxonomy
    
        // Loop through variation attributes
        foreach( $data['attributes'] as $variation_attribute => $term_slug ) {
            $attribute_taxonomy = str_replace( 'attribute_', '', $variation_attribute ); // Get product attribute the taxonomy
    
            // For matching variation attribute …
            if ( $attribute_taxonomy == $targeted_taxonomy ){
                $verpackung  = get_term_by( 'slug', $term_slug, $attribute_taxonomy )->name; // Get the term name
                $verpackung = ' <span class="verpackung">'. __('für eine '). $verpackung .'</span></span>';
                // Set the "packaging" after the selected viariation price
                $data['price_html'] = str_replace( '</span></span>', '</span>' . $verpackung, $data['price_html'] );
                break; // Stop the loop
            }
        }
        return $data;
    }
    

    代码进入您的活动子主题(或活动主题)的 function.php 文件中。经过测试并且可以工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-07-22
      • 2017-11-24
      • 1970-01-01
      • 2020-08-21
      • 2019-04-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多