简体   繁体   中英

woocommerce get current product name in functions.php

I create a button in a current product page after woocommerce_single_product_summary

I would get information about the current product ( for example name and price) and through the button send an email to get information

this is the code of my functions.php file:

add_action( 'woocommerce_single_product_summary','content_after_addtocart_button' );

function content_after_addtocart_button() { 
echo '<div class="content-section">
<a href="mailto:riccardoairone@gmail.com?&subject= Richiesta Informazioni&body= product name ??? product price ??? ">
<input type="button" value="Richiedi Informazioni"/ ></a></div>'; 
}

Anyone can help me?

You can fetch product details from the global $product variable which WooCommerce set for each product. You can check following example to create URL.

function content_after_addtocart_button() {
    global $product;
    echo '<div class="content-section">';
    $url = 'mailto:riccardoairone@gmail.com?subject= Richiesta Informazioni';
    $url .= '&body=Product:' . $product->get_name() . ' Price: ' . $product->get_price();
    echo '<a href="' . esc_url( $url ) . '" class="button">Richiedi Informazioni</a>';
    echo '</div>';
}

Is this what you're looking for?

global $product;
$product->get_name();
$product->get_price_html();

Don't forget to use esc_attr() .

add_action( 'woocommerce_single_product_summary','content_after_addtocart_button', 10, 2 );

function content_after_addtocart_button($product = null) { 
global $product;
echo '<div class="content-section">
<a href="mailto:riccardoairone@gmail.com?&subject= Richiesta Informazioni&body= $product->get_name() ??? $product->get_price() ??? ">
<input type="button" value="Richiedi Informazioni"/ ></a></div>'; 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM