Last active
December 4, 2023 08:17
-
-
Save Jon007/ae3a112659d6670ebbd2f26d55143773 to your computer and use it in GitHub Desktop.
Add default Product Attributes to all WooCommerce products
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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'); |
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)); } }
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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".