Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to add custom attributes on product in WooCommerce?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 2.77k
    Comment on it

    Hello Everyone, In this blog I will guide you "To add custom attributes on product in WooCommerce?"

    This is how to produce custom item attributes on the WooCommerce single web page. (To display attributes on the cart page, see Indicate WooCommerce Product Attributes on Cart Page. ) This can be done programmatically by using ones functions document. That relies on a Woocommerce land it is woocommerce_single_product_summary, which will make the goods feature seem above the add to cart button. That goes into functions.php file, certainly not in the template document.

    <?php
    
    /**
    * Show custom product attribute above Add-to-cart button on single product page.
    */
    function isa_woo_get_one_pa(){
    
        // Edit below with the title of the attribute you wish to display
        $desired_att = 'Some Attribute Title';
    
        global $product;
        $attributes = $product->get_attributes();
    
        if ( ! $attributes ) {
            return;
        }
    
        $out = '';
    
        foreach ( $attributes as $attribute ) {
    
            if ( $attribute['is_taxonomy'] ) {
    
                // sanitize the desired attribute into a taxonomy slug
                $tax_slug = strtolower(trim(preg_replace('/[^A-Za-z0-9-]+/', '_', $desired_att)));
    
                // if this is desired att, get value and label
    
                if ( $attribute['name'] == 'pa_' . $tax_slug ) {
    
                    $terms = wp_get_post_terms( $product->id, $attribute['name'], 'all' );
    
                    // get the taxonomy
                    $tax = $terms[0]->taxonomy;
    
                    // get the tax object
                    $tax_object = get_taxonomy($tax);
    
                    // get tax label
                    if ( isset ($tax_object->labels->name) ) {
                        $tax_label = $tax_object->labels->name;
                    } elseif ( isset( $tax_object->label ) ) {
                        $tax_label = $tax_object->label;
                    }
    
                    foreach ( $terms as $term ) {
    
                        $out .= $tax_label . ': ';
                        $out .= $term->name . '<br />';
    
                    }           
    
                } // our desired att
    
            } else {
    
                // for atts which are NOT registered as taxonomies
    
                // if this is desired att, get value and label
                if ( $attribute['name'] == $desired_att ) {
                    $out .= $attribute['name'] . ': ';
                    $out .= $attribute['value'];
                }
            }       
    
    
        }
    
        echo $out;
    }
    add_action('woocommerce_single_product_summary', 'isa_woo_get_one_pa');
    
    ?>
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: