【问题标题】:Add custom field value after selected WooCommerce product variation price在选择 WooCommerce 产品变体价格后添加自定义字段值
【发布时间】:2021-01-18 14:09:23
【问题描述】:

我们为简单的产品添加了一个选择字段,这样我们就可以定义价格单位,并在单个产品页面上显示价格后的单位。这很好用。

此外,我们还为可​​变产品添加了一个选择字段,以定义每个变体的价格单位。此价格单位应显示在产品页面上的变化价格之后:

<?php

function mytheme_woo_add_custom_variation_fields( $loop, $variation_data, $variation ) {

    echo '<div class="options_group form-row form-row-full">';

    $value = get_post_meta( $post->ID, '_variable_select_field', true );
    if( empty( $value ) ) $value = '';
    // Select
    woocommerce_wp_select(
        array(
            'id'      => '_variable_select_field[' . $variation->ID . ']',
            'label'   => __( 'Mengeneinheit', 'woocommerce' ),
            'value' => get_post_meta( $variation->ID, '_variable_select_field', true ),
            'options' => array(
                ''          => __( 'ohne', 'woocommerce' ),
                'Stück'     => __( 'Stück', 'woocommerce' ),
                'Paar'      => __( 'Paar', 'woocommerce' ),
                'Karton'   => __( 'Karton', 'woocommerce' ),
                'Packung'   => __( 'Packung', 'woocommerce' ), 
                'Set'   => __( 'Set', 'woocommerce' ),              
                'lfm'       => __( 'Laufmeter', 'woocommerce' ),
                'm²'        => __( 'm2', 'woocommerce' ),
                )
        )
    );
    echo '</div>';
}

// Variations tab
add_action( 'woocommerce_variation_options_pricing', 'mytheme_woo_add_custom_variation_fields', 10, 3 );


// Add custom field for variations
function load_variation_settings_fields( $variations ) {
$variations['_variable_select_field'] = get_post_meta( $variations[ 'variation_id' ], '_variable_select_field', true );
  return $variations;
}

add_filter( 'woocommerce_available_variation', 'load_variation_settings_fields' );

//Save variable product field
function mytheme_woo_add_custom_variation_fields_save( $post_id ){

    $woocommerce_select_field = $_POST['_variable_select_field'][ $post_id ];
    update_post_meta( $post_id, '_variable_select_field', esc_attr( $woocommerce_select_field ) );
}
add_action( 'woocommerce_save_product_variation', 'mytheme_woo_add_custom_variation_fields_save', 10, 2 );


/* Display the custom field after the price */
function mytheme_display_woo_custom_fields( $price ){
    global $post, $product;

    if ( $product->is_type( 'variable' ) ){
        $variation_price_unit = get_post_meta( $post->ID, '_variable_select_field', true );
        $price .= $variation_price_unit;
    }
        return $price;  
}

add_action( 'woocommerce_get_price_html', 'mytheme_display_woo_custom_fields', 15 );



但是,它不起作用,我找不到正确的方法来做到这一点。 谁能告诉我如何在价格之后显示产品变化中保存的价值?

谢谢!

【问题讨论】:

    标签: php wordpress woocommerce price product-variations


    【解决方案1】:

    这个简单的代码sn-p会在选择变化价格后显示相关的价格单位如下:

    add_filter( 'woocommerce_available_variation', 'display_price_unit_after_variations_price_html', 10, 3) ;
    function display_price_unit_after_variations_price_html( $data, $product, $variation ) {
        if ( $mengeneinheit = $variation->get_meta('_variable_select_field') )
            $data['price_html'] .= ' ' . $mengeneinheit;
    
        return $data;
    }
    

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

    您应该在任何地方重命名密钥 _variable_select_field,例如 _mengeneinheit,因为它会更明确(或英文 _price_unit)。

    【讨论】:

    • 感谢您的帮助 LoicTheAztec!
    猜你喜欢
    • 1970-01-01
    • 2015-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-30
    • 1970-01-01
    • 1970-01-01
    • 2016-09-12
    相关资源
    最近更新 更多