dongyoufo5672 2019-05-06 19:55
浏览 57

从价格变化的Woocommerce变量产品下拉列表中排除产品

In WooCommerce, based on "Woocommerce get variation product price" answer code, I have been using the following to show the price beside each variation on my variable products dropdowns:

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

It works great on all products except one (which I use a separate plugin for as it's for a gift card).

Is it possible to exclude one specific product ID or a product category, OR for it to apply to only specific product categories? If so, what should be added to the code and where to apply that?

I also use Woo Discount Rules plugin for price discounts and that's the code included for that:

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条回答 默认 最新

  • douhuangjie4503 2019-05-07 04:23
    关注

    Updated (for Woo Discount Rules plugin usage - see at the end)

    Your code is a bit outdated since WooCommerce 3: woocommerce_price() is replaced by wc_Price()

    In the following revisited code, you will be able to exclude some product Ids:

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

    Code goes in functions.php file of your active child theme (or active theme). Tested and works.


    Maybe, to make it work with the Woo Discount Rules plugin, try to replace:

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

    by:

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

报告相同问题?

悬赏问题

  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 MATLAB中streamslice问题
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端