2017-04-07 3 views
4

Ich habe von 2.6.14 auf WC 3.0.1 aktualisiert.
Mein ursprünglicher Code ist wie folgt:woocommerce_before_calculate_totals Hook funktioniert nicht mehr nach Update auf WC 3.0.1

add_action('woocommerce_before_calculate_totals', 'add_custom_price'); 

function add_custom_price($cart_object) { 
    $custom_price = 10; // This will be your custome price 
    foreach ($cart_object->cart_contents as $key => $value) { 
     $value['data']->price = $custom_price; 
    } 
} 

Es ist nicht mehr der Preis im Warenkorb oder minicart aktualisieren.

+0

Sie können nicht mehr direkt auf die Eigenschaften von $ product zugreifen. Wenn Sie 'WP_DEBUG' aktiviert haben, sollten Sie eine Warnung dazu im' debug.log' sehen. Sie müssen nun Setter und Getter für Produkt-, Auftrags-, Bestellartikel- und Coupon-Objekte verwenden. – helgatheviking

+0

Danke helgatheviking, Könnten Sie bitte ein Beispiel für die Preisfindung und -einstellung geben? –

+1

Schauen Sie sich 'set_price()' in der [source] an (https://github.com/woocommerce/woocommerce/blob/3.0.0/includes/abstracts/abstract-wc-product.php#L804-L806) – helgatheviking

Antwort

7

Zum Überschreiben des Produktpreises auf dem Warenkorb in der neuesten Version von Woocommerce (3.0.1) versuchen Sie die set_price ($ price) -Funktion in woocommerce dies hilft. Source here

add_action('woocommerce_before_calculate_totals', 'woocommerce_pj_update_price', 99); 

function woocommerce_pj_update_price() { 

    $custom_price = $_COOKIE["donation"]; // This will be your custom price 
    $target_product_id = 413; //Product ID 

    foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) { 

     if($cart_item['data']->get_id() == $target_product_id){ 

      $cart_item['data']->set_price($custom_price); 
     } 

    } 

} 
+1

3 Stunden meiner Zeit verschwendet Suche nach einer Lösung für diese. Dank dir ist nicht 3 Tage ... –

+1

lol. froh, dass ich @CarlosFaria geholfen habe! –

0

Arbeiten mit einer kleinen Änderung:

//OLD: 
$value['data']->price = $custom_price; 

//NEW: 
$value['data']->set_price($custom_price); 

function add_custom_price($cart_object) { 
    $custom_price = 10; // This will be your custome price 
    foreach ($cart_object->cart_contents as $key => $value) { 
     $value['data']->set_price($custom_price); 
    } 
} 
0
add_action('woocommerce_before_calculate_totals', 'add_custom_price', 10, 1); 
function add_custom_price($cart_obj) { 

    // This is necessary for WC 3.0+ 
    if (is_admin() && ! defined('DOING_AJAX')) 
     return; 

    foreach ($cart_obj->get_cart() as $key => $value) { 
     $value['data']->set_price(40); 
    } 
} 

wenn i $ Wert [ 'data'] -> set_price (40) funktionieren, aber:

foreach ($cart_obj->get_cart() as $key => $value) { 
      $price = 50; 
      $value['data']->set_price($price); 
} 
Verwandte Themen