Wordpress / Woocommerce-在两个表单/页面之间传递数据

时间:2018-11-11 21:36:52

标签: php wordpress woocommerce

我自己的代码有问题。它的主要目标是过滤我的woocommerce商店的产品。这样的样子:

function flt_product_search_func() {
?>

<form role="search" method="get" class="woocommerce-product-search" name="b2bform" id="b2bform" action="<?php echo esc_url( home_url( '/' ) ); ?>">
<div class="container">
    <div class="row">
        <div class="col-lg-3">
            <p class="search-field-name">Nazwa</p>
            <input type="search" id="woocommerce-product-search-field-1" class="search-field" value="<?php echo get_search_query(); ?>" name="s" />         
        </div>
        <div class="col-lg-3">
            <p class="search-field-name">Marka</p>

      <?php
      $pabase = "pa_marka";
      $subheadingvalues = get_terms($pabase, array(
        'hide_empty' => false,
        ));
        ?> <select name="<?php echo $pabase; ?>">
            <option label=" " selected disabled value></option>
            <?php foreach ($subheadingvalues as $subheadingvalue): ?>
            <option value="<?php echo $subheadingvalue->name; ?>">
                <?php echo $subheadingvalue->name; ?>
            </option>
            <?php endforeach;?>
        </select>      

        </div>
        <div class="col-lg-3">
            <p class="search-field-name">Typ</p>

      <?php
      $pabase = "pa_typ-wozka";
      $subheadingvalues = get_terms($pabase, array(
        'hide_empty' => false,
        ));
        ?> <select name="<?php echo $pabase; ?>">
            <option label=" " selected disabled value></option>
            <?php foreach ($subheadingvalues as $subheadingvalue): ?>
            <option value="<?php echo $subheadingvalue->name; ?>">
                <?php echo $subheadingvalue->name; ?>
            </option>
            <?php endforeach;?>
        </select>   

        </div>
        <div class="col-lg-3">
            <p class="search-field-name">Udźwig od</p>
      <?php
      $pabase = "pa_udzwig";
      $subheadingvalues = get_terms($pabase, array(
        'hide_empty' => false,
        ));
        ?> <select name="rng_min_pa_udzwig">
            <option label=" " selected disabled value></option>
            <?php foreach ($subheadingvalues as $subheadingvalue): ?>
            <option value="<?php echo $subheadingvalue->name; ?>">
                <?php echo $subheadingvalue->name; ?>
            </option>
            <?php endforeach;?>
        </select>         
        </div>
    </div>
    <div class="row">
        <div class="col-lg-3">
            <p class="search-field-name">Udźwig do</p>
      <?php
      $pabase = "pa_udzwig";
      $subheadingvalues = get_terms($pabase, array(
        'hide_empty' => false,
        ));
        ?> <select name="rng_max_pa_udzwig">
            <option label=" " selected disabled value></option>
            <?php foreach ($subheadingvalues as $subheadingvalue): ?>
            <option value="<?php echo $subheadingvalue->name; ?>">
                <?php echo $subheadingvalue->name; ?>
            </option>
            <?php endforeach;?>
        </select>                   
        </div>
        <div class="col-lg-3">
            <p class="search-field-name">Napęd</p>
      <?php
      $pabase = "pa_naped";
      $subheadingvalues = get_terms($pabase, array(
        'hide_empty' => false,
        ));
        ?> <select name="<?php echo $pabase; ?>">
            <option label=" " selected disabled value></option>
            <?php foreach ($subheadingvalues as $subheadingvalue): ?>
            <option value="<?php echo $subheadingvalue->name; ?>">
                <?php echo $subheadingvalue->name; ?>
            </option>
            <?php endforeach;?>
        </select>   
        </div>
        <div class="col-lg-3">
            <p class="search-field-name">Podnoszenie od</p>
      <?php
      $pabase = "pa_podnoszenie";
      $subheadingvalues = get_terms($pabase, array(
        'hide_empty' => false,
        ));
        ?> <select name="rng_min_pa_podnoszenie">
            <option label=" " selected disabled value></option>
            <?php foreach ($subheadingvalues as $subheadingvalue): ?>
            <option value="<?php echo $subheadingvalue->name; ?>">
                <?php echo $subheadingvalue->name; ?>
            </option>
            <?php endforeach;?>
        </select>        
        </div>
        <div class="col-lg-3">
            <p class="search-field-name">Podnoszenie do</p>
      <?php
      $pabase = "pa_podnoszenie";
      $subheadingvalues = get_terms($pabase, array(
        'hide_empty' => false,
        ));
        ?> <select name="rng_max_pa_podnoszenie">
            <option label=" " selected disabled value></option>
            <?php foreach ($subheadingvalues as $subheadingvalue): ?>
            <option value="<?php echo $subheadingvalue->name; ?>">
                <?php echo $subheadingvalue->name; ?>
            </option>
            <?php endforeach;?>
        </select>       
        </div>
    </div>
    <div class="row">
        <div class="col-lg-3">
            <p class="search-field-name">Rok produkcji od</p>
      <?php
      $pabase = "pa_rok-produkcji";
      $subheadingvalues = get_terms($pabase, array(
        'hide_empty' => false,
        ));
        ?> <select name="rng_min_pa_rok-produkcji">
            <option label=" " selected disabled value></option>
            <?php foreach ($subheadingvalues as $subheadingvalue): ?>
            <option value="<?php echo $subheadingvalue->name; ?>">
                <?php echo $subheadingvalue->name; ?>
            </option>
            <?php endforeach;?>
        </select>       
        </div>
        <div class="col-lg-3">
            <p class="search-field-name">Rok produkcji do</p>
      <?php
      $pabase = "pa_rok-produkcji";
      $subheadingvalues = get_terms($pabase, array(
        'hide_empty' => false,
        ));
        ?> <select name="rng_max_pa_rok-produkcji">
            <option label=" " selected disabled value></option>
            <?php foreach ($subheadingvalues as $subheadingvalue): ?>
            <option value="<?php echo $subheadingvalue->name; ?>">
                <?php echo $subheadingvalue->name; ?>
            </option>
            <?php endforeach;?>
        </select> 
        </div>
        <div class="col-lg-3">
            <p class="search-field-name">Cena od</p>
      <?php
      $pabase = "rng_min_price";
      $subheadingvalues = get_terms($pabase, array(
        'hide_empty' => false,
        ));
        ?> 
            <input type="text" id="<?php echo $pabase; ?>" class="<?php echo $pabase; ?>" value="" name="<?php echo $pabase; ?>" />

        </div>
        <div class="col-lg-3">
            <p class="search-field-name">Cena do</p>
      <?php
      $pabase = "rng_max_price";
      $subheadingvalues = get_terms($pabase, array(
        'hide_empty' => false,
        ));
        ?>
        <input type="text" id="<?php echo $pabase; ?>" class="<?php echo $pabase; ?>" value="" name="<?php echo $pabase; ?>" />
        </div>
    </div>
    <div class="row">
        <div class="col-lg-3">
            <p class="search-field-name">Maszt</p>
      <?php
      $pabase = "pa_maszt";
      $subheadingvalues = get_terms($pabase, array(
        'hide_empty' => false,
        ));
        ?> <select name="<?php echo $pabase; ?>">
            <option label=" " selected disabled value></option>
            <?php foreach ($subheadingvalues as $subheadingvalue): ?>
            <option value="<?php echo $subheadingvalue->name; ?>">
                <?php echo $subheadingvalue->name; ?>
            </option>
            <?php endforeach;?>
        </select>       
        </div>
        <div class="col-lg-3">
            <p class="search-field-name">Motogodziny od</p>
      <?php
      $pabase = "pa_motogodziny";
      $subheadingvalues = get_terms($pabase, array(
        'hide_empty' => false,
        ));
        ?> <select name="rng_min_pa_motogodziny">
            <option label=" " selected disabled value></option>
            <?php foreach ($subheadingvalues as $subheadingvalue): ?>
            <option value="<?php echo $subheadingvalue->name; ?>">
                <?php echo $subheadingvalue->name; ?>
            </option>
            <?php endforeach;?>
        </select>                   
        </div>
        <div class="col-lg-3">
            <p class="search-field-name">Motogodziny do</p>
            <?php
      $pabase = "pa_motogodziny";
      $subheadingvalues = get_terms($pabase, array(
        'hide_empty' => false,
        ));
        ?> <select name="rng_max_pa_motogodziny">
            <option label=" " selected disabled value></option>
            <?php foreach ($subheadingvalues as $subheadingvalue): ?>
            <option value="<?php echo $subheadingvalue->name; ?>">
                <?php echo $subheadingvalue->name; ?>
            </option>
            <?php endforeach;?>
        </select>                   
        </div>
        <div class="col-lg-3">
            <p class="search-field-name">Kabina</p>
      <?php
      $pabase = "pa_kabina";
      $subheadingvalues = get_terms($pabase, array(
        'hide_empty' => false,
        ));
        ?> <select name="<?php echo $pabase; ?>">
            <option label=" " selected disabled value></option>
            <?php foreach ($subheadingvalues as $subheadingvalue): ?>
            <option value="<?php echo $subheadingvalue->name; ?>">
                <?php echo $subheadingvalue->name; ?>
            </option>
            <?php endforeach;?>
        </select>                   
        </div>
    </div>

    <div class="row">
  <div class="col-lg-3">
            <p class="search-field-name">Przebieg od</p>
      <?php
      $pabase = "pa_przebieg";
      $subheadingvalues = get_terms($pabase, array(
        'hide_empty' => false,
        ));
        ?> <select name="rng_min_pa_przebieg">
            <option label=" " selected disabled value></option>
            <?php foreach ($subheadingvalues as $subheadingvalue): ?>
            <option value="<?php echo $subheadingvalue->name; ?>">
                <?php echo $subheadingvalue->name; ?>
            </option>
            <?php endforeach;?>
        </select>                   
        </div>
        <div class="col-lg-3">
            <p class="search-field-name">Przebieg do</p>
            <?php
      $pabase = "pa_przebieg";
      $subheadingvalues = get_terms($pabase, array(
        'hide_empty' => false,
        ));
        ?> <select name="rng_max_pa_przebieg">
            <option label=" " selected disabled value></option>
            <?php foreach ($subheadingvalues as $subheadingvalue): ?>
            <option value="<?php echo $subheadingvalue->name; ?>">
                <?php echo $subheadingvalue->name; ?>
            </option>
            <?php endforeach;?>
        </select>                   
        </div>
    <div class="col-lg-3"></div>
    <div class="col-lg-3"></div>
  </div>

    <div class="row">
        <div class="col-lg-12">
           <button type="submit" value="<?php echo esc_attr_x( 'Search', 'submit button', 'woocommerce' ); ?>" class="takeright btnorange"><i class="fas fa-search"></i> <?php echo esc_html_x( 'Search', 'submit button', 'woocommerce' ); ?></button>
            <button type="reset" value="<?php echo esc_attr_x( 'Wyczyść', 'reset button', 'woocommerce' ); ?>" id="resetbtn" class="takeright marginright1em btnorange" onClick="this.form.reset()"><?php echo esc_html_x( 'Wyczyść', 'reset button', 'woocommerce' ); ?></button>
            <input type="hidden" name="post_type" value="product" />            
        </div>        
    </div>  
</div>
</form>

<?php
}
add_shortcode( 'flt_product_search', 'flt_product_search_func' );

我很确定代码可以被优化,但是仍然很不错。我在首页上使用了此短代码,当有人提交搜索时,它会显示正确的结果(即-list产品)。但是我已经解决了两个问题:

  • 如果我想在woocomerce页面(产品列表)上显示相同的表单(使用相同的简码)怎么办?它根本不起作用。通过不起作用,它不搜索产品
  • 如果我要将选定的值(在下拉列表中)从首页传递到搜索页(woocommerce产品列表),该怎么办?我不知道该怎么实现

也许我可以利用if(isset($ _GET ['submit'])){?

感谢任何建议

0 个答案:

没有答案