从Woocommerce可变价格变动的可变产品下拉列表中排除产品

时间:2019-05-06 19:56:00

标签: php wordpress woocommerce price variations

在WooCommerce中,基于Woocommerce get variation product price答案代码,我一直在使用以下代码来显示变量产品下拉菜单中每个变体旁边的价格:

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 Discount Rules插件进行价格折扣,这是其中包含的代码:

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

1 个答案:

答案 0 :(得分:0)

已更新 (有关Woo Discount Rules插件的用法-请参见结尾)

自WooCommerce 3以来,您的代码有点过时了:woocommerce_price()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 Discount Rules插件一起使用,请尝试替换:

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

作者:

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