2015-06-22 116 views
3

现在我用下面的代码附加产品搜索栏

// Display Fields 
add_action('woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields'); 
function woo_add_custom_general_fields() { 


    global $woocommerce, $post; 


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


    // Custom fields will be created here... 

// Product Select 
?> 
<p class="form-field product_field_type"> 
<label for="product_field_type"><?php _e('Product Select', 'woocommerce'); ?></label> 
<select style="width:100%" id="product_field_type" name="product_field_type[]" class="ajax_chosen_select_products" multiple="multiple" data-placeholder="<?php _e('Search for a product&hellip;', 'woocommerce'); ?>"> 
    <?php 
     $product_field_type_ids = get_post_meta($post->ID, '_product_field_type_ids', true); 
     $product_ids = ! empty($product_field_type_ids) ? array_map('absint', $product_field_type_ids) : null; 
     if ($product_ids) { 
      foreach ($product_ids as $product_id) { 

       $product  = get_product($product_id); 
       $product_name = woocommerce_get_formatted_product_name($product); 

       echo '<option value="' . esc_attr($product_id) . '" selected="selected">' . esc_html($product_name) . '</option>'; 
      } 
     } 
    ?> 
</select> <img class="help_tip" data-tip='<?php _e('Your description here', 'woocommerce') ?>' src="<?php echo $woocommerce->plugin_url(); ?>/assets/images/help.png" height="16" width="16" /> 
</p> 
<?php 

    echo '</div>'; 

} 

,显示下面的产品领域,但不能正常工作

enter image description here

但我希望产品搜索如下图片

enter image description here

我使用下面的网站代码

http://www.remicorson.com/mastering-woocommerce-products-custom-fields/

+0

我不明白为什么你需要在那里的产品领域? – DevelopmentBucket

+0

我有一个自定义的帖子类型,即订购周期,我想在产品上添加订单周期ID,这样我就可以知道这个产品是否适用于特定的订单周期。我想这样做,就像我们选择交叉销售产品并向上销售,如屏幕截图2 –

+1

您可以显示自定义帖子类型即订购周期中的所有帖子,并且可以使用多个选择列表 – DevelopmentBucket

回答

-2

您正在使用woocommerce插件。要筛选价格范围,颜色,尺寸和其他字段的产品,您可以使用另一个woocommerce插件“woocommerce产品过滤器”。你可以使用yith插件来搜索产品。如果您想为整个网站提供搜索工作,而不仅仅是用于用户dave的实时搜索插件的产品。

Dave's live search plugin

Yith products search plugin

3

我试图最近找出我的一个客户端同样的问题。我最初使用相同的教程在后端为他添加一个自定义产品搜索输入字段,并且当我们将他更新到WooCommerce 2.5+时,它就会崩溃。

要将产品搜索小插件/字段添加到后端:

?> 

    <p class="form-field product_field_type"> 
    <label for="product_field_type_ids"><?php _e('Component Products:', 'woocommerce'); ?></label> 

     <input type="hidden" class="wc-product-search" style="width: 50%;" id="product_field_type_ids" name="product_field_type_ids" data-placeholder="<?php esc_attr_e('Search for a product&hellip;', 'woocommerce'); ?>" data-action="woocommerce_json_search_products" data-multiple="true" data-exclude="<?php echo intval($post->ID); ?>" data-selected="<?php 
         $product_ids = array_filter(array_map('absint', (array) get_post_meta($post->ID, '_product_field_type_ids', true))); 
         $json_ids = array(); 

         foreach ($product_ids as $product_id) { 
          $product = wc_get_product($product_id); 
          if (is_object($product)) { 
           $json_ids[ $product_id ] = wp_kses_post(html_entity_decode($product->get_formatted_name(), ENT_QUOTES, get_bloginfo('charset'))); 
          } 
         } 

         echo esc_attr(json_encode($json_ids)); 
        ?>" value="<?php echo implode(',', array_keys($json_ids)); ?>" /> <?php echo wc_help_tip(__('Select component parts to display on the product page.', 'woocommerce')); ?> 
    </p> 

    <?php 

拯救:(保存在同一个ID int数组格式与现有的领域,所以你不必从头开始与您现有的数据库条目)

$my_product_ids = isset($_POST['product_field_type_ids']) ? array_filter(array_map('intval', explode(',', $_POST['product_field_type_ids']))) : array(); 
    update_post_meta($post_id, '_product_field_type_ids', $my_product_ids ); 

要显示在前端:(同前)

global $post; 
$items = get_post_meta($post->ID, '_product_field_type_ids', true); 
if (! empty($items)) { 
     foreach ($items as $product_id) { 
      echo '<div class="productLink"><a href="'.get_permalink($product_id).'">'.get_the_title($product_id).'</a></div>'; 
     } 
    } 
+0

这似乎不适用于我再次在WC 3.0.8中。不知道如何解决它。 – FascistDonut

+0

您可以使用[我的回答](https://stackoverflow.com/a/48205855/1675394)中的信息使其与最新的WooCommerce版本一起使用。 –

1

我从追加销售复制了此代码。这只是搜索表单。

<p class="form-field"> 
     <label for="upsell_ids"><?php _e('Up-sells', 'woocommerce'); ?></label> 
     <select class="wc-product-search" multiple="multiple" style="width: 50%;" id="upsell_ids" name="upsell_ids[]" data-placeholder="<?php esc_attr_e('Search for a product&hellip;', 'woocommerce'); ?>" data-action="woocommerce_json_search_products_and_variations" data-exclude="<?php echo intval($post->ID); ?>"> 
      <?php 
      $product_object = new WC_Product($post->ID); 
      $product_ids = $product_object->get_upsell_ids('edit'); 

      foreach ($product_ids as $product_id) { 
       $product = wc_get_product($product_id); 
       if (is_object($product)) { 
        echo '<option value="' . esc_attr($product_id) . '"' . selected(true, true, false) . '>' . wp_kses_post($product->get_formatted_name()) . '</option>'; 
       } 
      } 
      ?> 
     </select> <?php echo wc_help_tip(__('Up-sells are products which you recommend instead of the currently viewed product, for example, products that are more profitable or better quality or more expensive.', 'woocommerce')); ?> 
</p> 
1

This answer是一个很好的起点,但也有使与该产品搜索领域已经从input变化与type=hiddenselectmultiple属性的最新WooCommerce版本这项工作需要做一些改变。

后端输入字段

可以使用WooCommerce加售的领域,你可以找到here on GitHub的后台HTML,为自己的后端领域的模板。

保存后端输入字段

您可以使用下面的代码检索选定的项目,有IDS保存为后期荟萃。

$my_product_ids = isset($_POST['product_field_type_ids']) ? array_map('intval', explode(',', (array) $_POST['product_field_type_ids'])) : array(); 
update_post_meta($post_id, '_product_field_type_ids', $my_product_ids);