dream8877 2017-11-25 12:23
浏览 31
已采纳

当取消选择时,为什么WooCommerce自定义复选框不会删除“附加”的PHP编码?

I am working on a WordPress eCommerce website, with the chosen shopping platform; WooCommerce.

I have created a Custom Checkbox, within the WooCommerce Product Dashboard, by placing the following code into the functions.php file:

function product_custom_fields_add(){

   global $post;

   $input_checkbox = get_post_meta( $post->ID, '_engrave_text_option', true );
   if( empty( $input_checkbox ) || $input_checkbox == 'no' ) $input_checkbox = '';

       echo '<div class="product_custom_field">';

    woocommerce_wp_checkbox(
        array(
            'id'        => '_engrave_text_option',
            'desc'      =>  __('set custom Engrave text field', 'woocommerce'),
            'label'     => __('Display custom Engrave text field', 'woocommerce'),
            'desc_tip'  => 'true',
            'value'     => $input_checkbox
        )
    );  

 echo '</div>';
}
add_action('woocommerce_product_options_advanced', 'product_custom_fields_add');

To save the Custom Field's values, I have inserted the following code into the functions.php file:

function woocommerce_product_custom_fields_save($post_id){               
    $_engrave_text_option = isset( $_POST['_engrave_text_option'] ) ? 'yes' : 'no';
    update_post_meta( $post_id, '_engrave_text_option', $_engrave_text_option );       
 }
add_action('woocommerce_process_product_meta', 'woocommerce_product_custom_fields_save');

The idea is that when a Site Admin selects the Checkbox, it triggers the below code, in order to create a Custom Text Box on the Product Page:

function add_engrave_text_field() {
    global $post;

    // Get the checkbox value
    $engrave_option = get_post_meta( $post->ID, '_engrave_text_option', true );

    // If is single product page and have the "engrave text option" enabled we display the field
    if ( is_product() && ! empty($engrave_option) ) {

        ?>
        <div>
            <label class="product-custom-text-label" for="engrave_text"><?php _e( 'Custom Letters:', 'woocommerce'); ?><br>
                <input style="min-width:220px" type="text" class="product-counter" name="engrave_text" placeholder="<?php _e( 'Enter Your Custom Letters ...', 'woocommerce'); ?>" minlength="<?php global $post; echo get_post_meta($post->ID,'_minimum_engrave_text_option',true);?>" maxlength="<?php global $post; echo get_post_meta($post->ID,'_maximum_engrave_text_option',true);?>" />
            </label>
        </div><br>
<?php
    }
}
add_action( 'woocommerce_before_add_to_cart_button', 'add_engrave_text_field', 0 );
?> 

The above code works when selecting the Checkbox, to create the Custom Text Field on the Product Page. Where the problem lies, is that when the Checkbox is deselected, the Custom Text Box remains on the Product Page.

Is anyone able to see where I am going wrong here?

  • 写回答

1条回答 默认 最新

  • doutao5419 2017-11-25 13:27
    关注

    Here is missing point:

    $_engrave_text_option = isset( $_POST['_engrave_text_option'] ) ? 'yes' : 'no';
    

    So your meta value never gets empty value. It gets yes or no. There are 2 solutions.

    1. Change "no" to "";

      $_engrave_text_option = isset( $_POST['_engrave_text_option'] ) ? 'yes' : '';

    2. Change empty to =='yes'

      if ( is_product() && $engrave_option=='yes' ) {

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何解决y_true和y_predict数据类型不匹配的问题(相关搜索:机器学习)
  • ¥15 PB中矩阵文本型数据的总计问题。
  • ¥40 宿舍管理系统设计(c#)
  • ¥15 MATLAB卫星二体模型仿真
  • ¥15 怎么让数码管亮的同时让led执行流水灯代码
  • ¥20 SAP HANA SQL Script 。如何判断字段值包含某个字符串
  • ¥85 cmd批处理参数如果含有双引号,该如何传入?
  • ¥15 fx2n系列plc的自控成型机模拟
  • ¥15 时间序列LSTM模型归回预测代码问题
  • ¥50 使用CUDA如何高效的做并行化处理,是否可以多个分段同时进行匹配计算处理?目前数据传输速度有些慢,如何提高速度,使用gdrcopy是否可行?请给出具体意见。