Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save plugin-republic/b76588e2a06078df51f47cb8eb40c555 to your computer and use it in GitHub Desktop.

Select an option

Save plugin-republic/b76588e2a06078df51f47cb8eb40c555 to your computer and use it in GitHub Desktop.
<?php
/**
* Variable Select layout: show the attribute name and value in the variation dropdown,
* e.g. 'Magazijn: Hillegom (137375-A)'
*/
function prefix_variable_select_variation_name_only( $name, $product, $option_price = false, $item = false ) {
if ( empty( $item['products_layout'] ) || 'variable-select' !== $item['products_layout'] ) {
return $name;
}
if ( ! is_object( $product ) || ! $product->is_type( 'variation' ) ) {
return $name;
}
// The third parameter adds the attribute name, e.g. 'Magazijn: Hillegom'
$attributes = wc_get_formatted_variation( $product, true, true );
if ( '' === $attributes ) {
return $name;
}
// Keep the SKU, or the ID if there's no SKU
$identifier = $product->get_sku() ? $product->get_sku() : '#' . $product->get_id();
return sprintf( '%s (%s)', $attributes, $identifier );
}
add_filter( 'pewc_child_product_title', 'prefix_variable_select_variation_name_only', 5, 4 );
/**
* Variable Select layout: hide the price in the variation dropdown
*/
function prefix_variable_select_hide_variation_price( $hide, $item ) {
if ( ! empty( $item['products_layout'] ) && 'variable-select' === $item['products_layout'] ) {
return true;
}
return $hide;
}
add_filter( 'pewc_hide_zero_option_price', 'prefix_variable_select_hide_variation_price', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment