
Kuberban Aliye
Ich möchte ein PHP-Code-Snippet erstellen, das den Gesamtpreis auf der WooCommerce-Einzelproduktseite mit dem ursprünglichen Produktpreis * Mindestmenge anzeigt
Basierend auf dem Antwortcode von WooCommerce – Gesamtpreis automatisch aktualisieren, wenn sich die Menge geändert hat, habe ich bisher Folgendes:
add_action( 'woocommerce_single_product_summary', 'woocommerce_total_product_price', 31 );
function woocommerce_total_product_price() {
global $woocommerce, $product;
// let's setup our divs
echo sprintf('<div id="product_total_price" style="margin-bottom:20px;">%s %s</div>',__('Prezzo Totale:','woocommerce'),'<span class="price">'.$product->get_price().'</span>');
?>
<script>
jQuery(function($){
var price = <?php echo $product->get_price(); ?>,
currency = '<?php echo get_woocommerce_currency_symbol(); ?>';
$('[name=quantity]').change(function(){
if (!(this.value < 1)) {
var product_total = parseFloat(price * this.value);
$('#product_total_price .price').html( currency + product_total.toFixed(2));
}
});
});
</script>
<?php
}
Das Problem ist, dass beim Anzeigen der Einzelproduktseite der Preis für die Menge eines Produkts angezeigt wird. Das Änderungsereignis tritt ein, wenn die Menge geändert wurde, daher werden keine Berechnungen durchgeführt, bevor dieses Ereignis stattgefunden hat.
Ich habe eine B2B-Website. Wenn Kunden also die Seite mit einem einzelnen Produkt aufrufen, sehen sie sofort die Mindestmenge. Wie kann ich also sofort den Gesamtpreis für die Mindestmenge für jedes Produkt erhalten?
Der folgende Code berücksichtigt die Produktmenge beim Laden der Seite, dann wird der Preis aktualisiert, wenn die Produktmenge geändert wird.
Erklärung über im Code hinzugefügte Kommentar-Tags
function action_woocommerce_single_product_summary() {
global $product;
// Getters
$price = $product->get_price();
$currency_symbol = get_woocommerce_currency_symbol();
// let's setup our div
echo sprintf('<div id="product_total_price" style="margin-bottom:20px;">%s %s</div>', __('Product Total:','woocommerce'), '<span class="price">' . $currency_symbol . $price . '</span>' );
?>
<script>
jQuery(function($) {
// jQuery variables
var price = <?php echo $price; ?>,
currency = '<?php echo $currency_symbol; ?>',
quantity = $( '[name=quantity]' ).val();
// Code to run when the document is ready
var product_total = parseFloat( price * quantity );
$( '#product_total_price .price' ).html( currency + product_total.toFixed( 2 ) );
// On change
$( '[name=quantity]' ).change( function() {
if ( ! ( this.value < 1 ) ) {
product_total = parseFloat( price * this.value );
$( '#product_total_price .price' ).html( currency + product_total.toFixed( 2 ) );
}
});
});
</script>
<?php
}
// We are going to hook this on priority 31, so that it would display below add to cart button.
add_action( 'woocommerce_single_product_summary', 'action_woocommerce_single_product_summary', 31, 0 );
$product->get_price()
wird den Preis für 1 Produkt zurückgeben.
Sie können verwenden get_min_purchase_quantity()
und zum ersten Mal mit dem Preis multiplizieren.
add_action( 'woocommerce_single_product_summary', 'woocommerce_total_product_price', 31 );
function woocommerce_total_product_price() {
global $woocommerce, $product;
// let's setup our divs
$price = $product->get_price();
$min_qnty = $product->get_min_purchase_quantity();
$initial_price = floatval($price) * intval ($min_qnty);
echo sprintf('<div id="product_total_price" style="margin-bottom:20px;">%s %s</div>',__('Prezzo Totale:','woocommerce'),'<span class="price">'.$initial_price.'</span>');
?>
<script>
jQuery(function($){
var price = <?php echo $product->get_price(); ?>,
currency = '<?php echo get_woocommerce_currency_symbol(); ?>';
$('[name=quantity]').change(function(){
if (!(this.value < 1)) {
var product_total = parseFloat(price * this.value);
$('#product_total_price .price').html( currency + product_total.toFixed(2));
}
});
});
</script>
<?php
}
Ich hatte ein ähnliches Problem und habe viele Antworten gefunden, aber diese, die ich basierend auf den vorherigen Antworten und Online-Quellen geschrieben habe, ist die einzige, die perfekt mit Mindestmengen und Problemen beim Neuladen von Seiten funktionierte, die nicht den richtigen Gesamtpreis anzeigten
// Priority 31 is placed below the 'add to cart' button
add_action( 'woocommerce_single_product_summary', 'action_woocommerce_single_product_summary', 31, 0 );
function action_woocommerce_single_product_summary() {
global $product;
$price = $product->get_price();
$currency_symbol = get_woocommerce_currency_symbol();
$min_qnty = $product->get_min_purchase_quantity();
$initial_price = floatval($price) * intval ($min_qnty);
// leave the span class 'prezzo' as it is or it won't render the price
echo sprintf('<div id="product_total_price">%s %s</div>', __('Total:','woocommerce'), '<span class="prezzo">' . $currency_symbol . $initial_price . '</span>' );
?>
<script>
jQuery(function($) {
// vars
var price = <?php echo $price; ?>,
currency = '<?php echo $currency_symbol; ?>',
quantity = $( '[name=quantity]' ).val();
var product_total = parseFloat( price * quantity );
$( '#product_total_price .prezzo' ).html( currency + product_total.toFixed( 2 ) );
// On change
$( '[name=quantity]' ).change( function() {
if ( ! ( this.value < 1 ) ) {
product_total = parseFloat( price * this.value );
$( '#product_total_price .prezzo' ).html( currency + product_total.toFixed( 2 ) );
}
});
});
</script>
<?php
}
10047100cookie-checkZeigen Sie den Gesamtpreis auf der WooCommerce-Einzelproduktseite mit dem ursprünglichen Produktpreis * Mindestmenge anyes