我想做的事情其实很简单,但我显然不是很棒,而且对PHP非常陌生。我正在使用woocommerce作为我的商店和一个名为"woocommerce调度器“的woocommerce添加设置一个开始和结束时间特定的产品出售每天。
调度程序的工作方式是用24小时时钟设置开始和结束时间,例如上午10点(10:00)到晚上8点(20:00)。产品可以在上午8点到晚上10点之后的任何时间购买。插件“隐藏”“添加到购物车”按钮和显示自定义消息,如“当前不可用”,您无法购买该项目。
我遇到了两个问题,其中一个问题解决了。
因此,这个问题的解决方案是一个名为高级自定义字段的插件,并创建一个可以设置在产品页面上的自定义字段,然后我可以通过将这个php代码放在另一个产品页面上来显示,如果feild在另一个产品页面上为空,则不会将<?php if( get_field('product_time') ): ?> <p>Available at <?php the_field('product_time'); ?></p> <?php endif; ?>显示到我的单个产品页面模板文件中。
我把它做得很好。现在是我无法解决的第二个问题。
我找到了一些代码来提取不可用的消息,但它是一个js。文件,所以我不能将我拥有的php放到那个文件中。
wdm_validate.js
jQuery(document).ready(function(){
jQuery('.cart').html('<p class = "wdm_message3">'+wdm_message.wdm_expiration_message+' </p>');
});
这是调用js文件的调度程序插件的php文件中的位。
woocommerce_scheduler.php
if($wdm_start_date <= $d && $d<=$wdm_end_date && $str_start_time <=
$curtime && $curtime <= $str_end_time) {
}else{
//echo "<style>.cart{display:none;}</style>";
//echo "hello";
//exit;
wp_enqueue_script('wdm_expiration_message',plugins_url('js/wdm_validate.js',__FILE__),
array('jquery'));
$data = array(
'wdm_expiration_message' => get_option( 'woocommerce_custom_product_expiration',true )
);
wp_localize_script('wdm_expiration_message','wdm_message',$data);
}发布于 2015-08-07 08:27:03
在您的自定义代码中,只需向<p>标记添加类并执行以下操作:
<?php if( get_field('product_time') ): ?>
<p class='wdm_available'>Available at <?php the_field('product_time'); ?></p>
<script>
jQuery(document).ready(function(){
//checking wdm_message3 i.e. expiration message exist or not
if(!jQuery('.wdm_message3').is(':visible')){
jQuery('.wdm_available').hide();
}
});
</script>
<?php endif; ?>发布于 2015-08-07 10:35:26
在woocommerce中将这些代码添加到我的price.php模板页面中,这正是我所需要的。时间是隐藏的,当产品是可用的,并显示什么时候该项目是不可出售的,让我的客户知道什么时候可以购买它。
<?php if( get_field('product_time') ): ?>
<p class='wdm_available'>Unavailable Until <?php the_field('product_time'); ?></p>
<script>
jQuery(document).ready(function(){
// set a timeout function to get around brief delay in scheduler script
setTimeout(function(){
//check if scheduler message is NOT visible
if(!jQuery('.wdm_message3').is(':visible')) {
// if not, hide custom availabilty time message
jQuery('.wdm_available').hide();
}
}, 0);
});
</script>
<?php endif; ?>发布于 2015-08-07 15:45:28
我不可能知道您的自定义字段的格式,但是您应该能够与当前时间进行比较。作为一种想法:
$current_time = time();
$sale_time = get_field('product_time');
if( $sale_time && $sale_time < $current_time ): ?>
<p class='wdm_available'>Unavailable Until <?php the_field('product_time'); ?></p>
<?php endif; ?>https://stackoverflow.com/questions/31868607
复制相似问题