Ver*_*rse 3 php wordpress custom-fields orders woocommerce
与供应商(WC供应商)一起开发WooCommerce网上商店.
我需要显示我在供应商资料中创建的自定义字段.它应显示在项目和供应商名称下order-details.php.
如何按卖家/供应商ID显示个人资料字段?
有人可以帮帮我吗?
这是我将要撒谎的屏幕截图:
配置自定义字段
向用户个人资料页面添加自定义字段
add_action( 'show_user_profile', 'wp_added_user_profile_fields' );
function wp_added_user_profile_fields( $user ) {
?>
<table class="form-table">
<tr>
<th><label for="billing_enumber"><?php _e( "eNumber", 'woocommerce' ); ?></label></th>
<td>
<input type="text"
name="billing_enumber"
id="billing_enumber"
class="regular-text"
value="<?php echo esc_attr( get_the_author_meta( 'billing_enumber', $user->ID ) ); ?>"/>
<span class="description"><?php _e( 'Please enter your eNumber.', 'woocommerce' ); ?></span>
</td>
</tr>
</table>
<?php
}
Run Code Online (Sandbox Code Playgroud)
将更新功能添加到用户配置文件的自定义字段
add_action( 'edit_user_profile', 'wp_added_user_profile_fields' );
function wp_save_added_user_profile_fields( $user_id ) {
if ( current_user_can( 'edit_user', $user_id ) ) {
update_user_meta( $user_id, 'billing_enumber', trim($_POST['billing_enumber'] ) );
$saved = true;
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
谢谢.
您可以通过2个步骤以干净的方式完成:
步骤1)您需要首先在产品中添加一个属性,以获得自定义字段值的"可读标签",该字段值将显示为订单商品元数据.
在您的情况下,您将创建"Billing E Number"属性:
然后,您将在目标产品中设置任意值(因为它将被您的自定义字段值替换),该值可以是简单的或可变的.如果未使用此属性设置值,则在更新产品时不会设置和保存该值.
然后,您将在保存和更新后获得此信息:
然后,woocommerce中的属性slu starts开始pa_.所以你的属性slug将是:pa_billing-e-number
我们将在下面的函数中使用它,以显示自定义字段值的可读标签.所以你会得到订单项目:Billing E Number: (some value)
步骤2)您的自定义函数挂钩在
woocommerce_add_order_item_meta动作钩子中.
现在,在订单项的细节,以显示您的自定义字段,你将需要得到提交的值保存为订单项目元数据,我们将在这里使用pa_billing-e-Number的meta_key.
因此代码将简单地类似于:
add_action('woocommerce_add_order_item_meta', 'add_custom_order_item_meta_data', 1, 3 );
function add_custom_order_item_meta_data( $item_id, $values, $cart_item_key ) {
global $order;
// Get the user ID
$user_id = get_post_meta( $order->id, '_customer_user', true );
// Get User custom field value for 'billing_enumber'
$billing_e_number = get_user_meta( $user_id, 'billing_enumber', true );
// Setting this custom field in order item meta
if(!empty($billing_e_number))
wc_add_order_item_meta($item_id, 'pa_billing-e-number', $billing_e_number, true);
}
Run Code Online (Sandbox Code Playgroud)
此代码位于活动子主题(或主题)的function.php文件中,或者也可以放在任何插件文件中.
然后在我的帐户>订单>订单视图的前端,您将得到:
正如您所看到的,您在woocommerce正常行为中得到了类似的东西.这里的值只是为了说明这个例子......
这个例子真的经过测试并且有效.
| 归档时间: |
|
| 查看次数: |
2943 次 |
| 最近记录: |