Ich versuche, einen Parameter mithilfe einer URL an eine Woocommerce-Site zu übergeben – zum Beispiel:
site/?custom_p=77
wird die URL sein.
Hier ist mein Code
add_filter('woocommerce_product_get_price', 'custom_price', 99, 2 );
add_filter('woocommerce_product_get_regular_price', 'custom_price', 99, 2 );
function custom_price( $cprice, $product ) {
// Delete product cached price (if needed)
wc_delete_product_transients($product->get_id());
// Get the data from the GET request
$custom_p=$_GET['custom_p'];
return ($custom_p) ;
}
URL in den Warenkorb legen site/?add-to-cart=455&custom_p=77
Das Problem, wenn ich die Nummer deklariere 77
direkt funktioniert es $custom_p=77
aber $_GET['custom_p']
Deklaration ergibt als Ergebnis Null im Warenkorb.
Ihre GET-Variablen gehen verloren, sobald sich die URL ändert, sodass alle Produktpreise leer werden, da Sie auch nicht auf das Produkt abzielen, das dem Warenkorb hinzugefügt wurde.
In diesem Fall müssen Sie eine WooCommerce-Sitzungsvariable aktivieren und verwenden, um die Produkt-ID und den benutzerdefinierten Preis zu speichern, wenn a custom_p
GET-Variable erkannt. Dann können Sie diese WooCommerce-Sitzungsvariable verwenden, um den Produktpreis zu ändern.
Zuerst erfassen und speichern wir die notwendigen Daten in a WC_Session
Variable:
// get and set the custom product price in WC_Session
add_action( 'init', 'get_custom_product_price_set_to_session' );
function get_custom_product_price_set_to_session() {
// Check that there is a 'custom_p' GET variable
if( isset($_GET['add-to-cart']) && isset($_GET['custom_p'])
&& $_GET['custom_p'] > 0 && $_GET['add-to-cart'] > 0 ) {
// Enable customer WC_Session (needed on first add to cart)
if ( ! WC()->session->has_session() ) {
WC()->session->set_customer_session_cookie( true );
}
// Set the product_id and the custom price in WC_Session variable
WC()->session->set('custom_p', [
'id' => (int) wc_clean($_GET['add-to-cart']),
'price' => (float) wc_clean($_GET['custom_p']),
]);
}
}
Dann gibt es zwei Möglichkeiten, den Preis des in den Warenkorb gelegten Produkts zu ändern (wählen Sie nur eine)
Option 1 – Produktpreis direkt ändern:
// Change product price from WC_Session data
add_filter('woocommerce_product_get_price', 'custom_product_price', 900, 2 );
add_filter('woocommerce_product_get_regular_price', 'custom_product_price', 900, 2 );
add_filter('woocommerce_product_variation_get_price', 'custom_product_price', 900, 2 );
add_filter('woocommerce_product_variation_get_regular_price', 'custom_product_price', 900, 2 );
function custom_product_price( $price, $product ) {
if ( ( $data = WC()->session->get('custom_p') ) && $product->get_id() == $data['id'] ) {
$price = $data['price'];
}
return $price;
}
Option 2 – Ändern Sie den Warenkorbartikelpreis:
// Change cart item price from WC_Session data
add_action( 'woocommerce_before_calculate_totals', 'custom_cart_item_price', 20, 1 );
function custom_cart_item_price( $cart ){
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Must be required since Woocommerce version 3.2 for cart items properties changes
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
// Looo through our specific cart item keys
foreach ( $cart->get_cart() as $cart_item ) {
// Get custom product price for the current item
if ( ( $data = WC()->session->get('custom_p') ) && $cart_item['data']->get_id() == $data['id'] ) {
// Set the new product price
$cart_item['data']->set_price( $data['price'] );
}
}
}
Der Code wird in die Datei functions.php Ihres aktiven untergeordneten Designs (oder aktiven Designs) eingefügt. Getestet und funktioniert.
Beispiel für URL-Variablen für die Verwendung: www.example.com/?add-to-cart=37&custom_p=75
Option 1 gibt einen schwerwiegenden Fehler bei WC Admin aus. Um das zu beheben, musste ich hinzufügen !is_admin()
zur if-Klausel, also sollte es so aussehen:
add_filter('woocommerce_product_get_price', 'custom_product_price', 900, 2 );
add_filter('woocommerce_product_get_regular_price', 'custom_product_price', 900, 2 );
add_filter('woocommerce_product_variation_get_price', 'custom_product_price', 900, 2 );
add_filter('woocommerce_product_variation_get_regular_price', 'custom_product_price', 900, 2 );
function custom_product_price( $price, $product ) {
if ( ( !is_admin() && $data = WC()->session->get('custom_p') ) && $product->get_id() == $data['id'] ) {
$price = $data['price'];
}
return $price;
}
Schön, es funktioniert sogar, wenn Dreamweaver sagt, dass es im ersten Teil, wo die WC_Session-Variable gesetzt ist, einen Syntaxfehler gibt:
// Set the product_id and the custom price in WC_Session variable
WC()->session->set('custom_p', [
'id' => (int) wc_clean($_GET['add-to-cart']),
'price' => (float) wc_clean($_GET['custom_p']),
]);
.
@RST Seit Woocommerce Version 3 ist woocommerce_get_price der veraltete Hook.
– LoicTheAztec
17. Juni 20 um 16:44 Uhr