【问题标题】:Add x to meta_value and display result in Woocommerce将 x 添加到 meta_value 并在 Woocommerce 中显示结果
【发布时间】:2019-12-07 05:27:12
【问题描述】:

我创建了一个函数来向 woocommerce 产品管理屏幕添加一列,然后获取键 ae_cost_pkr 的元值并将 5 添加到它并在新创建的列中回显结果。它有效,但我遇到了一个问题,即原始元值附加到相邻列。我做错了什么?

在这张图片中可以看到错误:ibb.co/n3y6n2n

我的功能是:

// add
add_filter( 'manage_edit-product_columns', 'my_total_sales_1', 20 );
// populate
add_action( 'manage_posts_custom_column', 'my_total_sales_2' );


function my_total_sales_1( $col_th ) {

    // create colunm
    return wp_parse_args( array( 'ae_cost_pkr' => 'AE Price' ), $col_th );

}

// add 5 to the meta value and display in column
function my_total_sales_2( $column_id ) {

    if( $column_id  == 'ae_cost_pkr' )

    $re = get_post_meta( get_the_ID(), 'ae_cost_pkr', true );
    echo $re + 5; 

}

【问题讨论】:

    标签: wordpress woocommerce


    【解决方案1】:

    试试下面的代码,如果需要加加5就需要更新meta

    // add
    add_filter( 'manage_edit-product_columns', 'my_total_sales_1', 20 );
    // populate
    add_action( 'manage_posts_custom_column', 'my_total_sales_2' );
    
    
    function my_total_sales_1( $col_th ) {
    
        // create colunm
        return wp_parse_args( array( 'ae_cost_pkr' => 'AE Price' ), $col_th );
    
    }
    
    // add 5 to the meta value and display in column
    function my_total_sales_2( $column_id ) {
    
        if( $column_id  == 'ae_cost_pkr' )
        $readd = '5';
        $re = update_post_meta( get_the_ID() , 'ae_cost_pkr', $readd);
        echo $re;
    }
    

    【讨论】:

    • 这会永久改变元值吗?或者只要此功能处于活动状态,值就会改变?我不打算永久更改元值。
    • 但我不打算永久更改它。我只是想看看如果我加 5 会是什么结果。
    • $re = '5'; $re = $re + get_post_meta(get_the_ID(), 'ae_cost_pkr', true );回声$重新;可以试试这个
    • 但这会产生错误并将先前的值附加到相邻列,如下所示:ibb.co/n3y6n2n
    猜你喜欢
    • 2016-12-05
    • 2017-01-14
    • 1970-01-01
    • 1970-01-01
    • 2022-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多