-1

Situation: A Woocommerce product object typically holds an array dimensions with raw x y z values.

$product = [
  'dimensions' => [
    'length' => 1,
    'width' => 1,
    'height' => 1
  ],
  'dimensions_html' => '1 x 1 x 1 cm',
  ...

Using "Additional custom dimensions for products in Woocommerce" answer code, I created 3 new custom dimensions (depth, diameter, seat-height)…

Problem: I want to add these properties to the product class so they are directly available everywhere like:

$product = [
  'dimensions' => [
    'length' => 1,
    'width' => 1,
    'height' => 1,
    'depth' => 1,
    'diameter' => 1,
    'seat-height' => 1
  ],
  'dimensions_html' => '1 x 1 x 1 x 1 x 1 x 1 cm',
  ...

How can this be done?

moritzebeling
  • 233
  • 1
  • 4
  • 8

1 Answers1

-1

I worked around and manipulated the dimensions_html instead to include all necessary dimensions. This is not a elegant or universal solution, but works for me right now.

// functions.php
add_filter( 'woocommerce_format_dimensions', 'change_formated_product_dimentions', 10, 2 );
function change_formated_product_dimentions( $dimension_string, $dimensions ){
    global $product;
    $cm = get_option( 'woocommerce_dimension_unit' );

    $html = '';

    if( $dimensions['length'] ){
        $html .= '<span><strong>Length</strong> '.$dimensions['length'].' '.$cm.'</span>';
    }

    if( $dimensions['width'] ){
        $html .= '<span><strong>Width</strong> '.$dimensions['width'].' '.$cm.'</span>';
    }

    if( $dimensions['height'] ){
        $html .= '<span><strong>Height</strong> '.$dimensions['height'].' '.$cm.'</span>';
    }

    $depth = $product->get_meta( '_depth' );
    if( $depth ){
        $html .= '<span><strong>Depth</strong> '.$depth.' '.$cm.'</span>';
    }

    $diameter = $product->get_meta( '_diameter' );
    if( $diameter ){
        $html .= '<span><strong>Diameter</strong> '.$diameter.' '.$cm.'</span>';
    }

    $seat_height = $product->get_meta( '_seat_height' );
    if( $seat ){
        $html .= '<span><strong>Seat height</strong> '.$seat_height.' '.$cm.'</span>';
    }

    return $html;
}

This is now included in $product['dimensions_html'] and, when echoed results in

Length 1 cm Width 1 cm Height 1 cm Depth 1 cm Diameter 1 cm Seat height 1 cm

and which (almost) just what I wanted.

moritzebeling
  • 233
  • 1
  • 4
  • 8