如何在管理面板的WooCommerce订单中显示自定义选项字段?

时间:2020-06-28 作者:piyush arora

实际上,这个问题已经回答了,因为我已经检查过了,但我无法对这个问题发表评论,因为我使用的是相同的代码,但在我的场景中,我使用的是选项字段,并且值没有反映在管理面板中,下面是我尝试的代码

    //*Multiple Options Php - Select Occasion*/

add_filter( \'woocommerce_checkout_fields\' , \'my_custom_checkout_field_update_order_meta\' );

function my_custom_checkout_field_update_order_meta( $fields ) { 
     $fields[\'billing\'][\'myfield\'] = array(
        \'type\'      => \'select\',
        \'id\' => \'myfield\',
        \'placeholder\'     => __(\'Select Occasion\', \'woocommerce\'),
        \'required\'  => false,
        \'class\'     => array(\'form-row-wide\'),
        \'clear\'     => false,
        \'options\'       => [
            \'key1\'  => __(\'Birthday\', \'woocommerce\'),
            \'key2\'  => __(\'Anniversary\', \'woocommerce\'),
            \'key3\'  => __(\'Congratulations\', \'woocommerce\'),
            \'key4\'  => __(\'Sorry\', \'woocommerce\'),
            \'key5\'  => __(\'Good Luck\', \'woocommerce\'),
            \'key6\'  => __(\'Thank You\', \'woocommerce\'),
            \'key7\'  => __(\'Get Well Soon\', \'woocommerce\'),
            \'key8\'  => __(\'Love\', \'woocommerce\'),
        ]
    );
    return $fields;
}

add_action( \'woocommerce_admin_order_data_after_billing_address\', \'edit_woocommerce_checkout_page\', 10, 1 );

function edit_woocommerce_checkout_page($order){
    global $post_id;
    $order = new WC_Order( $post_id );
    echo \'<p><strong>\'.__(\'Occasion\').\':</strong> \' . get_post_meta($order->get_id(), \'_myfield\', true ) . \'</p>\';
}

screenshot

1 个回复
SO网友:Tanmay Patel

您忘记保存自定义元数据字段数据,所以请在下面的代码中进行检查。Reference

add_filter( \'woocommerce_checkout_fields\' , \'occasion_checkout_field_update_order_meta\' );
function occasion_checkout_field_update_order_meta( $fields ) { 
$fields[\'billing\'][\'occasion\'] = array( \'type\' => \'select\', 
    \'id\' => \'occasion\', 
    \'placeholder\' => __(\'Select Occasion\', \'woocommerce\'), 
    \'required\' => false, 
    \'class\' => array(\'form-row-wide\'), 
    \'clear\' => false, 
    \'options\' => array( 
        \'Birthday\' => __(\'Birthday\', \'woocommerce\'), 
        \'Anniversary\' => __(\'Anniversary\', \'woocommerce\'), 
        \'Congratulations\' => __(\'Congratulations\', \'woocommerce\'), 
        \'Sorry\' => __(\'Sorry\', \'woocommerce\'), 
        \'Good Luck\' => __(\'Good Luck\', \'woocommerce\'), 
        \'Thank You\' => __(\'Thank You\', \'woocommerce\'), 
        \'Get Well Soon\' => __(\'Get Well Soon\', \'woocommerce\'), 
        \'Love\' => __(\'Love\', \'woocommerce\')
    )
); 
return $fields; 
}
add_action( \'woocommerce_checkout_update_order_meta\', \'occasion_checkout_update_order_meta\' );
function occasion_checkout_update_order_meta( $order_id ){
    if( !empty( $_POST[\'occasion\'] ) )
        update_post_meta( $order_id, \'_occasion\', sanitize_text_field( $_POST[\'occasion\'] ) );
}
add_action( \'woocommerce_admin_order_data_after_billing_address\', \'occasion_edit_woocommerce_checkout_page\', 10, 1 );
function occasion_edit_woocommerce_checkout_page($order){ 
    echo __(\'Occasion\').\': \'.get_post_meta($order->id, \'_occasion\', true ); 
}

相关推荐

Functions.php上未定义$_GET和&_REQUEST索引

我最近尝试学习为我的自定义主题创建一个主题选项页面,这是按照stackoverflow和其他资源的教程进行的。但脚本显示错误if ($_GET[\'page\'] == basename(__FILE__)) { if (\'save\' == $_REQUEST[\'formaction\']) { foreach ($options as $value) { if( isset( $_REQUEST[ $value[\'id\']