在特定类别上显示并由管理员管理



我想使计算器应该显示在特定的类别上。我添加了一个代码,如果显示标志打开,则显示如果没有,则隐藏。基本上,基于产品的隐藏显示属于哪个类别。例如汽车然后计算富尔,如果水果那么不应该显示。

/*
 * Display input on single product page
 * @return html
 */
function wallc_custom_option(){
    global $post;

    // when its display
    if($display_flag){
    // Logic of calling calculator
    $wall_width_value = isset( $_POST['wall_width_value'] ) ? sanitize_text_field( $_POST['wall_width_value'] ) : '';
    $wall_height_value = isset( $_POST['wall_height_value'] ) ? sanitize_text_field( $_POST['wall_height_value'] ) : '';
    printf( '<div class="rollcalculator">');
   ?>
   <div class="form-group"><label class="control-label" >Wall width</label>
    <input  class="form-control control-label" id="wall_width_value" name="wall_width_value"  />
    </div>
    <div class="form-group">
        <label class="control-label" >Wall height</label>
        <input  class="form-control control-label"  id="wall_height_value" name="wall_height_value" />
    </div>
    <div class="form-group"> <p><button id="estimateRole" class="btn btn-info btn-block" onclick="myFunction()">Estimate number of rolls</button></p></div>
<?php
    global $product;
    $dimensions = $product->get_dimensions();
    if ( ! empty( $dimensions ) ) {
        // all in cm
    $height= $product->get_height();
    $width=$product->get_width();
    $length=$product->get_length();
    //  CONVERT TO METER
    $pattern_height= $height/100;
    $pattern_width= $width/100;
    $pattern_repeat= $length/100;

    echo '<input type="hidden" id="pattern_height" value="'.$pattern_height.'" />';
    echo '<input type="hidden" id="pattern_width" value="'.$pattern_width.'"/>';
    echo '<input type="hidden" id="pattern_repeat" value="'.$pattern_repeat.'"/>';
    echo '<div class="form-group"><label class="control-label" >Roll Required</label><label class="form-control" id="totalRole"></label></div>';

    //correct
   // $WALL_AREA = ceil(($wall_width_value+$pattern_height) * $wall_height_value);    
?>
    <script type="text/javascript">
        function myFunction(){
          }
    </script>
    <?php 

    }
}

}
add_action( 'woocommerce_after_single_product_summary', 'wallc_custom_option', 9 );

您必须获取术语并获取详细信息,如果找到类别,则更改显示标志,然后断开循环并执行其余代码。

   /*
 * Display input on single product page
 * @return html
 */
function wallc_custom_option(){
    global $post;
    $terms = wp_get_post_terms( $post->ID, 'product_cat' );
    $display_flag=false;
    foreach ( $terms as $term ) 
    {       
            $wh_meta_checkbox = get_term_meta($term->term_id, 'wh_meta_checkbox', true);
            if($wh_meta_checkbox == 'on'){
                $display_flag=true;
                break;
            }
    }
    // when its display
    if($display_flag){
    // Logic of calling calculator
    $wall_width_value = isset( $_POST['wall_width_value'] ) ? sanitize_text_field( $_POST['wall_width_value'] ) : '';
    $wall_height_value = isset( $_POST['wall_height_value'] ) ? sanitize_text_field( $_POST['wall_height_value'] ) : '';
    printf( '<div class="rollcalculator">');
   ?>
   <div class="form-group"><label class="control-label" >Wall width</label>
    <input  class="form-control control-label" id="wall_width_value" name="wall_width_value"  />
    </div>
    <div class="form-group">
        <label class="control-label" >Wall height</label>
        <input  class="form-control control-label"  id="wall_height_value" name="wall_height_value" />
    </div>
    <div class="form-group"> <p><button id="estimateRole" class="btn btn-info btn-block" onclick="myFunction()">Estimate number of rolls</button></p></div>
<?php
    global $product;
    $dimensions = $product->get_dimensions();
    if ( ! empty( $dimensions ) ) {
        // all in cm
    $height= $product->get_height();
    $width=$product->get_width();
    $length=$product->get_length();
    //  CONVERT TO METER
    $pattern_height= $height/100;
    $pattern_width= $width/100;
    $pattern_repeat= $length/100;

    echo '<input type="hidden" id="pattern_height" value="'.$pattern_height.'" />';
    echo '<input type="hidden" id="pattern_width" value="'.$pattern_width.'"/>';
    echo '<input type="hidden" id="pattern_repeat" value="'.$pattern_repeat.'"/>';
    echo '<div class="form-group"><label class="control-label" >Roll Required</label><label class="form-control" id="totalRole"></label></div>';

    //correct
   // $WALL_AREA = ceil(($wall_width_value+$pattern_height) * $wall_height_value);    
?>
    <script type="text/javascript">
        function myFunction(){
          // wall input
          wall_width_value=Number($("#wall_width_value").val());
          wall_height_value=Number($("#wall_height_value").val());
          // Product dimensions
          pattern_height=Number($("#pattern_height").val());
          pattern_width=Number($("#pattern_width").val());
          pattern_repeat=Number($("#pattern_repeat").val());
          WALL_AREA = Math.ceil((wall_width_value+pattern_height) * wall_height_value);  
          wall_width_value_meter=wall_width_value/100;
         pattern_width_meter=pattern_width;
          PATTERN_AREA = (pattern_width * pattern_repeat) ;
          totalRole=Math.ceil(WALL_AREA/PATTERN_AREA);
            $("#totalRole").text(totalRole);
        }
    </script>
    <?php 

    }
}

}
add_action( 'woocommerce_after_single_product_summary', 'wallc_custom_option', 9 );

最新更新