php添加商品信息,php – 添加用户自定义字段值以订购商品详细信息

与供应商(WC供应商)一起开发WooCommerce网上商店.

我需要显示我在供应商资料中创建的自定义字段.它应显示在order-details.php中的项目和供应商名称下.

如何按卖家/供应商ID显示个人资料字段?

有人可以帮帮我吗?

这是我将要撒谎的屏幕截图:

oPp76.jpg

配置自定义字段

向用户个人资料页面添加自定义字段

add_action( 'show_user_profile', 'wp_added_user_profile_fields' );

function wp_added_user_profile_fields( $user ) {

?>

<?php _e( "eNumber", 'woocommerce' ); ?>

name="billing_enumber"

id="billing_enumber"

class="regular-text"

value="<?php echo esc_attr( get_the_author_meta( 'billing_enumber', $user->ID ) ); ?>"/>

}

将更新功能添加到用户配置文件的自定义字段

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;

}

谢谢.

解决方法:

您可以通过2个步骤以干净的方式完成:

STEP 1) You will need first to add an attribute in your products to get a “readable label” for your custom field value that is going to appear as order items meta data.

在您的情况下,您将创建“Billing E Number”属性:

xMe2h.png

然后,您将在目标产品中设置任意值(因为它将被您的自定义字段值替换),该值可以是简单的或可变的.如果未使用此属性设置值,则在更新产品时不会设置和保存该值.

F1ITE.png

然后,您将在保存和更新后获得此信息:

kTfu9.png

然后,woocommerce中的属性slugs以pa_开头.所以你的属性slug将是:pa_billing-e-number

我们将在下面的函数中使用它,以显示自定义字段值的可读标签.所以你会得到订单项目:Billing E Number :(某个值)

STEP 2) Your custom function hooked in woocommerce_add_order_item_meta action hook.

现在要在订单商品详细信息中显示您的自定义字段,您需要将提交的值保存为订单商品元数据,我们将在此处使用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);

}

此代码位于活动子主题(或主题)的function.php文件中,或者也可以放在任何插件文件中.

然后在我的帐户的前端>订单>订单视图,你会得到这个:

ERZP9.png

As you can see, you get exactly something similar at the woocommerce normal behavior. The value here is just to illustrate this example…

这个例子真的经过测试并且有效.

标签:orders,custom-fields,php,wordpress,woocommerce

来源: https://codeday.me/bug/20190823/1701507.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值