Ich bin ein Anfänger, der versucht, WordPress zu lernen, und habe ein variables Produkt mit Preisen:
- Kabeljau Rs 250
- Online Rs 200
Ich möchte den Mindestpreis in meinen Themen verwenden functions.php
.
Wie kann ich den Mindestwert abrufen oder erhalten?
Ich möchte im folgenden Code verwenden:
function filter_gateways($gateways){
$payment_NAME = 'cod'; //
$min_variation_price=""; <--------------- minimum variation price
global $woocommerce;
foreach ($woocommerce->cart->cart_contents as $key => $values ) {
// Get the terms, i.e. category list using the ID of the product
$terms = get_the_terms( $values['product_id'], 'product_cat' );
// Because a product can have multiple categories, we need to iterate through the list of the products category for a match
foreach ($terms as $term) {
// 20 is the ID of the category for which we want to remove the payment gateway
if($term->term_id == $min_variation_price){
unset($gateways[$payment_NAME]);
// If you want to remove another payment gateway, add it here i.e. unset($gateways['cod']);
break;
}
break;
}
}
return $gateways;
}
So erhalten Sie den aktiven Preis der Mindestvariation in WooCommerce von a WC_Product_Variable
Objekt:
$variation_min_price = $product->get_variation_price();
Es scheint, dass Sie hier versuchen, das Zahlungsgateway “Nachnahme” zu deaktivieren, wenn der Mindestvariationspreis eines Warenkorbartikels mit seiner Produktkategorie-ID übereinstimmt.
Verwenden der bedingten Funktion von WordPress has_term()
wird Ihren Code wirklich vereinfachen (es akzeptiert Begriffs-IDs, Begriffs-Slugs oder Begriffsnamen für jede Taxonomie (als ‘product_cat’ hier für Produktkategorien).
Ich nehme an, dass Ihre Funktion eingehakt ist woocommerce_available_payment_gateways
Filterhaken…
Ich habe einige Änderungen an Ihrem Code vorgenommen, da er etwas veraltet ist und einige Fehler enthält:
add_filter('woocommerce_available_payment_gateways', 'conditional_payment_gateways', 10, 1);
function conditional_payment_gateways( $available_gateways ) {
// Not in backend (admin)
if( is_admin() )
return $available_gateways;
foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
// Get the WC_Product object
$product = wc_get_product($cart_item['product_id']);
// Only for variable products
if($product->is_type('variable')){
// Get the Variation "min" price
$variation_min_price = $product->get_variation_price();
// If the product category match with the variation 'min" price
if( has_term( $variation_min_price, 'product_cat', $cart_item['product_id'] ) ){
// Cash on delivery (cod) payment gateway
unset($available_gateways['cod']); // unset 'cod'
break; // stop the loop
}
}
}
return $available_gateways;
}
Code geht in die functions.php-Datei Ihres aktiven Child-Themes (oder Themes) oder auch in jede Plugin-Datei.
Das sollte funktionieren (aber ich kann es nicht wirklich testen, da es sehr spezifisch in Bezug auf die Produktkategorie-ID und die Mindestvariationspreise ist)…
Alle zugehörigen Preismethoden für variable Produkttypen:
// All prices: multidimensional array with all variation prices (including active prices, regular prices and sale prices).
$prices = $product->get_variation_prices();
$prices_for_display = $product->get_variation_prices( true ); // For display
// The min or max active price.
$min_price = $product->get_variation_price(); // Min active price
$min_price_for_display = $product->get_variation_price('min', true); // Min active price for display
$max_price = $product->get_variation_price('max'); // Max active price
$max_price_for_display = $product->get_variation_price('max', true); // Max active price for display
// The min or max regular price.
$min_price = $product->get_variation_regular_price(); // Min regular price
$min_price_for_display = $product->get_variation_regular_price('min', true); // Min regular price for display
$max_price = $product->get_variation_regular_price('max'); // Max regular price
$max_price_for_display = $product->get_variation_regular_price('max', true); // Max regular price for display
// The min or max sale price.
$min_price = $product->get_variation_sale_price(); // Min sale price
$min_price_for_display = $product->get_variation_sale_price('min', true); // Min sale price for display
$max_price = $product->get_variation_sale_price('max'); // Max sale price
$max_price_for_display = $product->get_variation_sale_price('max', true); // Max sale price for display
Verwandte Antworten:
Deaktivieren Sie die WooCommerce-Zahlungsmethoden, wenn die Mengenbegrenzung des Einkaufswagens erreicht ist
Ich glaube du suchst das:
add_filter( 'woocommerce_variable_sale_price_html', 'get_min_variation_price_format', 10, 2 );
add_filter( 'woocommerce_variable_price_html', 'get_min_variation_price_format', 10, 2 );
function get_min_variation_price_format( $price, $product ) {
$min_variation_price = $product->get_variation_regular_price( 'min');
return wc_price($min_variation_price);
}