【问题标题】:WooCommerce: Get product variation custom field value in a hooked functionWooCommerce:在挂钩函数中获取产品变体自定义字段值
【发布时间】:2021-06-11 04:52:43
【问题描述】:

这就是我为产品变体添加管理自定义字段的方法:

add_action( 'woocommerce_variation_options_pricing', 'add_custom_field_to_variations', 10, 3 );
     
function add_custom_field_to_variations( $loop, $variation_data, $variation ) {
    woocommerce_wp_text_input( array(
        'id' => 'custom_field[' . $loop . ']',
        'class' => 'short',
        'label' => __( 'Custom Field', 'woocommerce' ),
        'value' => get_post_meta( $variation->ID, 'custom_field', true )
    ) );
}

保存产品变体保存的自定义字段:

add_action( 'woocommerce_save_product_variation', 'save_custom_field_variations', 10, 2 );
function save_custom_field_variations( $variation_id, $i ) {
    $custom_field = $_POST['custom_field'][$i];
    if ( isset( $custom_field ) ) update_post_meta( $variation_id, 'custom_field', esc_attr( $custom_field ) );
}

将自定义字段值存储到变体数据中

add_filter( 'woocommerce_available_variation', 'add_custom_field_variation_data' );
function add_custom_field_variation_data( $variations ) {
    $variations ['custom_field'] = '<div class="woocommerce_custom_field">Custom Field: <span>' . get_post_meta( $variations[ 'variation_id' ], 'custom_field', true ) . '</span></div>';
    return $variations;
}

数值在单品页面保存显示,但下面的函数没有返回数值

尝试在函数中检索自定义字段的值:

add_filter('woocommerce_variation_prices_price', 'get_custom_variable_price' )
function get_custom_variable_price() {

    global $post;
    $custom_value = get_post_meta( $variations[ 'variation_id' ], 'custom_field', true );
    $custom_value = (float)$custom_value;
    return $custom_value;   
}

我只需要获取值(浮点数)

【问题讨论】:

  • 你在哪里使用你的最后一个函数?...请添加更多上下文,因为您的问题实际上还不清楚...
  • 我在“woocommerce_variation_prices_price”上使用最后一个函数。目的是让自定义值乘以变体产品价格
  • 我已经编辑了你的问题,并回答了。请对以下答案提供一些反馈。

标签: php wordpress woocommerce custom-fields product-variations


【解决方案1】:

你的 las 函数中有一些错误和遗漏的东西(见下文)

现在有两种方法可以在该挂钩函数中获取自定义字段值:

1)。使用 WC_Data get_meta() 方法(从 WooCommerce 3 开始)

add_filter('woocommerce_variation_prices_price', 'custom_variable_price', 100, 3 );
function custom_variable_price( $price, $variation, $product ) {
    $value = floatval( $variation->get_meta( 'custom_field' ) );
    if( $value > 0 ) {
        $price = $value; 
    }
    return $price;
}

2)。或者使用 WordPress get_post_meta() 函数(旧方式)

add_filter('woocommerce_variation_prices_price', 'custom_variable_price', 100, 3 );
function custom_variable_price( $price, $variation, $product ) {
    $value = floatval( floatval( get_post_meta( $variation->get_id(), 'custom_field', true ) );
    if( $value > 0  ) {
        $price = $value; 
    }
    return $price;
}

代码位于活动子主题(或活动主题)的functions.php 文件中。经过测试,两者都适用。

重要提示:您可能在前端看不到结果,因为可变的产品价格范围被缓存在 WooCommerce 中以提高性能(请参阅下面的链接线程以获得更好的理解) em>。

相关话题:Change product prices via a hook in WooCommerce 3+


与您的其他功能相关的添加

从 WooCommerce 3 开始,您可以按如下方式更新您的第二个和第三个函数:

add_action( 'woocommerce_admin_process_variation_object', 'save_custom_field_variation_value', 10, 2 );
function save_custom_field_variation_value( $variation, $i ) {
    if( isset($_POST['custom_field'][$i]) ) {
        $variation->update_meta_data( 'custom_field', floatval( sanitize_text_field($_POST['custom_field'][$i]) ) );
    }
}

第三个功能

add_filter( 'woocommerce_available_variation', 'add_variation_custom_field_value_to_variation_data', 10, 3 );
function add_variation_custom_field_value_to_variation_data( $variation_data, $product, $variation ) {
    if ( $value = $variation->get_meta( 'custom_field' ) ) {
        $variation_data['custom_field'] = '<div class="woocommerce_custom_field">' .__("Custom Field") . ': <span>' . $value . '</span></div>';
    }
    return $variation_data;
}

【讨论】:

  • 嗨@loicTheAztec 感谢您的回复和努力。我尝试了这两个版本,但它无法正常工作并在前端显示严重错误。我想我没有问对,让我再解释一下;检查:stackoverflow.com/questions/45806249/… 你也回答了这个问题,我需要的是让自定义添加的变体字段值相乘。 2) 主题版:function get_price_multiplier() { return 2; // x2 for testing // I NEED TO LOAD CUSTOM FIELD VALUE HERE}
  • @Bilal 抱歉,两个代码都有错误……再试一次,现在应该可以了。如果这个答案回答了你的问题,你可以请accept回答,如果你喜欢/想要你也可以请upvote回答,谢谢。
  • 实际上它没有检索 custom_field ID 也没有 get_meta 工作,我也尝试使用 get_the_ID() 但它们都返回 null
  • 我已经解释了这里的要求; stackoverflow.com/questions/66735034/…
猜你喜欢
  • 2016-04-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-12
  • 2015-07-12
  • 2012-08-19
  • 2021-12-03
  • 1970-01-01
相关资源
最近更新 更多