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 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事: