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

a7qyws3x  于 2022-11-22  发布在  WordPress
关注(0)|答案(1)|浏览(187)

在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);
5lhxktic

5lhxktic1#

已更新*(有关Woo折扣规则插件的使用-请参阅末尾)*

您的代码有点过时,因为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() ) ) . ')';

相关问题