【发布时间】:2021-10-14 18:29:34
【问题描述】:
我正在为 WooCommerce 中的单个产品制定反向“销售价格”,如“Future Price”。
总体思路是设置价格(未来价格)和开始+结束日期。
然后,使用woocommerce_product_get_price 过滤器,检查今天是否与“未来价格开始日期”的值相同,如果相同,则更改产品价格。
当未来价格结束日期过去后,将产品价格恢复为正常价格。
问题是:我没有收到任何错误或通知或类似的东西——这意味着我迷路了:)
为了完成这一切,我创建了一个自定义字段(未来价格)。我还创建了日期字段。它们都已保存和更新。
如果有人可以提供帮助并给我提示/指点,我将不胜感激。这是我一周内到期的第二个(3 个中的 2 个)学校项目。
这是我的完整代码:
add_filter( 'woocommerce_product_get_price', 'future_product_price', 20, 2 );
function future_product_price( $price, $product ) {
if ( is_admin() ) return $price;
$future_price = get_post_meta( $product->get_id(), 'future_price', true ); // future product price
$future_start_date = get_post_meta( $product->get_id(), '_future_price_date_from', true ); // future product price start date
$future_end_date = get_post_meta( $product->get_id(), '_future_price_date_to', true ); // future product price end date
if ( ! empty( $future_price ) && ! empty( $future_start_date ) && ! empty( $future_end_date ) ) {
$future_date_time = (int) strtotime( $future_start_date ); // convert
$now_time = (int) strtotime("now"); // now in seconds
// check if right now is the same day as the future price start date
if ( $now_time == $future_start_date )
// if they are the same, set the product price to the future price
$price = $future_price;
}
return $price;
}
【问题讨论】:
标签: javascript jquery wordpress woocommerce