Produktpreise über einen Hook in WooCommerce 3+ ändern

Lesezeit: 5 Minuten

Produktpreise uber einen Hook in WooCommerce 3 andern
KronosL

IN WooCommerce muss ich alle Produktpreise mit einer Zahl multiplizieren. Also ich habe folgendes verwendet (über ein Plugin):

add_filter('woocommerce_get_regular_price', array( $this, 'my_custom_price'), 99);
add_filter('woocommerce_get_price', array( $this, 'my_custom_price'), 99);

function my_custom_price( $original_price ) {
  global $post, $woocommerce;

  //Logic for calculating the new price here
  $new_price = $original_price * 2;

  //Return the new price (this is the price that will be used everywhere in the store)
  return $new_price;
 }

Aber das funktioniert nicht für Variationsprodukte. Folgende Hooks habe ich erfolglos ausprobiert:

add_filter('woocommerce_get_variation_regular_price', array( $this, 'my_custom_price'), 99);
add_filter('woocommerce_get_variation_price', array( $this, 'my_custom_price'), 99);

Das einzige, das halbwegs funktioniert, ist dieses:

add_filter('woocommerce_variation_prices_price', array( $this, 'my_custom_price'), 99);

Damit ändert sich aber nur der Gesamtpreis, nicht der gewählte Variationspreis. Siehe das Bild unten, der Preis ist BsF. 200 und der Gesamtpreis stimmt, 200 x 2 = 400, aber der Variationspreis zeigt bei Auswahl immer noch 200 an:

Hinweis: Ich brauche es, um es tatsächlich zu ändern, also funktionieren HTML-Hooks nicht.

Variationspreis

Gibt es etwas, das ich vermisse, oder ist etwas falsch?

Produktpreise uber einen Hook in WooCommerce 3 andern
LoicTheAztec

Aktualisieren (Dezember 2020)

  • 2 Codeversionen für Themes und Plugins (funktioniert auch in Woocommerce 3.3.x)
  • Preise für zwischengespeicherte Variationen in Woocommerce 3 (Aktualisierung und Ergänzung):
    Jetzt mit woocommerce_get_variation_prices_hash Filterhaken viel effizienter, statt wc_delete_product_transients()… Sehen diesen verwandten Thread
  • Produktpreisfilter-Widget-Hooks hinzugefügt (siehe am ende).

1) Plugin-Version mit einer Konstruktorfunktion:

Die von Ihnen verwendeten Hooks sind in WooCommerce 3+ veraltet

Damit es für alle Produktpreise funktioniert, einschließlich Variationspreisesollten Sie dies verwenden:

## The following goes inside the constructor ##

// Simple, grouped and external products
add_filter('woocommerce_product_get_price', array( $this, 'custom_price' ), 99, 2 );
add_filter('woocommerce_product_get_regular_price', array( $this, 'custom_price' ), 99, 2 );
// Variations 
add_filter('woocommerce_product_variation_get_regular_price', array( $this, 'custom_price' ), 99, 2 );
add_filter('woocommerce_product_variation_get_price', array( $this, 'custom_price' ), 99, 2 );

// Variable (price range)
add_filter('woocommerce_variation_prices_price', array( $this, 'custom_variable_price' ), 99, 3 );
add_filter('woocommerce_variation_prices_regular_price', array( $this, 'custom_variable_price' ), 99, 3 );

// Handling price caching (see explanations at the end)
add_filter( 'woocommerce_get_variation_prices_hash', array( $this, 'add_price_multiplier_to_variation_prices_hash' ), 99, 3 );


## This goes outside the constructor ##

// Utility function to change the prices with a multiplier (number)
public function get_price_multiplier() {
    return 2; // x2 for testing
}

public function custom_price( $price, $product ) {
    return (float) $price * get_price_multiplier();
}

public function custom_variable_price( $price, $variation, $product ) {
    return (float) $price * get_price_multiplier();
}

public function add_price_multiplier_to_variation_prices_hash( $price_hash, $product, $for_display ) {
    $price_hash[] = get_price_multiplier();
    return $price_hash;
}

Der Code wurde getestet und funktioniert (nur) in WooCommerce 3+ perfekt.


2) Für Theme-Version: functions.php Datei zum aktiven Child-Theme (oder aktiven Theme):

// Utility function to change the prices with a multiplier (number)
function get_price_multiplier() {
    return 2; // x2 for testing
}

// Simple, grouped and external products
add_filter('woocommerce_product_get_price', 'custom_price', 99, 2 );
add_filter('woocommerce_product_get_regular_price', 'custom_price', 99, 2 );
// Variations
add_filter('woocommerce_product_variation_get_regular_price', 'custom_price', 99, 2 );
add_filter('woocommerce_product_variation_get_price', 'custom_price', 99, 2 );
function custom_price( $price, $product ) {
    return (float) $price * get_price_multiplier();
}

// Variable (price range)
add_filter('woocommerce_variation_prices_price', 'custom_variable_price', 99, 3 );
add_filter('woocommerce_variation_prices_regular_price', 'custom_variable_price', 99, 3 );
function custom_variable_price( $price, $variation, $product ) {
    // Delete product cached price  (if needed)
    // wc_delete_product_transients($variation->get_id());

    return (float) $price * get_price_multiplier();
}

// Handling price caching (see explanations at the end)
add_filter( 'woocommerce_get_variation_prices_hash', 'add_price_multiplier_to_variation_prices_hash', 99, 3 );
function add_price_multiplier_to_variation_prices_hash( $price_hash, $product, $for_display ) {
    $price_hash[] = get_price_multiplier();
    return $price_hash;
}

Getestet und funktioniert auf woocommerce 3+


Für Produkte im Verkauf haben Sie diese Haken:

  • woocommerce_product_get_sale_price (Einfache, gruppierte und externe Produkte)
  • woocommerce_variation_prices_sale_price (Variable Produkte (min-max))
  • woocommerce_product_variation_get_sale_price (Produktvariationen)

Zwischengespeicherte Preise und Woocommerce 3:

Die 3 Filter-Hooks, die an zwischengespeicherten Preisen beteiligt sind, sind:

  • woocommerce_variation_prices_price
  • woocommerce_variation_prices_regular_price
  • woocommerce_variation_prices_sale_price

Eingeführt in Woocommerce 3, woocommerce_get_variation_prices_hash Filterhaken wird ermöglichen es, zwischengespeicherte Preise für Variationen viel effizienter zu aktualisierenohne dass zugehörige Transienten jederzeit gelöscht werden, wenn diese Hooks ausgeführt werden.

So bleiben die Leistungen gesteigert (Danke an Matthew Clark, der diesen besseren Weg gezeigt hat)

Sehen: Caching und dynamische Preisgestaltung – bevorstehende Änderungen an der Methode get_variation_prices


Zum Filtern von Produktpreisen mit einem Widget (Mindest- und Höchstpreis)verwenden Sie die folgenden Hooks:

  • woocommerce_price_filter_widget_min_amount das hat ein Argument $price
  • woocommerce_price_filter_widget_max_amount das hat ein Argument $price

  • Danke! Jetzt funktioniert es nur, wenn ich DB-Transienten mit einem Plugin lösche, um die Änderungen zu sehen. Gibt es eine Möglichkeit, diese Transienten automatisch mit einer Funktion zu löschen?

    – KronosL

    23. August 2017 um 0:24 Uhr

  • Können Sie mir bitte zeigen, wie ich es richtig zur Funktion my_custom_price hinzufüge? Danke im Voraus, tolle Antwort!

    – KronosL

    23. August 2017 um 0:37 Uhr

  • @KronosL Aktualisierte meine Antwort mit wc_delete_product_transients($post->ID); … da dies spezifisch erscheint, wenn es von einem Plugin verwendet wird. Wenn Sie diese Hooks durch das Thema verwenden, besteht dieses Problem nicht. Danach wird dies nicht ausreichen… also müssen Sie ein wenig suchen.

    – LoicTheAztec

    23. August 2017 um 0:55 Uhr


  • das scheint nicht zu funktionieren, alle Preise werden zu 0

    – kos

    20. Januar 2018 um 15:52 Uhr

  • @kos Ich habe den Code getestet, korrigiert und aktualisiert … Es gibt jetzt 2 Versionen, also wähle die gute, je nachdem, ob es sich um ein Plugin oder die aktive Theme-Function.php-Datei handelt.

    – LoicTheAztec

    20. Januar 2018 um 23:43 Uhr

923820cookie-checkProduktpreise über einen Hook in WooCommerce 3+ ändern

This website is using cookies to improve the user-friendliness. You agree by using the website further.

Privacy policy