-
-
Save Jon007/ae3a112659d6670ebbd2f26d55143773 to your computer and use it in GitHub Desktop.
/* | |
* Example adds certain named Product Attribute fields to the product Edit screen ready for completion. | |
* (Pre-requisite ) | |
* This saves the Shop Admin having to add them manually. | |
* | |
* Why might you want to do this instead of adding Custom fields? There's plenty of nice documentation on adding custom fields | |
* for example: http://www.remicorson.com/mastering-woocommerce-products-custom-fields/ | |
* | |
* Well a Product Attributes are a built in WooCommerce feature, using Terms which are a built in Wordpress feature. | |
* - no add-ons required | |
* - pre-existing functionality including | |
* - ability to link from Product to archive page of products sharing the same term | |
* - Term selection, hide or show Attribute, use for Product Variations | |
* - built-in configuration screens, display and selection mechanisms | |
* - highly compatible with plugin components for example to | |
* - enhance Product Attributes display, | |
* - show Variation swatches, | |
* - translate Attributes and terms | |
* - offers a level of separation between built-in fields and extension fields | |
* (though custom fields can be added to their own tab, and some fields make sense to add to other tabs) | |
* | |
*/ | |
/* | |
* Create a default Product Attribute object for the supplied name | |
* | |
* @param string name Product Attribute taxonomy name | |
* | |
* @return WC_Product_Attribute/bool new Attribute or false if named Attribute is not found | |
* | |
*/ | |
function acme_make_product_attribute($name) | |
{ | |
global $wc_product_attributes; | |
if ( isset($wc_product_attributes[$name]) ){ | |
$newattr = new WC_Product_Attribute(); | |
$newattr->set_id(1); //any positive value is interpreted as is_taxonomy=true | |
$newattr->set_name($name); | |
$newattr->set_visible(true); | |
$newattr->set_variation(false); | |
//example of setting default value for item | |
if ($name=='pa_brand'){ | |
$term = get_term_by('slug', 'acme', $name); | |
$newattr->set_options(array($term->term_id)); | |
} | |
return $newattr; | |
} else { | |
return false; | |
} | |
} | |
/* | |
* Add default attributes to a product | |
*/ | |
function acme_default_product_attributes() | |
{ | |
global $product; | |
if (! $product) { | |
$product = $GLOBALS['product_object']; | |
} | |
if (! $product) { | |
return; | |
} | |
$attributes = $product->get_attributes(); | |
$defaultAttributes = array( | |
'pa_brand', | |
'pa_maker', | |
'pa_materials', | |
'pa_asin', | |
'pa_upc', | |
'pa_packaging', | |
'pa_recommend-to', | |
'pa_suitable-for', | |
'pa_product-size', | |
'pa_net-weight', | |
); | |
$changed=false; | |
foreach ($defaultAttributes as $key){ | |
if (! isset($attributes[$key])){ | |
$newattr = acme_make_product_attribute($key); | |
if ($newattr){ | |
$attributes[$key] = $newattr; | |
} | |
$changed = true; | |
} | |
} | |
if ($changed){ | |
$product->set_attributes($attributes); | |
} | |
} | |
/* | |
* added to last hook before rendering of Product Edit screen | |
*/ | |
add_action('woocommerce_product_write_panel_tabs', 'acme_default_product_attributes'); |
Where would I add this code in order for it to execute on editing a product?
@Divepit So you want to execute this code when you start editing product or it's just a general question where to insert the code? Code goes to functions.php file which you find in your theme directory. But it's strongly recommended to use child theme for all customizations!
Um, well actually as this is admin code not front-end code it should be in a plugin rather than a theme.
For plugin development refer to: https://developer.wordpress.org/plugins/
Though actually the simplest plugin would be eg create a directory under plugins and a php file with the same name and put the code in that.
Thanks for the clarification!
@fkoomek the Yoast compatibility issue appears resolved on Yoast update 15.9.1
I had to sign up just to say thank you for this, with all of my products having 15+ attributes this has saved me so much time. Thank you!
not working with Woocommerce Version 5.7.1
not working with Woocommerce Version 5.7.1
Is it throwing an error or simply not ordering the attributes? Did you find a solution?
How do I trigger the execution of this snippet? Is it executed automatically if I post it in the functions.php page and whenever a new product is added?
Do you probably have an idea, how to automaticall apply a combination of attribute terms to all products in a given category? Like this: if a new product is published in a category 1, the product gets attribute "color", value "green" and attribute "size", value "big".
For those getting the error "Attempt to read property 'term_id' on bool" with PHP 8, here's the fix:
this part
if ($name=='pa_brand'){ $term = get_term_by('slug', 'acme', $name); $newattr->set_options(array($term->term_id)); }
should become
if ($name == 'pa_brand') { $term = get_term_by('slug', 'acme', $name); if ($term) { $newattr->set_options(array($term->term_id)); } }
@Jon007 Thank you very much for looking into it 🙂.