taxonomy – Get users meta and show to attribute


I have created a registration form (using plugin ultimate-member) for a partner as a salesperson. The key here is:

Label : Registration Number
Meta : ‘regno’
Value : X123W

So, each new registrant will get a different value as a salesperson unique ID .

On the other hand, I have created a product which is divided into 3 packages, separated by attribute with values ​​’pa_silver’, ‘pa_gold’, and ‘pa_diamond’.

‘pa_silver’ & ‘pa_gold’ => self-sold product (no salesperson required)
‘pa_diamond’ => product entrusted for selling (using salesperson).

Then on products that have taxonomy ‘pa_diamond’ I’ve added the attribute:
Label : List
tax : ‘pa_list’
value : ‘X123W’, ‘X234W’ => this has been created manually.

Targets : What I want is that every time someone registers as a partner, the partner code should be “added” automatically on existing attribute (‘pa_list’) and on products that have taxonomy ‘pa_diamond’.

I’m trying something, like:

1 Print partner code:

$users = get_users( array( 'role' => array( 'partner' ) ) );
foreach($users as $user){
$regno = get_the_author_meta( 'regno', $user->ID);

echo $regno;

Note : it output like this => X345WX456W (2 New partner code)

  1. Tried using the code I got from @LoicTheAztec’s answer here.

The code is :

$product_id = get_the_ID();
$taxonomy = 'pa_list';
$clean_keywords = array('X345W','X456W');
$term_taxonomy_ids = wp_set_object_terms( $product_id, $clean_keywords, $taxonomy, true );

// Get existing attributes
$product_attributes = get_post_meta( $product_id, '_product_attributes', true);

// get the count of existing attributes to set the "position" in the array
$count = count($product_attributes);

// Insert new attribute in existing array of attributes (if there is any)
$product_attributes[$taxonomy] = array(
'name' => $taxonomy,
'value' => '',
'position' => $count, // added
'is_visible' => '0',
'is_variation' => '0', // added (set the right value)
'is_taxonomy' => '1'

// Save the data
update_post_meta( $product_id, '_product_attributes', $product_attributes );

Note : I replaced (manually) $taxonomy -> ‘pa_list’ and $clean_keywords = array(‘X345W’,’X465W’);

In attribute field ‘pa_list’ both partner codes have been added but do not fill or set to product.

I’m having a hard time connecting some logic here.

I hit a dead end to get what I wanted.

Can anyone help me?

Thank You

carlozsan 1 year 2021-12-13T03:40:57-05:00 0 Answers 0 views 0

Leave an answer