首页 > 解决方案 > 从具有变化价格的 Woocommerce 可变产品下拉列表中排除产品

问题描述

在 WooCommerce 中,基于Woocommerce 获取变体产品价格答案代码,我一直在使用以下内容在我的可变产品下拉列表的每个变体旁边显示价格:

add_filter( 'woocommerce_variation_option_name', 'display_price_in_variation_option_name' );
function display_price_in_variation_option_name( $term ) {
    global $wpdb, $product;

    $result = $wpdb->get_col( "SELECT slug FROM {$wpdb->prefix}terms WHERE name = '$term'" );

    $term_slug = ( !empty( $result ) ) ? $result[0] : $term;

    $query = "SELECT postmeta.post_id AS product_id
        FROM {$wpdb->prefix}postmeta AS postmeta
        LEFT JOIN {$wpdb->prefix}posts AS products ON ( products.ID = postmeta.post_id )
        WHERE postmeta.meta_key LIKE 'attribute_%'
        AND postmeta.meta_value = '$term_slug'
        AND products.post_parent = $product->id";

    $variation_id = $wpdb->get_col( $query );

    $parent = wp_get_post_parent_id( $variation_id[0] );

    if ( $parent > 0 ) {
        $_product = new WC_Product_Variation( $variation_id[0] );
        return $term . ' (' . woocommerce_price( $_product->get_price() ) . ')';
    }
    return $term;
}

它适用于除一个之外的所有产品(我使用单独的插件,因为它用于礼品卡)。

是否可以排除一个特定的产品 ID 或产品类别,或者仅适用于特定的产品类别?如果是这样,应该在代码中添加什么以及在哪里应用?

我还使用 Woo 折扣规则插件进行价格折扣,这就是其中包含的代码:

function woocs_fixed_raw_woocommerce_price_method($tmp_val, $product_data, $price){
    remove_filter('woocs_fixed_raw_woocommerce_price', 'woocs_fixed_raw_woocommerce_price_method', 10, 3);
    global $flycart_woo_discount_rules;
    if(!empty($flycart_woo_discount_rules)){
        global $product;
        if(empty($product)){
            $discount_price = $flycart_woo_discount_rules->pricingRules->getDiscountPriceOfProduct($product_data);
            if($discount_price !== null) $tmp_val = $discount_price;
        }
    }
    add_filter('woocs_fixed_raw_woocommerce_price', 'woocs_fixed_raw_woocommerce_price_method', 10, 3);

    return $tmp_val;
}
add_filter('woocs_fixed_raw_woocommerce_price', 'woocs_fixed_raw_woocommerce_price_method', 10, 3);

标签: phpwordpresswoocommercepricevariations

解决方案


更新 (对于 Woo 折扣规则插件的使用 - 见最后)

woocommerce_price()自从 WooCommerce 3:被替换后,您的代码有点过时了wc_Price()

在以下重新访问的代码中,您将能够排除一些产品 Id:

// for woocommerce 3+
add_filter( 'woocommerce_variation_option_name', 'display_price_in_variation_option_name' );
function display_price_in_variation_option_name( $term_name ) {
    global $product, $wpdb;

    if( ! is_a($product, 'WC_Product') )
        return $term_name;

    // Excluding product IDs
    $excluded_product_ids = array(37, 53);
    if( in_array( $product->get_id(), $excluded_product_ids ) )
        return $term_name;

    if( $product->is_type('variable') && ! empty($term_name) ) {
        $term_slug = $wpdb->get_var("SELECT slug FROM {$wpdb->prefix}terms WHERE name = '$term_name'");

        $term_slug = empty($term_slug) ? sanitize_title( $term_name ) : $term_slug;

        $variation_id = (int) $wpdb->get_var( $wpdb->prepare("
            SELECT postmeta.post_id AS product_id
            FROM {$wpdb->prefix}postmeta AS postmeta
            LEFT JOIN {$wpdb->prefix}posts AS products
                ON ( products.ID = postmeta.post_id )
            WHERE postmeta.meta_key LIKE 'attribute_%'
                AND postmeta.meta_value = '%s'
                AND products.post_parent = %d
        ", $term_slug, $product->get_id() ) );

        if( $variation_id > 0 ){
            $variation = wc_get_product( $variation_id );

            $term_name .= ' (' . strip_tags( wc_price( wc_get_price_to_display( $variation ) ) ) . ')';
        }
    }
    return $term_name;
}

代码在您的活动子主题(或活动主题)的 functions.php 文件中。测试和工作。


也许,要使其与Woo 折扣规则插件一起使用,请尝试替换:

$term_name .= ' (' . strip_tags( wc_price( wc_get_price_to_display( $variation ) ) ) . ')';

经过:

$term_name .= ' (' . strip_tags( wc_price( $variation->get_price() ) ) . ')';

推荐阅读