php - WooCommerce 产品类别自定义字段 - 设置时间购买限制

标签 php wordpress woocommerce categories advanced-custom-fields

我有一个显示分类产品的 Wordpress/WooCommerce 网站。我正在尝试在管理各个类别页面(后端)中创建一个自定义字段,我可以在其中设置与以下内容相关的开始结束时间分类产品购买可用性。

这应该如何工作:
客户只能购买类别为“开放”的产品(在为此类别设置的开始时间和结束时间之间),但所有产品即使不可购买,仍然可见。

我想知道是否有人知道开始和实现这一目标的最佳方法。

目前这是我拥有的相关代码:

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 );

此代码在管理类别页面中添加了一个自定义字段,但正如我之前所说,我正在寻找一个可以设置开始和结束时间的自定义字段。

欢迎提出任何建议,因为我已经研究这个问题数周了,只是在寻找解决方案。

提前致谢。

最佳答案

考虑到产品类别的时间可用性,我选择添加 3 个自定义选择器:

  • 用于启用功能,
  • 一个表示开始时间
  • 一个持续时间(期间):

enter image description here

在下面的代码片段中,我们创建了 3 个可编辑的自定义字段(代码非常紧凑,因为我使用 for 循环来生成小时选择器选项,而不是显示 24 小时选项...):

add_action( 'product_cat_edit_form_fields', 'availiability_product_taxonomy_edit_meta_field', 10, 2 );
add_action( 'product_cat_add_form_fields', 'availiability_product_taxonomy_edit_meta_field', 10, 2 );
function availiability_product_taxonomy_edit_meta_field($term) {
    //put the term ID into a variable
    $term_id = $term->term_id;
    //retrieve the start and ends values
    $enable_time = get_term_meta( $term_id, 'enable_time', true ) == '1' ? '1' : '0';
    $start_time = get_term_meta( $term_id, 'start_time', true );
    $lenght_time = get_term_meta( $term_id, 'lenght_time', true );
    ?>

    <tr class="form-field">
        <th scope="row" valign="top">
            <label for="time-availiability"><?php _e( 'Time availiability' ,'my-theme-slug' ); ?></label>
        </th>
        <td>
            <span style="padding-right:8px">
                <label style="padding-right:8px" for="enable_time"><b><?php _e( 'Hours: ' ,'my-theme-slug' ); ?></b></label>
                <select name="enable_time" id="enable_time" class="postform">
                    <option <?php if ( $enable_time == '0') echo 'selected'; ?> value='0'><?php _e( 'Disabled' ,'my-theme-slug' ); ?></option>
                    <option <?php if ( $enable_time == '1') echo 'selected'; ?> value='1'><?php _e( 'Enabled' ,'my-theme-slug' ); ?></option>
                </select>
            </span>
            <span style="padding-right:10px">
                <label for="start_time"><?php _e( "Start" ,"my-theme-slug" ); ?></label>
                <select name="start_time" id="start_time" class="postform">

            <?php // Genereting the 24 options tags
                for ($i = 0; $i < 24; $i++) {
                    $j = $i < 10 ? '0'.$i : ''.$i ; // values with on 2 digits
                    $selected = $start_time == $j ? "selected" : "";
            ?>
                    <option <?php echo $selected; ?> value="<?php echo $j; ?>"><?php echo $j; ?></option>
            <?php } ?>

                </select>
            </span>
            <span>
                <label for="lenght_time"><?php _e( "Length" ,"my-theme-slug" ); ?></label>
                <select name="lenght_time" id="lenght_time" class="postform">

            <?php // Genereting the 24 options tags
                for ($i = 0; $i < 25; $i++) {
                    $j = $i < 10 ? '0'.$i : ''.$i ; // values with on 2 digits
                    $selected = $lenght_time == $j ? "selected" : "";
            ?>
                    <option <?php echo $selected; ?> value="<?php echo $j; ?>"><?php echo $j; ?></option>
            <?php } ?>

                </select>
            </span>
        </td>
    </tr>

    <?php
}

然后在下面的代码片段中,我们在提交时保存自定义字段数据。

add_action( 'create_product_cat', 'availiability_taxonomy_custom_meta', 10, 2 );
add_action( 'edited_product_cat', 'availiability_taxonomy_custom_meta', 10, 2 );
function availiability_taxonomy_custom_meta( $term_id ) {
  if ( isset( $_POST['enable_time'] ) ) {
      update_term_meta ($term_id, 'enable_time', $_POST['enable_time'] );
  }
  if ( isset( $_POST['start_time'] ) ) {
      update_term_meta ($term_id, 'start_time', $_POST['start_time'] );
  }
  if ( isset( $_POST['lenght_time'] ) ) {
      update_term_meta ($term_id, 'lenght_time', $_POST['lenght_time'] );
  }
}

在您的数据库中,如果您查看 wp_termmeta 表,您将看到您的类别 ID (这里是 ID 11 测试时) 3 个自定义字段及其相应值:

enter image description here

This code is using more recent functions included since WordPress 4.4+ (Much better) than found usually on most other related threads: get_term_meta() and update_term_meta()

所有这些代码都经过测试并且功能齐全。它位于位于事件子主题或主题中的 function.php 文件中。

非常好的引用(使用新的核心功能):Add custom field to Category

关于php - WooCommerce 产品类别自定义字段 - 设置时间购买限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38403532/

相关文章:

php - Android - PHP if else 不会停止

php - 如何在 php 中发送 cookie 值的正确翻译

php - 在 ajax 添加到购物车上显示特定 Woocommerce 购物车商品数量的甜蜜警报

css - Wordpress 不遵守移动设备上的 css 规则

javascript - 在 Wordpress 主题中使用 AJAX 的 SQL 查询

Javascript 在控制台上工作,但不是 onclick 事件

mysql - 带有 WooCommerce 插件的 WordPress 数据库未显示所有数据

php - 更新 MySQL 中包含外键列的表

php - 从 mysql 查询特定的 JSON 键值

php - Laravel - 按 "a Field and Its Relationship Model Field"之间的条件过滤模型