【发布时间】:2016-11-19 02:27:01
【问题描述】:
我有一个显示分类产品的 Wordpress/WooCommerce 网站。我正在尝试在管理单个类别页面(后端)中创建一个自定义字段,我可以在其中设置一个 start 和 end time分类产品购买可用性。
这应该如何工作:
客户只能购买类别为“开放”的产品(在设置为此类别的开始和结束时间之间),但所有产品即使不可购买也仍然可见。
我想知道是否有人知道开始和实现它的最佳方式。
目前这里是我拥有的相关代码:
function custom_product_taxonomy_add_new_meta_field() { //add term page
//this will add the custom meta field to the add new term page
?>
<div class="form-field">
<label for="term_meta[custom_term_meta]"><?php _e( 'Post Code(s)' ); ?></label>
<input type="text" name="term_meta[custom_term_meta]" id="term_meta[custom_term_meta]" value="">
<p class="description"><?php _e( 'Enter the delivery post codes available for this store.','my-text-domain' ); ?></p>
</div>
<?php
}
add_action( 'product_cat_add_form_fields', 'custom_product_taxonomy_add_new_meta_field', 10, 2 );
//edit term page
function custom_product_taxonomy_edit_meta_field($term) {
//put the term ID into a variable
$t_id = $term->term_id;
//retrieve the existing value(s) for this meta field. This returns an array
$term_meta = get_option( "taxonomy_$t_id" ); ?>
<tr class="form-field">
<th scope="row" valign="top"><label for="term_meta[custom_term_meta]"><?php _e( 'Post Code(s)' ); ?></label></th>
<td>
<input type="text" name="term_meta[custom_term_meta]" id="term_meta[custom_term_meta]" value="<?php echo esc_attr( $term_meta['custom_term_meta'] ) ? esc_attr( $term_meta['custom_term_meta'] ) : ''; ?>">
<p class="description"><?php _e( 'Enter the delivery post codes available for this store.' ); ?></p>
</td>
</tr>
<?php
}
add_action( 'product_cat_edit_form_fields', 'custom_product_taxonomy_edit_meta_field', 10, 2 );
//save extra taxonomy fields callback function.
function save_taxonomy_custom_meta( $term_id ) {
if ( isset( $_POST['term_meta'] ) ) {
$t_id = $term_id;
$term_meta = get_option( "taxonomy_$t_id" );
$cat_keys = array_keys( $_POST['term_meta'] );
foreach ( $cat_keys as $key ) {
if ( isset ( $_POST['term_meta'][$key] ) ) {
$term_meta[$key] = $_POST['term_meta'][$key];
}
}
update_option( "taxonomy_$t_id", $term_meta ); //save the option array.
}
}
add_action( 'edited_product_cat', 'save_taxonomy_custom_meta', 10, 2 );
add_action( 'create_product_cat', 'save_taxonomy_custom_meta', 10, 2 );
此代码在管理类别页面中添加了一个自定义字段,但就像我之前所说的,我正在寻找一个可以设置开始和结束时间的自定义字段。
欢迎提出任何建议,因为我已经为此工作了数周,只是在寻找解决方案。
提前致谢。
【问题讨论】:
标签: php wordpress woocommerce categories advanced-custom-fields