Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Woocommerce 将商业订单产品名称保存到用户元自定义字段_Woocommerce_Custom Fields - Fatal编程技术网

Woocommerce 将商业订单产品名称保存到用户元自定义字段

Woocommerce 将商业订单产品名称保存到用户元自定义字段,woocommerce,custom-fields,Woocommerce,Custom Fields,警告:我是一个独立的自由设计师,不是开发人员;-) 我在Wordpress用户元中创建了一个名为membership的自定义字段 我已尝试使用以下代码在签出时将WooCommerce产品名称保存到会员资格自定义字段 更新的尝试: function wascc_woocommerce_checkout_update_user_meta_membership ( $customer_id, $posted ) { if (isset($posted['$orderid'])) {

警告:我是一个独立的自由设计师,不是开发人员;-)

我在Wordpress用户元中创建了一个名为
membership
的自定义字段

我已尝试使用以下代码在签出时将WooCommerce产品名称保存到
会员资格
自定义字段

更新的尝试

function wascc_woocommerce_checkout_update_user_meta_membership ( $customer_id, $posted ) {

    if (isset($posted['$orderid'])) {
        $order = $posted['$orderid'];
    }
    $theorder = new WC_Order( $order );
    $items = $theorder->get_items();

    foreach ( $items as $item ) {
        $product_name = $item['name'];
    }

    if (!(empty($product_name))) {
        update_user_meta( $customer_id, 'membership', $product_name);
    }   

}

add_action( 'woocommerce_checkout_update_user_meta', 'wascc_woocommerce_checkout_update_user_meta_membership', 10, 2 );
它不会产生任何错误,但不会将产品名称保存到
成员身份

谢谢你的帮助


.

正如我所评论的,您应该使用
woocmerce\u checkout\u update\u order\u meta

大概是这样的:

function wascc_woocommerce_checkout_update_user_meta_membership ( $order_id ) {

    $theorder = new WC_Order( $order_id );
    $items = $theorder->get_items();

    foreach ( $items as $item ) {
        $product_name = $item['name'];
    }

    if (!(empty($product_name))) {

        // Gets the customer/user ID associated with the order. Guests are 0.
        $customer_id = $theorder->get_user_id();

        update_user_meta( $customer_id, 'membership', $product_name );

    }   

}

add_action( 'woocommerce_checkout_update_order_meta', 'wascc_woocommerce_checkout_update_user_meta_membership' );

但是我对你的foreach循环有疑问。。。您循环只是为了获取最后一项?

请重新阅读@Reigel的答案,您已接受该答案。第二个参数是
$posted
,它包含
$\u POST[]
值,而不是您在上述代码中使用的
$order\u id
。由于代码找不到有效的订单,因此无法保存元数据。请使用上一个问题中的
woocmerce\u checkout\u update\u order\u meta
。。。它有订单id。。。您可以从订单id获取客户id。。。我现在无法提供完整的代码,因为我在手机上…我看不到订单id在
woocommerce\u checkout\u update\u order\u meta
。我已经更新了上面的尝试@Reigel.Yes!:-)谢谢你,雷格尔。我对我的foreach循环也有同样的想法,但没有研究如何获取最后一项。我会这样做。