【问题标题】:WooCommerce displaying variable description after variable priceWooCommerce 在可变价格后显示可变描述
【发布时间】:2016-12-07 15:09:59
【问题描述】:

我正在尝试将变量描述显示到 woocommerce 产品页面中。 我安装了一个名为 woocommerce 单选按钮的插件,用于将我的可变产品和价格显示为单选按钮而不是选择按钮。

我正在这个插件中编辑 variable.php 文件(完成后我会将它转移到我的子主题),基本上我需要将变量描述显示为标签,作为名为 $variable_description 的变量。

    printf( '<div>
    <input type="radio" name="%1$s" value="%2$s" id="%3$s" %4$s>
    <label for="%3$s">%5$s</label>
    <label>'.$variable_description.'</label>
    </div>', $input_name, $esc_value, $id, $checked, $filtered_label );

由于我不了解数据库的结构,我无法从数据库中恢复这些数据。

你知道任何短代码或函数来恢复它并显示每个可变价格的变量描述吗?

在我正在编辑的函数中,作为第一个标签的单选按钮旁边的每个变体都正确显示了价格。该函数的完整代码为:

if ( ! function_exists( 'print_attribute_radio' ) ) {
    function print_attribute_radio( $checked_value, $value, $label, $name, $product_id ) {

        // This handles < 2.4.0 bw compatibility where text attributes were not sanitized.
        $checked = sanitize_title( $checked_value ) === $checked_value ? checked( $checked_value, sanitize_title( $value ), false ) : checked( $checked_value, $value, false );

        $input_name = 'attribute_' . esc_attr( $name ) ;
        $esc_value = esc_attr( $value );
        $id = esc_attr( $name . '_v_' . $value );
        $filtered_label = apply_filters( 'woocommerce_variation_option_name', $label );

    //here is where I try to recover the variable_description

        global $wpdb;
        $post_id = $product_id + 3;

         $querystr = "SELECT wpostmeta.meta_value
                    FROM $wpdb->postmeta wpostmeta
                    WHERE wpostmeta.post_id = '$post_id'
                    AND wpostmeta.meta_key = '_variation_description'
                    ORDER BY wpostmeta.meta_value DESC
                    "; 


     $variable_description = $wpdb->get_var($querystr);

        printf( '<div>
        <input type="radio" name="%1$s" value="%2$s" id="%3$s" %4$s>
        <label for="%3$s">%5$s</label>
        <label>'.$variable_description.'</label>
        </div>', $input_name, $esc_value, $id, $checked, $filtered_label );

    }
}

谢谢

【问题讨论】:

    标签: php wordpress woocommerce product variations


    【解决方案1】:

    要获取发布元数据,您可以改用WordPress get_post_meta() 函数(它更短且价格合理)。

    所以你的代码现在应该是:

    if ( ! function_exists( 'print_attribute_radio' ) ) {
        function print_attribute_radio( $checked_value, $value, $label, $name, $product_id ) {
    
            // This handles < 2.4.0 bw compatibility where text attributes were not sanitized.
            $checked = sanitize_title( $checked_value ) === $checked_value ? checked( $checked_value, sanitize_title( $value ), false ) : checked( $checked_value, $value, false );
    
            $input_name = 'attribute_' . esc_attr( $name ) ;
            $esc_value = esc_attr( $value );
            $id = esc_attr( $name . '_v_' . $value );
            $filtered_label = apply_filters( 'woocommerce_variation_option_name', $label );
    
            // HERE the product variation description
            $variation_id = $product_id + 3;
            $variable_description = get_post_meta( $variation_id, '_variation_description', true );
    
            printf( '<div>
            <input type="radio" name="%1$s" value="%2$s" id="%3$s" %4$s>
            <label for="%3$s">%5$s</label>
            <label>'.$variable_description.'</label>
            </div>', $input_name, $esc_value, $id, $checked, $filtered_label );
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-11-24
      • 2020-02-24
      • 2015-10-10
      • 2019-04-26
      • 1970-01-01
      • 1970-01-01
      • 2022-01-03
      • 1970-01-01
      • 2018-11-13
      相关资源
      最近更新 更多