wordpress - WooCommerce 批量产品价格更新
问题描述
我正在开发一个销售珠宝产品的门户网站。汇率计算取决于每日更新的黄金/白银市场汇率。例如,我有计算公式。产品费率=金属费率+制作费+税金
每日金属费率变化 我想要实现允许我通过更新金属费率来更新所有产品费率的功能。
是否有任何内置功能,插件可用?谢谢你。
解决方案
您可以在 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;
}
推荐阅读
- ios - 向导航栏添加渐变将隐藏 iOS 13.0 + 中的栏按钮项目
- javascript - 将渲染反应到字符串
- javascript - Liveware.emit 在全局 javascript 中不起作用
- exception - 处理应用程序中的异常
- vaadin - Vaadin 14 主题 - 使用“包含”
- amazon-web-services - 等效于 AWS 的自定义脚本扩展
- javascript - 反应 - 更新(componentdidupdate)
- linux - 使用 CA 签名证书(不是自签名证书)在本地网络上启用 ssl
- angular - 订阅主题的返回值如何返回服务
- flutter - 如何将中心的 TabBarView 移动到页面顶部?