Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save hamidrezayazdani/480c8ea3fcc3d89db9379eba99e97dc6 to your computer and use it in GitHub Desktop.
Save hamidrezayazdani/480c8ea3fcc3d89db9379eba99e97dc6 to your computer and use it in GitHub Desktop.
<?php
/**
* Show weight and dementions end of additional information
*/
remove_action( 'woocommerce_product_additional_information', 'wc_display_product_attributes', 10 );
add_action( 'woocommerce_product_additional_information', 'ywp_display_product_attributes', 10 );
function ywp_display_product_attributes( $product ) {
$product_attributes = array();
// Add product attributes to list.
$attributes = array_filter( $product->get_attributes(), 'wc_attributes_array_filter_visible' );
foreach ( $attributes as $attribute ) {
$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 = make_clickable( esc_html( $value ) );
}
}
$product_attributes[ 'attribute_' . sanitize_title_with_dashes( $attribute->get_name() ) ] = array(
'label' => wc_attribute_label( $attribute->get_name() ),
'value' => apply_filters( 'woocommerce_attribute', wpautop( wptexturize( implode( ', ', $values ) ) ), $attribute, $values ),
);
}
// Display weight and dimensions before attribute list.
$display_dimensions = apply_filters( 'wc_product_enable_dimensions_display', $product->has_weight() || $product->has_dimensions() );
if ( $display_dimensions && $product->has_weight() ) {
$product_attributes['weight'] = array(
'label' => __( 'Weight', 'woocommerce' ),
'value' => wc_format_weight( $product->get_weight() ),
);
}
if ( $display_dimensions && $product->has_dimensions() ) {
$product_attributes['dimensions'] = array(
'label' => __( 'Dimensions', 'woocommerce' ),
'value' => wc_format_dimensions( $product->get_dimensions( false ) ),
);
}
/**
* Hook: woocommerce_display_product_attributes.
*
* @since 3.6.0.
* @param array $product_attributes Array of atributes to display; label, value.
* @param WC_Product $product Showing attributes for this product.
*/
$product_attributes = apply_filters( 'woocommerce_display_product_attributes', $product_attributes, $product );
wc_get_template(
'single-product/product-attributes.php',
array(
'product_attributes' => $product_attributes,
// Legacy params.
'product' => $product,
'attributes' => $attributes,
'display_dimensions' => $display_dimensions,
)
);
}
/**
* The code goes in your active theme (or child theme) funtions.php
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment