WooCommerce – Änderung der Preisausgabe
-
Ich möchte bei allen Produkten die sich in einer bestimmten Kategorie oder einer Unterkategorie davon befinden, die Preis Ausgabe anpassen.
der orginale Code der price.php sieht bei mir wie folgt aus:
<?php /** * Loop Price * * @author WooThemes * @package WooCommerce/Templates * @version 1.6.4 */ if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly global $product; ?> <?php if ( $price_html = $product->get_price_html() ) : ?> <span class="product_price headerfont"><?php echo $price_html; ?></span> <?php endif; ?>
Ich habe den Code dann so angepasst:
<?php /** * Loop Price * * @author WooThemes * @package WooCommerce/Templates * @version 1.6.4 */ if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly global $product; ?> <?php if ( $price_html = $product->get_price_html() ) : ?> <?php //Check for parent category - if id =23 show other price //Get parent category global $post; $prod_terms = get_the_terms( $post->ID, 'product_cat' ); foreach ($prod_terms as $prod_term) { // gets product cat id $product_cat_id = $prod_term->term_id; // gets an array of all parent category levels $product_parent_categories_all_hierachy = get_ancestors( $product_cat_id, 'product_cat' ); // This cuts the array and extracts the last set in the array $last_parent_cat = array_slice($product_parent_categories_all_hierachy, -1, 1, true); foreach($last_parent_cat as $last_parent_cat_value){ // $last_parent_cat_value is the id of the most top level category, can be use whichever one like if($last_parent_cat_value == 23){ $product_price_per_100g = $product->get_price_including_tax() * 100; //$new_price = preg_replace('/,00/', '', $product_price_per_100g); ?> <span class="product_price headerfont"><?php echo $price_html." per g"; ?></span> <?php echo number_format( $product_price_per_100g, // zu konvertierende zahl 2, // Anzahl an Nochkommastellen ",", // Dezimaltrennzeichen "." // 1000er-Trennzeichen ); //echo $product_price_per_100g; echo " ".get_woocommerce_currency(); echo " per 100g"; }else{ ?> <span class="product_price headerfont"><?php echo $price_html; ?></span> <?php } } } ?> <?php endif; ?>
Jetzt erscheint der Preis bei der gewünschten Kategorie richtig (mit Gramm Angabe + Preis per 100g)
allerdings seh ich jetzt bei Produkten aus anderen Kategorien gar keine Preise mehr. Hat vielleicht jemand eine Idee woran das liegen könnte ?Danke für eure Hilfe =)
Ansicht von 5 Antworten - 1 bis 5 (von insgesamt 5)
Ansicht von 5 Antworten - 1 bis 5 (von insgesamt 5)
- Das Thema „WooCommerce – Änderung der Preisausgabe“ ist für neue Antworten geschlossen.