Skip to content

Instantly share code, notes, and snippets.

@huubl
Forked from Jon007/functions.php
Created November 1, 2021 09:49
Show Gist options
  • Select an option

  • Save huubl/a38b1dd47147d80d2c4dce6c2b9dc4c2 to your computer and use it in GitHub Desktop.

Select an option

Save huubl/a38b1dd47147d80d2c4dce6c2b9dc4c2 to your computer and use it in GitHub Desktop.

Revisions

  1. @Jon007 Jon007 revised this gist Jun 20, 2017. No changes.
  2. @Jon007 Jon007 created this gist Jun 20, 2017.
    96 changes: 96 additions & 0 deletions functions.php
    Original file line number Diff line number Diff line change
    @@ -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');