Skip to content

Commit

Permalink
Merge pull request woocommerce#11615 from woothemes/variable-weight-d…
Browse files Browse the repository at this point in the history
…isplay-10621

Show variable weights/dimensions even when parent values are not set.
  • Loading branch information
mikejolley authored Aug 4, 2016
2 parents f24666a + b0064bd commit fea9a3b
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 8 deletions.
22 changes: 20 additions & 2 deletions includes/abstracts/abstract-wc-product.php
Original file line number Diff line number Diff line change
Expand Up @@ -1429,7 +1429,7 @@ public function has_attributes() {
* @return bool
*/
public function enable_dimensions_display() {
return apply_filters( 'wc_product_enable_dimensions_display', true );
return apply_filters( 'wc_product_enable_dimensions_display', true ) && ( $this->has_dimensions() || $this->has_weight() );
}

/**
Expand All @@ -1441,6 +1441,15 @@ public function has_dimensions() {
return $this->get_dimensions() ? true : false;
}

/**
* Does a child have dimensions set?
* @since 2.7.0
* @return boolean
*/
public function child_has_dimensions() {
return false;
}

/**
* Returns the product length.
* @return string
Expand Down Expand Up @@ -1483,6 +1492,15 @@ public function has_weight() {
return $this->get_weight() ? true : false;
}

/**
* Does a child have a weight set?
* @since 2.7.0
* @return boolean
*/
public function child_has_weight() {
return false;
}

/**
* Returns formatted dimensions.
* @return string
Expand All @@ -1498,7 +1516,7 @@ public function get_dimensions() {
$dimensions .= ' ' . get_option( 'woocommerce_dimension_unit' );
}

return apply_filters( 'woocommerce_product_dimensions', $dimensions, $this );
return apply_filters( 'woocommerce_product_dimensions', $dimensions, $this );
}

/**
Expand Down
47 changes: 46 additions & 1 deletion includes/class-wc-product-variable.php
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,7 @@ public function get_available_variation( $variation ) {
'price_html' => apply_filters( 'woocommerce_show_variation_price', $variation->get_price() === "" || $this->get_variation_price( 'min' ) !== $this->get_variation_price( 'max' ), $this, $variation ) ? '<span class="price">' . $variation->get_price_html() . '</span>' : '',
'availability_html' => $availability_html,
'sku' => $variation->get_sku(),
'weight' => $variation->get_weight() . ' ' . esc_attr( get_option('woocommerce_weight_unit' ) ),
'weight' => $variation->get_weight() ? $variation->get_weight() . ' ' . esc_attr( get_option('woocommerce_weight_unit' ) ) : '',
'dimensions' => $variation->get_dimensions(),
'min_qty' => 1,
'max_qty' => $variation->backorders_allowed() ? '' : $variation->get_stock_quantity(),
Expand Down Expand Up @@ -723,6 +723,33 @@ public static function sync_attributes( $product_id, $children = false ) {
}
}

/**
* Does a child have a weight set?
* @since 2.7.0
* @return boolean
*/
public function child_has_weight() {
return (bool) get_post_meta( $this->id, '_child_has_weight', true );
}

/**
* Does a child have dimensions set?
* @since 2.7.0
* @return boolean
*/
public function child_has_dimensions() {
return (bool) get_post_meta( $this->id, '_child_has_dimensions', true );
}

/**
* Returns whether or not we are showing dimensions on the product page.
*
* @return bool
*/
public function enable_dimensions_display() {
return apply_filters( 'wc_product_enable_dimensions_display', true ) && ( $this->has_dimensions() || $this->has_weight() || $this->child_has_weight() || $this->child_has_dimensions() );
}

/**
* Sync the variable product with it's children.
*/
Expand All @@ -740,6 +767,8 @@ public static function sync( $product_id ) {
// No published variations - product won't be purchasable.
if ( ! $children ) {
update_post_meta( $product_id, '_price', '' );
delete_post_meta( $product_id, '_child_has_weight' );
delete_post_meta( $product_id, '_child_has_dimensions' );
delete_transient( 'wc_products_onsale' );

if ( is_admin() && 'publish' === get_post_status( $product_id ) ) {
Expand Down Expand Up @@ -826,6 +855,22 @@ public static function sync( $product_id ) {
add_post_meta( $product_id, '_price', $max_price, false );
delete_transient( 'wc_products_onsale' );

// Sync weights
foreach ( $children as $child_id ) {
if ( get_post_meta( $child_id, '_weight', true ) ) {
update_post_meta( $product_id, '_child_has_weight', true );
break;
}
}

// Sync dimensions
foreach ( $children as $child_id ) {
if ( get_post_meta( $child_id, '_height', true ) || get_post_meta( $child_id, '_width', true ) || get_post_meta( $child_id, '_length', true ) ) {
update_post_meta( $product_id, '_child_has_dimensions', true );
break;
}
}

// Sync attributes
self::sync_attributes( $product_id, $children );

Expand Down
2 changes: 1 addition & 1 deletion includes/wc-template-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -1141,7 +1141,7 @@ function woocommerce_default_product_tabs( $tabs = array() ) {
}

// Additional information tab - shows attributes
if ( $product && ( $product->has_attributes() || ( $product->enable_dimensions_display() && ( $product->has_dimensions() || $product->has_weight() ) ) ) ) {
if ( $product && ( $product->has_attributes() || $product->enable_dimensions_display() ) ) {
$tabs['additional_information'] = array(
'title' => __( 'Additional Information', 'woocommerce' ),
'priority' => 20,
Expand Down
1 change: 1 addition & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ Yes you can! Join in on our [GitHub repository](http://github.com/woothemes/wooc
* Redirect to login after password reset.
* When using authorizations in PayPal standard, automatically capture funds when the order goes processing/completed.
* On multisite, when a user logs into a store with an account on a site, but not the current site, rather than error, add the user to the current site as a customer.
* Show variable weights/dimensions even when parent values are not set.

[See changelog for all versions](https://raw.githubusercontent.com/woothemes/woocommerce/master/CHANGELOG.txt).

Expand Down
8 changes: 4 additions & 4 deletions templates/single-product/product-attributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,17 @@

<?php if ( $product->enable_dimensions_display() ) : ?>

<?php if ( $product->has_weight() ) : $has_row = true; ?>
<?php if ( $product->has_weight() || get_post_meta( $product->id, '_child_has_weight', true ) ) : $has_row = true; ?>
<tr class="<?php if ( ( $alt = $alt * -1 ) === 1 ) echo 'alt'; ?>">
<th><?php _e( 'Weight', 'woocommerce' ) ?></th>
<td class="product_weight"><?php echo wc_format_localized_decimal( $product->get_weight() ) . ' ' . esc_attr( get_option( 'woocommerce_weight_unit' ) ); ?></td>
<td class="product_weight"><?php echo $product->get_weight() ? wc_format_localized_decimal( $product->get_weight() ) . ' ' . esc_attr( get_option( 'woocommerce_weight_unit' ) ) : __( 'N/A', 'woocommerce' ); ?></td>
</tr>
<?php endif; ?>

<?php if ( $product->has_dimensions() ) : $has_row = true; ?>
<?php if ( $product->has_dimensions() || get_post_meta( $product->id, '_child_has_dimensions', true ) ) : $has_row = true; ?>
<tr class="<?php if ( ( $alt = $alt * -1 ) === 1 ) echo 'alt'; ?>">
<th><?php _e( 'Dimensions', 'woocommerce' ) ?></th>
<td class="product_dimensions"><?php echo $product->get_dimensions(); ?></td>
<td class="product_dimensions"><?php echo $product->get_dimensions() ? $product->get_dimensions() : __( 'N/A', 'woocommerce' ); ?></td>
</tr>
<?php endif; ?>

Expand Down

0 comments on commit fea9a3b

Please sign in to comment.