|
|
@@ -0,0 +1,96 @@ |
|
|
/* |
|
|
* 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'); |