WooCommerce 中所有产品的临时全球销售价格折扣

问题描述 投票:0回答:2

我们正计划发起一项活动。我们希望在接下来的 30 天内对我们网站上销售的每件产品给予临时 10% 的折扣。理想情况下,我们希望同时显示正常价格和折扣价格。

  • 我们的商店建立在 WooCommerce 上。
  • 我们的产品没有变化。
  • 我们大约有200,000种产品。
  • 大多数工具都难以处理如此多的产品。

如何做到这一点?

php wordpress woocommerce product discount
2个回答
1
投票

更新(允许产品零价并重置现有销售价格)

您可以使用以下内容添加临时全球折扣,而不是按照您在评论中建议的方式覆盖产品价格,从而使原始产品价格与一般销售价格保持 10% 的折扣:

// Below define the discount rate
function discount_rate(){
    return 0.9; // 10% discount
}

add_filter('woocommerce_product_get_price', 'custom_price_discount', 20, 2);
add_filter('woocommerce_product_variation_get_price', 'custom_price_discount', 20, 2);
function custom_price_discount( $price, $product ) {
    if ( $price > 0 ) {
        return floatval($product->get_regular_price()) * discount_rate();
    }
    return $price;
}

add_filter('woocommerce_product_get_sale_price', 'custom_sale_price_discount', 20, 2);
add_filter('woocommerce_product_variation_get_sale_price', 'custom_sale_price_discount', 20, 2); 
function custom_sale_price_discount( $price, $product ) {
    return $product->get_price();
}
  
add_filter('woocommerce_product_is_on_sale', 'custom_product_is_on_sale', 20, 2);
function custom_product_is_on_sale( $is_on_sale, $product ) {
    return $product->get_price() > 0 ? true : false;
}

function remove_zero_prices( $prices ) {
    foreach( $prices as $key => $price ) {
        if ( $price > 0 ) {
            continue;
        }
        unset($prices[$key]);
    }
    return $prices;
}

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') ) {
        $prices     = $product->get_variation_prices( true );
        $reg_prices = remove_zero_prices( $prices['regular_price'] );

        if ( count($reg_prices) > 0 ) {
            return wc_format_sale_price( wc_price( end($reg_prices) ), wc_price( reset($reg_prices) * discount_rate() ) ) . $product->get_price_suffix();
        } 
    }
    return $price_html;
}

代码位于子主题的functions.php 文件中(或插件中)。经过测试并有效。它可以与您非常大的目录一起使用。


1
投票

我是如何做到的:

  • 通过使用
    _sale_price
    的数据乘以
    _price
    (10%) 来更新其
    _regular_price
    0.9
    ,从而对所有产品进行销售。
  • 促销结束后,通过执行 SQL 查询手动删除
    _sale_price
    并用
    _price
    更新
    _regular_price

警告:此解决方案不适用于变体!

为所有产品添加折扣

如果产品没有促销,则数据库表中没有

_sale_price
meta_key
wp_postmeta
。要添加这些,我们可以循环所有产品并使用
update_post_meta
。当使用较大的数据库时,我建议将更新分成较小的批次。我在下面的代码中每批使用了
1000
。如果您想一次查询所有产品,请将
'posts_per_page' => 1000
替换为
'posts_per_page' => -1

警告:如果您的任何产品已经有

_sale_price

,此代码将无法正常工作
// Get the products that don't have '_sale_price'
$args = array(
    'post_type'      => 'product',
    'posts_per_page' => 1000,
    'meta_query'     => array(
        'relation' => 'AND',
        array(
            'key'     => '_sale_price',
            'compare' => 'NOT EXISTS',
        ),
        array(
            'key'     => '_regular_price',
            'value'   => '',
            'compare' => '!='
        ),
    ),
);
$products = query_posts( $args );

// Loop through queried products
foreach ($products as $post) {
    $product = wc_get_product( $post->ID );

    // Discount by 10% and round to 2 decimal places
    $discount = 10;
    $newprice = round($product->get_regular_price() * ((100-$discount) / 100), 2);

    // Update product's prices
    update_post_meta( $product->get_id(), '_sale_price', $newprice );
    update_post_meta( $product->get_id(), '_price', $newprice );
}

取消折扣

我使用 SQL 查询,因为它快速且简单。如果您正在寻找替代方法,请查看此帖子:从 WooCommerce 管理中的所有产品中删除促销价格

警告:这些 SQL 查询将删除您所有现有的销售价格!

删除所有产品

_sale_price
:

DELETE FROM `wp_postmeta1` WHERE meta_key = '_sale_price';

使用

_price
的值更新产品的
_regular_price

UPDATE wp_postmeta as price 
    INNER JOIN wp_postmeta as regular_price ON 
        price.post_id = regular_price.post_id AND 
        regular_price.meta_key = "_regular_price" AND
        price.meta_key = "_price"
    SET price.meta_value = regular_price.meta_value;

参考资料:

© www.soinside.com 2019 - 2024. All rights reserved.