设置购物车商品仅在Woocommerce中为特定商品生成的销售价格 [英] Set cart item product generated sale price only for specific products in Woocommerce

查看:85
本文介绍了设置购物车商品仅在Woocommerce中为特定商品生成的销售价格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按照仅设置特定产品在WooCommerce 3 中以编程方式销售价格时,相关的购物车价格不会用产品的生成的销售价格进行更新.

Following Set only specific products sale price programmatically in WooCommerce 3, the related cart items price are not updated with the product's generated sale price.

如何在相关购物车项目中获取特定产品的生成的销售价格?

How can I get the generated sale price for specific products, in the related cart items?

感谢您的帮助.

推荐答案

要获取特定商品ID的购物车中正确的生成的销售价格,请尝试以下操作:

To get the right generated sale price in cart item for specific product IDs try the following:

// HERE below in the array set your specific product IDs
function specific_product_ids(){
    return array(37, 41); //  <===  <===  <===  <===  Your Product IDs
}

// Generating dynamically the product "regular price"
add_filter( 'woocommerce_product_get_regular_price', 'custom_dynamic_regular_price', 10, 2 );
add_filter( 'woocommerce_product_variation_get_regular_price', 'custom_dynamic_regular_price', 10, 2 );
function custom_dynamic_regular_price( $regular_price, $product ) {
    if( ( empty($regular_price) || $regular_price == 0 ) && in_array($product->get_id(), specific_product_ids() ) )
        return $product->get_price();
    else
        return $regular_price;
}


// Generating dynamically the product "sale price"
add_filter( 'woocommerce_product_get_sale_price', 'custom_dynamic_sale_price', 10, 2 );
add_filter( 'woocommerce_product_variation_get_sale_price', 'custom_dynamic_sale_price', 10, 2 );
function custom_dynamic_sale_price( $sale_price, $product ) {
    $rate = 0.8;
    if( ( empty($regular_price) || $regular_price == 0 ) && in_array($product->get_id(), specific_product_ids() ) )
        return $product->get_regular_price() * $rate;
    else
        return $sale_price;
};

// Displayed formatted regular price + sale price
add_filter( 'woocommerce_get_price_html', 'custom_dynamic_sale_price_html', 20, 2 );
function custom_dynamic_sale_price_html( $price_html, $product ) {
    if( $product->is_type('variable') ) return $price_html;

    if( in_array($product->get_id(), specific_product_ids() ) ) {
        $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;
}

// Set cart item generated "sale price" for specific product IDs
add_action( 'woocommerce_before_calculate_totals', 'set_cart_item_sale_price', 10, 1 );
function set_cart_item_sale_price( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // Iterate through each cart item
    foreach( $cart->get_cart() as $cart_item ) {
        if( in_array($cart_item['data']->get_id(), specific_product_ids() ) ) {
            $price = $cart_item['data']->get_sale_price(); // get sale price
            $cart_item['data']->set_price( $price ); // Set the sale price
        }
    }
}

代码进入您的活动子主题(活动主题)的function.php文件中.经过测试,可以正常工作.

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

这篇关于设置购物车商品仅在Woocommerce中为特定商品生成的销售价格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆