php – Woocommerce从外部来源更新价格
发布时间:2020-12-13 22:28:12 所属栏目:PHP教程 来源:网络整理
导读:我有从外部数据库更新产品价格的问题.我需要在每次访问此位置时检查产品的价格.为此,我使用the_post钩子.例如,单品的价格为’1718′. function chd_the_post_action( $post ) { if ( $post $post-post_type == 'product' ) { $product = wc_get_product( $po
我有从外部数据库更新产品价格的问题.我需要在每次访问此位置时检查产品的价格.为此,我使用the_post钩子.例如,单品的价格为’1718′.
function chd_the_post_action( $post ) { if ( $post && $post->post_type == 'product' ) { $product = wc_get_product( $post->ID ); if ( $product ) { $price = '1718'; $product->set_price( "$price" ); $product->set_regular_price( "$price" ); $product->set_sale_price( "$price" ); $product->save(); } } } 此代码更新了数据库中的产品价格,但它不会在同一时刻更改页面上的价格视图,而是仅在页面重新加载后,因为post和product变量是由setup_postdata()设置的. function chd_get_price_filter( $price,$item ) { return '1718'; } add_filter( 'woocommerce_product_get_price','chd_get_price_filter',100,2 ); add_filter( 'woocommerce_product_get_regular_price',2 ); add_filter( 'woocommerce_product_get_sale_price',2 ); 有没有可以用更好的方式做这个动作的钩子? 解决方法
使用update_post_meta函数更新产品价格
update_post_meta( $product->id,'_sale_price','1718' ); update_post_meta( $product->id,'_regular_price','1718 ); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |