简体   繁体   English

Woocommerce中特定产品的动态销售价格显示

[英]Dynamic sale price display for specific products in Woocommerce

I need to develop a script in function file that display sale price for specific categories for registered users.我需要在功能文件中开发一个脚本,为注册用户显示特定类别的销售价格。 This code work fine.这段代码工作正常。

I need to add sale price design to it我需要为其添加促销价设计

function thenga_customer_specific_pricing( $price, $product ) {    
    if ( ! is_user_logged_in() ) {
        return $price;
    }

    $id = $product->get_id();

    if( has_term( 'daniel-wellington', 'product_cat' ,$id ) ){
        // Give these customers a 20% discount.
        return $price * 0.8;
    } elseif( has_term( 'giardino-segreto', 'product_cat' ,$id ) ){
        return $price * 0.85;
    } else {
        return $price;
    }
}
add_filter( 'woocommerce_product_get_price', 'thenga_customer_specific_pricing', 10, 2 );

I expect an output like this:我期待这样的输出:

Was: 100€ Now: 80€原价:100 欧元 现在:80 欧元

I tried this filter :我试过这个过滤器:

function custom_dynamic_sale_price_html( $price_html, $product ) {
    if( $product->is_type('variable') ) return $price_html;

    $price_html = wc_format_sale_price( wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ), wc_get_price_to_display(  $product, array( 'price' => $product->get_sale_price() ) ) ) . $product->get_price_suffix();

    return $price_html;
}
add_filter( 'woocommerce_get_price_html', 'custom_dynamic_sale_price_html', 20, 2 );

but It work in all products, how I can call this filter only in specific categories?但它适用于所有产品,我如何仅在特定类别中调用此过滤器?

The following code will display your expected output (for simple products):以下代码将显示您的预期输出(对于简单产品):

// Discount prices
add_filter( 'woocommerce_product_get_price', 'specific_discounted_product_prices', 10, 2 );
function specific_discounted_product_prices( $price, $product ) {
    // For logged in customers
    if ( is_user_logged_in() ) {
        if( has_term( 'daniel-wellington', 'product_cat' ,$product->get_id() ) ){
            $price *= 0.8; // 20% discount
        } elseif( has_term( 'giardino-segreto', 'product_cat' ,$product->get_id() ) ){
            $price *= 0.85; // 15% discount
        }
    }
    return $price;
}

// Display the discount
add_filter( 'woocommerce_get_price_html', 'specific_discounted_product_prices_display', 10, 2 );
function specific_discounted_product_prices_display( $price, $product ) {
    // For simple products and logged in customers
    if( $product->is_type('simple') && is_user_logged_in() ){
        $regular_price = wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) );
        $sale_price    = wc_get_price_to_display( $product, array( 'price' => $product->get_sale_price() ) );
        $active_price  = wc_get_price_to_display( $product );

        if( $regular_price != $active_price ) {
            if( $product->is_on_sale() )
                $price = sprintf( 'Was: %s – Now: %s', wc_price($sale_price), wc_price($active_price) );
            else
                $price = sprintf( 'Was: %s – Now: %s', wc_price($regular_price), wc_price($active_price) );
        }
    }
    return $price;
}

Code goes in function.php file of your active child theme (or active theme).代码位于活动子主题(或活动主题)的 function.php 文件中。 Tested and works.测试和工作。

In case you came here to make it work with variations, you need all the following filters:如果你来这里是为了让它与变体一起工作,你需要以下所有过滤器:

add_filter( 'woocommerce_product_get_price', 'specific_discounted_product_prices', 10, 2 );
add_filter( 'woocommerce_product_variation_get_price', 'specific_discounted_product_prices', 10, 2 );
add_filter( 'woocommerce_product_get_sale_price', 'specific_discounted_product_prices', 10, 2 );
add_filter( 'woocommerce_product_variation_get_sale_price', 'specific_discounted_product_prices', 10, 2 );
add_filter( 'woocommerce_variation_prices_price', 'specific_discounted_product_prices', 10, 2 );
add_filter( 'woocommerce_variation_prices_sale_price', 'specific_discounted_product_prices', 10, 2 );

And you need to make sure you've changed the woocommerce_get_variation_prices_hash becasue of the stored transients.你需要确保你已经改变了woocommerce_get_variation_prices_hash因为存储的瞬态。

You may skip the woocommerce_get_price_html if you use the woocommerce_product_get_sale_price filter as well.如果您也使用woocommerce_product_get_sale_price过滤器,则可以跳过woocommerce_get_price_html

You may find the gist I've created for a client useful您可能会发现我为客户创建的要点很有用

https://www.rent-a-ninja.org/programming/wordpress-plugin-entwickler-naehe-graz/woocommerce-custom-sale-price-for-custom-role/ https://www.rent-a-ninja.org/programming/wordpress-plugin-entwickler-naehe-graz/woocommerce-custom-sale-price-for-custom-role/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM