admin管理员组

文章数量:1289548

Bonjour tous et merci par avance de votre temps.

Je souhaite modifier l'affichage du prix (prix au m2) avec le calcul de son prix, via un champ ACF, avec une concaténation j'obtiens bien un résultat mais ma page est cassée que je n'arrive pas à résoudre.

Hello everyone and thank you in advance for your time.

I want to modify the display of the price (price per m2) with the calculation of its price, via an ACF field, with a concatenation I get a result but my page is broken that I cannot resolve.

Wordpres 5.7
PHP 7.3
Thème Storefront

Mon function.php:

//On ajoute le prix / m2 sur la fiche produit
function change_product_price_display( $price ) {
    $surface = get_field( 'surface' );// Value to calculate the price per square meter
    $surfaces = str_replace( '.', ',', get_field( 'surface' ) ); 
    $price_unit = round( $price / $surface, 2);
    if ( is_product() && has_term( array( 'tons-clairs', 'tons-moyens', 'tons-fonces' ), 'product_cat' ) ) {
        if ( !empty( $surface ) ) {
            echo $price_unit . ' /m²<br><p>Soit ' . $price . ' le paquet de ' .$surfaces . ' m²</p>';
        }
    }elseif ( is_product() && !has_term( array( 'tons-clairs', 'tons-moyens', 'tons-fonces' ), 'product_cat' )  ) {
        return $price;
    }
    
}
add_filter( 'woocommerce_get_price_html', 'change_product_price_display' );
add_filter( 'woocommerce_product_get_price', 'change_product_price_display' );

Voici le front

Bonjour tous et merci par avance de votre temps.

Je souhaite modifier l'affichage du prix (prix au m2) avec le calcul de son prix, via un champ ACF, avec une concaténation j'obtiens bien un résultat mais ma page est cassée que je n'arrive pas à résoudre.

Hello everyone and thank you in advance for your time.

I want to modify the display of the price (price per m2) with the calculation of its price, via an ACF field, with a concatenation I get a result but my page is broken that I cannot resolve.

Wordpres 5.7
PHP 7.3
Thème Storefront

Mon function.php:

//On ajoute le prix / m2 sur la fiche produit
function change_product_price_display( $price ) {
    $surface = get_field( 'surface' );// Value to calculate the price per square meter
    $surfaces = str_replace( '.', ',', get_field( 'surface' ) ); 
    $price_unit = round( $price / $surface, 2);
    if ( is_product() && has_term( array( 'tons-clairs', 'tons-moyens', 'tons-fonces' ), 'product_cat' ) ) {
        if ( !empty( $surface ) ) {
            echo $price_unit . ' /m²<br><p>Soit ' . $price . ' le paquet de ' .$surfaces . ' m²</p>';
        }
    }elseif ( is_product() && !has_term( array( 'tons-clairs', 'tons-moyens', 'tons-fonces' ), 'product_cat' )  ) {
        return $price;
    }
    
}
add_filter( 'woocommerce_get_price_html', 'change_product_price_display' );
add_filter( 'woocommerce_product_get_price', 'change_product_price_display' );

Voici le front

Share Improve this question edited Jul 30, 2021 at 7:58 N. Staf asked Jul 30, 2021 at 7:52 N. StafN. Staf 112 bronze badges 3
  • You're mixing return and echo, and the overall else case (if neither the if nor the elseif match) doesn't return anything. I don't know WooCommerce or that filter but I'd guess you'd need to return the output text in all cases, unless you're deliberately echoing to sidestep an esc_html() elsewhere (there must be a better way than that). That said, I can't see why that would break your page off the top of my head - the HTML you're echoing looks OK. – Rup Commented Jul 30, 2021 at 7:56
  • Please edit your question, and write in English. Thank you. – fuxia Commented Jul 30, 2021 at 8:17
  • @Rup thank you for your answer, I changed the return to echo but it is not the PB, the page is well broken the title "Chêne bendor" and the price should be to the right of the image with the price and it is not shouldn't be the price repeat either. Im sorry for my English – N. Staf Commented Jul 30, 2021 at 8:34
Add a comment  | 

1 Answer 1

Reset to default 0

J'ai résolu une partie du problème.

I solved part of the problem.

**function change_product_price_display( $price ) {
if ( is_product() && has_term( array( 'clair', 'moyen', 'fonce' ), 'product_cat' ) ) {
    $price_unit = $price;
    $prices     = get_post_meta( get_the_ID(), '_price', true );
    $surface    = get_field( 'surface' );
    $surfaces   = str_replace( '.', ',', get_field( 'surface' ) );
    $price_surface = $prices/$surface;
    if ( !empty( $surface ) ) {
       return '<p><ins><span class="woocommerce-Price-amount amount"><bdi>'. str_replace( '.', ',', sprintf("%.2f", $price_surface ) ) .'<span class="woocommerce-Price-currencySymbol"> €</span></bdi></span></ins><small class="woocommerce-price-suffix"> TTC/m²</small><br>Soit ' . $price_unit . ' le paquet de ' .$surfaces . ' m²</p>';
    }
}
return $price;

} add_filter( 'woocommerce_get_price_html', 'change_product_price_display' );**

Voici le résultat :

67,70 € TTC/m² Soit 135,40 € TTC le paquet de 2 m²

How can we retrieve this function to display it in the cart under the name?

Comment peut-on récupérer cette function pour l'afficher dans le panier sous le nom ?

add_action( 'woocommerce_after_cart_item_name', '...' );

本文标签: phpModifier l39affichage du prix sur la fiche produit