WP+Woocommerce – edit attribute table
Default view product attributes for WooCommerce - table in tabs "Additional information". Image below - standart output attributes WooCommerce in theme WordPress twentysixteen:
About add and editing Woocommerce attributes i write here, this post will be about different output table attributes and editing code table attributes.
File from code table attributes (wich output tab "Additional information"):
wp-content/plugins/woocommerce/templates/single-product/product-attributes.php
Copy to:
wp-content/themes/*YOUR-THEME*/woocommerce/single-product/product-attributes.php
Default code table attributes Woocommerce 3.0.9
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
<?php /** * Product attributes * * Used by list_attributes() in the products class. * * This template can be overridden by copying it to yourtheme/woocommerce/single-product/product-attributes.php. * * HOWEVER, on occasion WooCommerce will need to update template files and you * (the theme developer) will need to copy the new files to your theme to * maintain compatibility. We try to do this as little as possible, but it does * happen. When this occurs the version of the template file will be bumped and * the readme will list any important changes. * * @see https://docs.woocommerce.com/document/template-structure/ * @author WooThemes * @package WooCommerce/Templates * @version 3.0.0 */ if ( ! defined( 'ABSPATH' ) ) { exit; } ?> <table class="shop_attributes"> <?php if ( $display_dimensions && $product->has_weight() ) : ?> <tr> <th><?php _e( 'Weight', 'woocommerce' ) ?></th> <td class="product_weight"><?php echo esc_html( wc_format_weight( $product->get_weight() ) ); ?></td> </tr> <?php endif; ?> <?php if ( $display_dimensions && $product->has_dimensions() ) : ?> <tr> <th><?php _e( 'Dimensions', 'woocommerce' ) ?></th> <td class="product_dimensions"><?php echo esc_html( wc_format_dimensions( $product->get_dimensions( false ) ) ); ?></td> </tr> <?php endif; ?> <?php foreach ( $attributes as $attribute ) : ?> <tr> <th><?php echo wc_attribute_label( $attribute->get_name() ); ?></th> <td><?php $values = array(); if ( $attribute->is_taxonomy() ) { $attribute_taxonomy = $attribute->get_taxonomy_object(); $attribute_values = wc_get_product_terms( $product->get_id(), $attribute->get_name(), array( 'fields' => 'all' ) ); foreach ( $attribute_values as $attribute_value ) { $value_name = esc_html( $attribute_value->name ); if ( $attribute_taxonomy->attribute_public ) { $values[] = '<a href="' . esc_url( get_term_link( $attribute_value->term_id, $attribute->get_name() ) ) . '" rel="tag">' . $value_name . '</a>'; } else { $values[] = $value_name; } } } else { $values = $attribute->get_options(); foreach ( $values as &$value ) { $value = esc_html( $value ); } } echo apply_filters( 'woocommerce_attribute', wpautop( wptexturize( implode( ', ', $values ) ) ), $attribute, $values ); ?></td> </tr> <?php endforeach; ?> </table> |
Code have two section -
- Output weight and dimensions;
- While on lists attributes product.
Differents methods output table attributes
In order to output table attributtes other place, need disable default call template attributes and after need add code in place who must be table attributes. Make this can with the help WordPress hooks.
Attributes output in tab "Additional information", this tabs call in file "content-single-product.php", hook -
do_action( 'woocommerce_after_single_product_summary' );
Hook 'woocommerce_after_single_product_summary' well output next blocks:
- Up-sells products;
- Cross products;
- Tabs block.
For disable only tab "Additional information", add next code in functions.php your theme:
1 2 3 4 5 6 7 8 9 10 |
add_filter('woocommerce_product_tabs', 'remove_tab_additional', 100); if (!function_exists('remove_tab_additional')) { function remove_tab_additional($tabs = array()) { unset($tabs['additional_information']); // Delete tab "Additional Information" // unset($tabs['description']); // Delete tab "Description" // unset($tabs['reviews']); // Delete tab "Reviews" return $tabs; } } |
Ths code have two rows disable, it rows can delete other tabs. If need delete all tabs, remove in action "woocommerce_after_single_product_summary" event "woocommerce_output_product_data_tabs":
remove_action('woocommerce_after_single_product_summary', 'woocommerce_output_product_data_tabs',10);
In order that output table attributes before tabs, add next hook (priority low 10):
add_action('woocommerce_after_single_product_summary', 'woocommerce_product_additional_information_tab', 4);
When have a little attributes in products, can paste table in right block after button "Add to cart" with the help hook "woocommerce_single_product_summary" (priority more 60):
add_action('woocommerce_single_product_summary', 'woocommerce_product_additional_information_tab',70);
Code for output attributes table in other places woocommerce theme:
do_action( 'woocommerce_product_additional_information', $product );
Don't forget add object product in this hook. Usualy object determine at the top page current templates, but if it missing, add:
global $product;
before call hook "woocommerce_product_additional_information".