首页 > 解决方案 > WooCommerce 批量产品价格更新

问题描述

我正在开发一个销售珠宝产品的门户网站。汇率计算取决于每日更新的黄金/白银市场汇率。例如,我有计算公式。产品费率=金属费率+制作费+税金

每日金属费率变化 我想要实现允许我通过更新金属费率来更新所有产品费率的功能。

是否有任何内置功能,插件可用?谢谢你。

标签: wordpresswoocommerce

解决方案


您可以在 function.php 中使用它

首先创建一个价格更新函数

<?php
function update_product_price(){
$args = array(
  'post_type' => 'product',
  'posts_per_page' => -1
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
  while ( $loop->have_posts() ) : $loop->the_post();
    global $product;
    //get metal rate,making charge and tax
    $product_rate = $metal_rate + $making_charge + $tax;
    //update product price
    update_post_meta($product->get_id(), '_regular_price', (float)$product_rate);
    update_post_meta($product->get_id(), '_price', (float)$product_rate);

  endwhile;
}
wp_reset_query();
}

现在您需要做的就是在 wordpress 中设置 cron 作业,您可以使用动作挂钩来完成

add_action( 'is_add_every_day', 'update_product_price' );

如果尚未安排行动,则安排行动

if ( ! wp_next_scheduled( 'is_add_every_day' ) ) {
    wp_schedule_event( time(), 'every_day', 'is_add_every_day' );
}

添加新的时间间隔

// See http://codex.wordpress.org/Plugin_API/Filter_Reference/cron_schedules
add_filter( 'cron_schedules', 'is_add_every_day' );
function isa_add_every_three_minutes( $schedules ) {
    $schedules['every_day'] = array(
            'interval'  => 24*60*60,
            'display'   => __( 'Every day', 'textdomain' )
    );
    return $schedules;
}

推荐阅读