如果销售价格满足WooCommerce中的变化,请添加“销售”徽章

用户名

在WooCommerce中,对于简单的产品,我可以添加“销售!” 这样的徽章:

add_action( 'woocommerce_before_shop_loop_item_title', function() {
    global $product; 
    $akc = get_post_meta(get_the_ID(), '_sale_price', true);
    if ( $akc > 0 ) {
        echo '<span class="onsale soldout">Sale!</span>';
    }
});

对于可变产品,它不起作用...
如何使用“ sale_price”获取所有变化字段的值
那么如何使其适用于可变产品呢?

通过此代码只为每个变量产品提供结果:

add_action( 'woocommerce_before_shop_loop_item_title', function($price, $_this) {
    global $product; 

    if($product->product_type == 'variable') {

        $variation_rrp_price = get_sale_price( $variation_price, $product_id );

        $t = get_post_meta(get_the_ID(), $prices_array['_sale_price'], $variation_price); var_dump($t);
        if ( !empty ($t)  ) {
            echo '<span class="onsale soldout">Sale!</span>';
        }
    }
});

更新

因此,我通过以下代码获得自定义价格:

function get_rrp_price( $price, $product_id ) {
    $rooms2 = get_post_meta( $product_id, 'rooms2', true );
    if ( '' === $rooms2) {
        $rooms2 = 1; 
    }

    $kurs = 1;
    $kurs2 = 10;
    $kurs3 = 100;

    switch ( $rooms2 ) {
        case 1:
            $rrp_price = $price * $kurs2;
            break;
        case 2:
            $rrp_price = $price * $kurs3;
            break;
        default:
            $rrp_price = $price * $kurs;
            break;
    }

    return $rrp_price;
}

function filter_woocommerce_get_price( $price, $_this ) {
    $product_id = $_this->id;
    $rrp_price = get_rrp_price( $price, $product_id );
    update_post_meta( $product_id, 'rrp_price', $rrp_price );

    return $rrp_price;
}

function filter_736700_woocommerce_product_variation_get_price( $price, $_this) {
    if ( 'product_variation' === $_this->post_type ) {
        $data            = (object) $_this->get_data();
        $variation_price = $data->price;
        $product_id       = $data->parent_id;
        $variation_rrp_price = get_rrp_price( $variation_price, $product_id );
        $price = $variation_rrp_price;
    }

    return $price;
}

add_filter( 'woocommerce_product_variation_get_price', 'filter_736700_woocommerce_product_variation_get_price', 10, 2);

function filter_736700_woocommerce_variation_prices( $prices_array, $product, $include_taxes ) {


    $product_id = $product->id;

    foreach ( $prices_array['price'] as $key => $price ) {
        $prices_array['price'][ $key ] = get_rrp_price( $price, $product_id );
    }

    foreach ( $prices_array['regular_price'] as $key => $price ) {
        $prices_array['price'][ $key ] = get_rrp_price( $price, $product_id );
    }

    foreach ( $prices_array['sale_price'] as $key => $price ) {
        $prices_array['price'][ $key ] = get_rrp_price( $price, $product_id );
    }

    return $prices_array;
}

add_filter( 'woocommerce_variation_prices', 'filter_736700_woocommerce_variation_prices', 10, 3 );


function filter_738363_woocommerce_product_variation_get_regular_price( $price, $_this ) {
    if ( 'product_variation' === $_this->post_type ) {
        $data            = (object) $_this->get_data();
        $variation_price = $data->regular_price;
        $product_id       = $data->parent_id;
        $variation_rrp_price = get_rrp_price( $variation_price, $product_id );
        update_post_meta( $product_id, $data->name . ' - regular' , $variation_rrp_price );
        $price = $variation_rrp_price;
    }

    return $price;
}

add_filter( 'woocommerce_product_variation_get_regular_price', 'filter_738363_woocommerce_product_variation_get_regular_price', 10, 2);

但是在此之后“出售!” 不再分配(仅适用于可变产品)

LoicTheAztec

为了使您的代码适用于所有产品类型,您应该使用方法。这样,您将只对所有产品类型使用以下一种功能:WC_Product is_on_sale()

add_action( 'woocommerce_before_shop_loop_item_title', 'custom_shop_loop_on_sale_badge' );
function custom_shop_loop_on_sale_badge(){
    global $product;

    if($product->is_on_sale())
        echo '<span class="onsale soldout">Sale!</span>';
}

代码在您的活动子主题(或主题)的function.php文件中,或者在任何插件文件中。

经过测试和工作。


更新 (与您的问题中的更新有关)

要使某些功能与您的销售计算价格一致,请尝试以下操作:

// Prices calculation common function
function get_rrp_price( $price, $product_id ) {
    $rooms2 = get_post_meta( $product_id, 'rooms2', true );
    if ( '' === $rooms2 )
        $rooms2 = 1;

    if ( $rooms2 == 1 )
        $price *= 10;
    elseif ( $rooms2 == 2 )
        $price *= 100;

    return $price;
}

// Simple products prices
add_filter( 'woocommerce_product_get_price', 'custom_simple_product_price', 20, 2 );
add_filter( 'woocommerce_product_get_regular_price', 'custom_simple_product_price', 20, 2 );
function custom_simple_product_price( $price, $product ) {
    $price = get_rrp_price( $price, $product->get_id() );
    update_post_meta( $product->get_id(), 'rrp_price', $price );

    return $price;
}

// Variable products: Selected variation prices
add_filter( 'woocommerce_variation_prices_price', 'custom_variations_prices', 10, 3 );
add_filter( 'woocommerce_variation_prices_regular_price', 'custom_variations_prices', 10, 3 );
add_filter( 'woocommerce_variation_prices_sale_price', 'custom_variations_prices', 10, 3 );
function custom_variations_prices( $price, $variation, $product ) {
    if (  $variation->is_type('variation') ) {
        $product_id = $product->get_id();

        $price = get_rrp_price( $price, $product_id );
    }
    return $price;
}

// Variable products: Min/Max variations prices
add_filter( 'woocommerce_product_variation_get_regular_price', 'custom_variations_get_prices' , 10, 2 );
add_filter( 'woocommerce_product_variation_get_sale_price', 'custom_variations_get_prices' , 10, 2 );
add_filter( 'woocommerce_product_variation_get_price', 'custom_variations_get_prices', 10, 2 );
function custom_variations_get_prices( $price, $variation ) {

    if (  $variation->is_type('variation') ) {
        $parent_product_id = $variation->get_parent_id();
        $rrp_price = get_rrp_price( $price, $product_id );

        // Adding/Updating "rrp regular price" custom field
        $regular_price = $variation->get_regular_price();
        if( $regular_price == $price )
            update_post_meta( $product_id, $variation->get_name() . ' - regular' , $rrp_price );

        $price = $rrp_price;
    }
    return $price;
}

代码在您的活动子主题(或主题)的function.php文件中,或者在任何插件文件中。

经过测试和工作。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如果没有销售价格,如何显示woocommerce销售价格或常规价格

在Woocommerce Single product中添加带有销售价格后的销售日期的自定义文本

在Woocommerce中用常规价格代替销售价格

以编程方式销售价格产品的Woocommerce问题

在WooCommerce中显示正常价格之前的销售价格

从WooCommerce管理中的所有产品中删除销售价格

在WooCommerce 3中以编程方式设置产品销售价格

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

在WooCommerce 3中以编程方式仅设置特定产品的销售价格

Woocommerce中的销售倒计时后在产品编辑页面自动清除销售价格

将WooCommerce产品的销售价格复制到正常价格并重置销售价格

用Javascript提取常规价格或销售价格

XPATH匹配的销售价格或正常价格

在正常价格之前显示销售价格(WooCommerce)

Woocommerce如何替换缺货产品的销售价格但保持正常价格?

在Woocommerce中将自定义“销售价格”列添加到管理产品列表

设置购物车商品仅针对Woocommerce中的特定商品生成的销售价格

Woocommerce产品报废常规和销售价格未提取

根据URL变量启用或禁用WooCommerce产品销售价格

修改Woocommerce查询以仅显示具有销售价格的产品

如何使用自定义产品属性值更新woocommerce销售价格?

更新Woocommerce可变产品的所有产品差异销售价格

在WooCommerce结帐问题中显示产品销售价格

根据python中的相关费用计算销售价格

使用PHP从Google Content API for Shopping中删除销售价格

如何在woocommerce的购物车页面中同时显示总订单销售价格和常规价格(或总折扣价格)

应用优惠券时,将 WooCommerce 购物车中的产品销售价格更改为正常价格,但排除某些类别

Power BI - 基于连续平均销售价格的同比变化百分比

通过每次销售/订购提高产品的销售价格