Skip to content

Instantly share code, notes, and snippets.

@mszkb
Created March 27, 2017 13:46
Show Gist options
  • Save mszkb/00ac40a82883b6ba95c1c6c22729f89f to your computer and use it in GitHub Desktop.
Save mszkb/00ac40a82883b6ba95c1c6c22729f89f to your computer and use it in GitHub Desktop.
WooCommerce - get specific Attribut
add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_alk', 4 );
function woocommerce_template_single_alk() {
global $product;
$alk = $alk_s = $alk_temp = $alk_original = "";
$alk = get_the_terms( $product->id, 'pa_alkoholgehalt'); // Get Attribute from Produkt
if($alk == NULL) {
_e('Kein Alkoholgehalt angegeben', 'GUID-Theme');
return;
}
$alk_count = count($alk);
if($alk_count < 1)
return;
// Change String into a comparable integer
$alk_temp = $clean = preg_replace('/[^\\d.,]+/', '', $alk{0}->name); // Remove everything but , . and digits for comparing
$alk_original = $alk_temp; // Save original value for output
$alk_temp = intval(preg_replace('/\,/', '.', $alk_temp)); // Replace all , with . - to make it compareable
// Change Value based on specific conditions
if($alk_temp < 10) {
$alk_s = '< 10 vol %';
} else if ($alk_temp > 13.5) {
$alk_s = '> 13,5 vol %';
} else {
$alk_s = $alk_original . ' vol %';
}
echo sprintf('<div class="shop-item-alkohol"><span>%s</span></div>', $alk_s);
}
@mszkb
Copy link
Author

mszkb commented Mar 27, 2017

Place into functions.php (e.g.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment