【问题标题】:Displaying custom field of product variations on Woocommerce cart在 Woocommerce 购物车上显示产品变体的自定义字段
【发布时间】:2018-08-28 13:23:12
【问题描述】:

在 Woocommerce 中,我成功地在后端为产品变体添加了一个自定义字段。保存其输入数据并在add-to-cart/variation.php 中显示也可以。

但是如何在cart.php 中显示该字段的值呢?

我只找到了针对常规产品项目的解决方案,而不是针对产品变体的解决方案。这是我的functions.php文件中到目前为止的内容:

//Display Fields in admin on product edit screen
add_action( 'woocommerce_product_after_variable_attributes', 'woo_variable_fields', 10, 3 );

//Save variation fields values
add_action( 'woocommerce_save_product_variation', 'save_variation_fields', 10, 2 );

// Create new fields for variations
function woo_variable_fields( $loop, $variation_data, $variation ) {

echo '<div class="variation-custom-fields">';

  // Text Field
  woocommerce_wp_text_input( 
    array( 
      'id'          => '_text_field['. $loop .']', 
      'label'       => __( 'additional fees (e.g. monthly fee)', 'woocommerce' ), 
      'placeholder' => '',
      //'desc_tip'    => true,
      'wrapper_class' => 'form-row form-row-first',
      //'description' => __( 'Enter the custom value here.', 'woocommerce' ),
      'value'       => get_post_meta($variation->ID, '_text_field', true)
    )
  );

echo "</div>"; 

}

/** Save new fields for variations */
function save_variation_fields( $variation_id, $i) {

// Text Field
$text_field = stripslashes( $_POST['_text_field'][$i] );
update_post_meta( $variation_id, '_text_field', esc_attr( $text_field ) );
}

// Custom Product Variation
add_filter( 'woocommerce_available_variation', 'custom_load_variation_settings_products_fields' );

function custom_load_variation_settings_products_fields( $variations )    { 
$variations['variation_custom_field'] = get_post_meta( $variations[ 'variation_id' ], '_text_field', true );  
return $variations;
}

【问题讨论】:

    标签: php woocommerce cart product variations


    【解决方案1】:

    要在购物车项目中显示此自定义字段,请使用以下命令:

    add_filter( 'woocommerce_get_item_data', 'display_custom_field_as_item_data', 20, 2 );
    function display_custom_field_as_item_data( $cart_data, $cart_item ) {
        if( $value = get_post_meta( $cart_item['data']->get_id(), '_text_field', true ) ){
            $cart_data[] = array(
                'name' => __( 'Additional Monthly Fee', 'woocommerce' ),
                'value' => sanitize_text_field( $value )
            );
        }
        return $cart_data;
    }
    

    它也适用于购物车和结帐页面中的所有产品发布类型和产品变体。

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

    【讨论】:

    • 感谢 LoicTheAztec!这很好,到目前为止有效。有没有机会在 cart.php 中的某个位置显示这个值,比如右边的总价下方?我尝试了类似echo get_post_meta( get_the_ID(), '_text_field', true ); 的东西,但没有返回任何东西。 echo get_post_meta( $_product-&gt;variation_id, ‘_text_field’, true );两者都没有..
    • @Bradley 因为这是一个购物车项目,它需要留在购物车项目循环动态表部分(因为您可以在购物车中有多个项目)。您可以更改表格,覆盖留在 foreach 循环内的模板 cart.php... 工作代码是:get_post_meta( $_product-&gt;get_id(), '_text_field', true ); 适用于所有产品类型甚至产品变体...
    • 终于!我现在覆盖子主题中的 cart.php,它在我想要的位置显示自定义字段的值,尽管附加值当然不会与项目数量相乘。但就目前而言,这让我离我想要的地方更近了一步。非常感谢!
    • 我现在禁用了此产品类型的数量功能,因为无论如何数量在这种情况下都没有意义。最后一个问题。有没有办法在购买电子邮件中显示字段的值?
    • 嗯,是否有像您上面提供的那样的钩子,用于在产品价格/购物车/结帐等中每件商品的总价下方显示字段的值?
    猜你喜欢
    • 2018-07-16
    • 1970-01-01
    • 2016-12-26
    • 2019-05-11
    • 2021-07-24
    • 2019-07-21
    • 1970-01-01
    • 1970-01-01
    • 2017-10-21
    相关资源
    最近更新 更多