Thread Starter
Chuck
(@chuckstr)
Thanks! I have a variety of products with over 40 different attributes and 5x that many terms. Any way to simplify the code you provided. I’m not using any imported DataFeedr attributes. They are all custom attributes.
Thread Starter
Chuck
(@chuckstr)
The attributes will change from time to time and I’m trying to avoid replacing/updating the plugin list of attributes. Thanks.
Hi Chuck
You don’t need to add the terms to the custom code but you will need to add your various attribute slugs to the $attrs array.
So you might have:
$attrs = [
'color',
'size',
'gender',
'material',
'condition',
];
You can find all of those 40 slugs here WordPress Admin Area > Products > Attributes.
Hope this helps!
Eric
So you aren’t using any Global Attributes? You are only using Custom Attributes?
Is that correct?
Thread Starter
Chuck
(@chuckstr)
What’s interesting, I’ve never had this problem before. What has changed, or what may I have done to now have to do this? Is there a way to not import “any” attributes?
Thread Starter
Chuck
(@chuckstr)
Sorry, our messages are overlapping. No, I am not using any Global Attributes.
Hi Chuck
I am confused… You mention that you are “not using any Global Attributes” but you said “the WooCommerce attribute I created, Antenna Amplified … with two terms …”.
Where did you create the WooCommerce Attribute “Antenna Amplified”? On this page: WordPress Admin Area > Products > Attributes?
Thanks
Eric
Thread Starter
Chuck
(@chuckstr)
To clarify, I’m not using any DataFeedr imported attributes. All attributes are custom on my end. Thanks.
OK, I see. Thanks for clarifying.
And just so I fully understand…
Were all of your attributes created here: WordPress Admin Area > Products > Attributes?
Thread Starter
Chuck
(@chuckstr)
Yes.
Also, I’m not seeing anywhere to set DataFeedr Global attributes, even if I wanted to.
Correct, there is no place to create Datafeedr Global Attributes… Datafeedr relies on the WooCommerce attribute system.
Can you try adding the following code to your custom plugin:
add_filter( 'dfrpswc_filter_attribute_value', function ( $value, $attribute, $post, $product, $set, $action ) {
$attrs = array_column( wc_get_attribute_taxonomies(), 'attribute_name' );
if ( 'insert' === $action ) {
return $value;
}
foreach ( $attrs as $attr ) {
$name = dfrapi_string_starts_with( $attr, 'pa_' ) ? $attr : 'pa_' . $attr;
if ( $name !== $attribute ) {
continue;
}
$value = wc_get_product_terms( $post['ID'], $name, [ 'fields' => 'names' ] );
$value = implode( WC_DELIMITER, $value );
}
return $value;
}, 10, 6 );
That code will recognize your existing and future attributes so you won’t need to update the code if you add or remove WooCommerce Global attributes in the future.
Hope this helps!
If you have any other questions, please don’t hesitate to ask.
Thanks,
Eric
-
This reply was modified 3 years, 9 months ago by
datafeedr.
-
This reply was modified 3 years, 9 months ago by
datafeedr.
Thread Starter
Chuck
(@chuckstr)
Great! Added in the code. What do I do with this code that is already in the default code from the internet?
/**
* Add slug value for each attribute that should not be modified during a Product Set update.
* Slugs can be found here: WordPress Admin Area > Products > Attributes
*/
$attrs = array(
'color',
'size',
);
if ( 'insert' == $action ) {
return $value;
}
foreach ( $attrs as $attr ) {
$name = ( substr( $attr, 0, 3 ) === 'pa_' ) ? $attr : 'pa_' . $attr;
if ( $name == $attribute ) {
$value = wc_get_product_terms( $post['ID'], $name, array( 'fields' => 'names' ) );
$value = implode( WC_DELIMITER, $value );
}
}
return $value;
}
The code I provided above is a replacement for all of the code you mentioned here.
Does that make sense?
Thread Starter
Chuck
(@chuckstr)
Yep. Got it. I’ll mark out the old code and add the new code you provided today. I’ll then do a test on a single Product Set update and see what happens.
You are awesome! Thank you very much for your help.