Created
March 27, 2017 13:46
-
-
Save mszkb/00ac40a82883b6ba95c1c6c22729f89f to your computer and use it in GitHub Desktop.
WooCommerce - get specific Attribut
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Place into functions.php (e.g.)