簡體   English   中英

獲取 WooCommerce 中的最小變動價格

[英]Get the minimum variation price in WooCommerce

我是一個初學者,試圖學習 Wordpress 並擁有價格可變的產品:

  • 鱈魚 250 盧比
  • 在線 200 盧比

我想在我的主題functions.php使用最低價格。

我將如何獲取或獲得最小值?

我想在下面的代碼中使用:

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;
}

要從WC_Product_Variable對象獲取 WooCommerce 中的最小變化活動價格:

$variation_min_price = $product->get_variation_price();

當購物車項目最小變化價格與他的產品類別 ID 匹配時,您似乎試圖在這里取消設置“鱈魚”支付網關。

使用 WordPress 條件函數has_term()將真正簡化您的代碼(它接受任何分類法的術語 Id、術語 slug 或術語名稱(此處為產品類別的“product_cat”)。

我想你的函數是掛在woocommerce_available_payment_gateways過濾鈎子里的……

我對您的代碼進行了一些更改,因為它有點過時並且有一些錯誤:

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;
}

代碼位於您的活動子主題(或主題)的 functions.php 文件中或任何插件文件中。

這應該有效(但我無法真正測試它,因為它對產品類別 ID 和最小變化價格非常具體) ......


可變產品類型的所有相關價格方法:

// 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

相關回答:

如果達到購物車項目數量限制,則禁用 WooCommerce 付款方式

我認為你正在尋找:

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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM