web-dev-qa-db-fra.com

Comment afficher le prix des produits Woocommerce par numéro d'identification sur une page personnalisée?

J'essaie d'afficher le prix d'un produit dans Woocommerce, sur une page personnalisée. Il existe un code court pour cela, mais il donne le prix du produit et ajoute également un "bouton Ajouter au panier", je ne veux pas le bouton, je veux juste obtenir le prix d'un produit spécifique par ID.

Est-ce possible?

Merci.

CODE:

<table class="unlockTableBorder">
<tbody>
<tr>
<td>
<h2>פתיחת מכשירי Alcatel כלל עולמי</h2>
<h4>אנא קראו והבינו את תנאי השירות הבאים לפני הזמנת שירות זה:</h4>
<ul>
        <li>שירות זה תומך בפתיחת מכשירים סלולריים מסוג Alcatel מארה"ב, קנדה ומקסיקו.</li>
        <li>מכשירי CDMA וספקיות שירות CDMA לא נתמכים בידי שירות זה, אנא אל תשתמשו בשירות זה בשביל מכשירים אלו - במידה ותשמשו בשירות זה למכשירי CDMA, אתם תקבלו קוד שלא תוכלו להשתמש בו, ולא תוכלו לקבל החזר כספי! - אנא <a title="פתיחת מכשירי CDMA לכל הרשתות" href="http://www.unlocker.co.il/sim-unlock-cdma-mobile-device/">ראו פתיחת מכשירי CDMA לכל הרשתות בישראל.</a></li>
</ul>
<h5><strong>זמן הספקה: 1-24 שעות</strong></h5>
<form id="unlock1" class="cart" enctype="multipart/form-data" method="post" name="unlock"><input class="the_imei" style="width: 80%; border-radius: 15px;" name="the_imei" type="text" value="" placeholder="מספר סידורי IMEI של המכשיר (חייג #06#*)" /> <input class="add-to-cart" name="add-to-cart" type="hidden" value="76" /> <button class="unlockButton" type="submit" value="submit">פתח לכל הרשתות בישראל </button></form>*בלחיצה על הפתור, אתם מסכימים ל<a title="תנאי השירות" href="http://www.unlocker.co.il/terms-and-conditions/">תנאי השירות</a>.</td>
</tr>
</tbody>
</table>
<script src="http://www.unlocker.co.il/checkimei1.js" type="text/javascript"></script>
40
MosesTheTool

Si vous avez l'ID du produit, vous pouvez l'utiliser pour créer un objet produit:

$_product = wc_get_product( $product_id );

Ensuite, à partir de l'objet, vous pouvez exécuter n'importe quelle méthode de produit de WooCommerce.

$_product->get_regular_price();
$_product->get_sale_price();
$_product->get_price();

Mettre à jour
Veuillez consulter l'article du Codex sur la procédure à suivre écrivez votre propre shortcode .

L'intégration des données du produit WooCommerce pourrait ressembler à ceci:

function so_30165014_price_shortcode_callback( $atts ) {
    $atts = shortcode_atts( array(
        'id' => null,
    ), $atts, 'bartag' );

    $html = '';

    if( intval( $atts['id'] ) > 0 && function_exists( 'wc_get_product' ) ){
         $_product = wc_get_product( $atts['id'] );
         $html = "price = " . $_product->get_price();
    }
    return $html;
}
add_shortcode( 'woocommerce_price', 'so_30165014_price_shortcode_callback' );

Votre shortcode ressemblerait alors à [woocommerce_price id="99"]

88
helgatheviking

Dans le commerce,

Obtenir un prix régulier:

$price = get_post_meta( get_the_ID(), '_regular_price', true);
// $price will return regular price

Obtenir le prix de vente:

$sale = get_post_meta( get_the_ID(), '_sale_price', true);
// $sale will return sale price
23
Ash Patel