簡體   English   中英

在 Woocommerce 中為特定類別的產品價格前添加文本

[英]Add text before product price for specific category in Woocommerce

在 woocommerce 中,我使用如果產品價格高於 Woocommerce 中的特定金額,則在產品價格之前添加文本答案代碼在所有價格之前添加文本。

如何使此代碼僅適用於特定產品類別?

任何幫助表示贊賞。

以下代碼適用於特定的產品類別。 該代碼使用產品類別的自定義條件函數處理父產品類別和產品變體:

// Custom conditional function that handle parent product categories too
function has_product_categories( $categories, $product_id = 0 ) {
    $parent_term_ids = $categories_ids = array(); // Initializing
    $taxonomy        = 'product_cat';
    $product_id      = $product_id == 0 ? get_the_id() : $product_id;

    if( is_string( $categories ) ) {
        $categories = (array) $categories; // Convert string to array
    }

    // Convert categories term names and slugs to categories term ids
    foreach ( $categories as $category ){
        $result = (array) term_exists( $category, $taxonomy );
        if ( ! empty( $result ) ) {
            $categories_ids[] = reset($result);
        }
    }

    // Loop through the current product category terms to get only parent main category term
    foreach( get_the_terms( $product_id, $taxonomy ) as $term ){
        if( $term->parent > 0 ){
            $parent_term_ids[] = $term->parent; // Set the parent product category
            $parent_term_ids[] = $term->term_id; // (and the child)
        } else {
            $parent_term_ids[] = $term->term_id; // It is the Main category term and we set it.
        }
    }
    return array_intersect( $categories_ids, array_unique($parent_term_ids) ) ? true : false;
}

add_filter( 'woocommerce_get_price_html', 'prepend_text_to_product_price', 20, 2 );
function prepend_text_to_product_price( $price_html, $product ) {
    // HERE set your product category in the array
    $product_category = array('clothing');

    // Only on frontend and excluding min/max prices for variable products
    if( is_admin() || $product->is_type('variable') )
        return $price_html;

    // Get the variable product ID for product variations (as variations dont handle product categories)
    $product_id = $product->is_type('variation') ? $product->get_parent_id() : $product->get_id();

    // Only for a specific product category
    if( ! has_product_categories( $product_id, $product_category ) )
        return $price_html;

    // Get product price
    $price = (float) $product->get_price(); // Regular price

    if( $price > 15 )
        $price_html = '<span>'.__('(GRATIS)', 'woocommerce' ).'</span> '.$price_html;

    return $price_html;
}

代碼位於活動子主題(或活動主題)的 function.php 文件中。 測試和工作。


您還可以使用has_term()條件函數(不處理父產品類別)

add_filter( 'woocommerce_get_price_html', 'prepend_text_to_product_price', 20, 2 );
function prepend_text_to_product_price( $price_html, $product ) {
    // HERE set your product category in the array
    $product_category = array('clothing');

    // Only on frontend and excluding min/max prices for variable products
    if( is_admin() || $product->is_type('variable') )
        return $price_html;

    // Get the variable product ID for product variations (as variations dont handle product categories)
    $product_id = $product->is_type('variation') ? $product->get_parent_id() : $product->get_id();

    // Only for a specific product category
    if( ! has_product_categories( $product_id, $product_category ) )
        return $price_html;

    // Get product price
    $price = (float) $product->get_price(); // Regular price

    if( $price > 15 )
        $price_html = '<span>'.__('(GRATIS)', 'woocommerce' ).'</span> '.$price_html;

    return $price_html;
}

代碼位於活動子主題(或活動主題)的 functions.php 文件中。 測試和工作。

相關主題: 如果產品價格高於 Woocommerce 中的特定金額,則在產品價格前添加文本

這應該有效:在該問題的現有答案中添加了類別的 has_term。

if ( has_term( 'putcategorynamehere', `'product_cat'` ) ) {
add_filter( 'woocommerce_get_price_html', 'prepend_text_to_product_price', 20, 2 );
function prepend_text_to_product_price( $price_html, $product ) {
    // Only on frontend and excluding min/max prices on variable products
    if( is_admin() || $product->is_type('variable') ) 
        return $price_html;

    // Get product price
    $price = (float) $product->get_price(); // Regular price

    if( $price > 15 )
        $price_html = '<span>'.__('(GRATIS)', 'woocommerce' ).'</span> '.$price_html;

    return $price_html;
}
}

暫無
暫無

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

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