Wordpress 在woo commerce产品页面中添加产品搜索字段

Wordpress 在woo commerce产品页面中添加产品搜索字段,wordpress,woocommerce,Wordpress,Woocommerce,现在我正在使用下面的代码 // 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">'

现在我正在使用下面的代码

// 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>';

}
//显示字段
添加操作(“woocommerce\u product\u options\u general\u product\u data”、“woo\u add\u custom\u general\u fields”);
函数woo_添加_自定义_常规_字段(){
全球$woocmerce,$post;
回声';
//自定义字段将在此处创建。。。
//产品选择
?>


您正在使用woocommerce插件。若要使用价格范围、颜色、大小和其他字段过滤产品,您可以使用另一个woocommerce插件“woocommerce产品过滤器”。您可以使用yith插件搜索产品。如果您想提供搜索,则整个网站不仅适用于产品,而且适用于用户dave的live搜索插件


最近,我试图为我的一个客户解决同样的问题。我最初使用同一个教程在后端为他添加自定义产品搜索输入字段,但当我们将他更新到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
显示在前端:(与前面相同)

global$post;
$items=get_post_meta($post->ID,“_product_field_type_ID”,true);
如果(!空($items)){
foreach($product\U id形式的项目){
回声';
}  
}

我从up sells复制了此代码。这只是搜索表单

<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>

这是一个很好的起点,但在最新的WooCommerce版本中,产品搜索字段已从带有

type=hidden
input
更改为带有
multiple
属性的
select

后端输入字段

您可以使用WooCommerce upsells字段的后端HTML(您可以找到该字段)作为您自己后端字段的模板

保存后端输入字段

您可以使用以下代码检索所选项目并将这些ID保存为post meta

$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);

在WC 3.0.8中,这似乎不再适用于我。尚不确定如何修复它。您可以使用中的信息在最新的WooCommerce版本中使用此功能。
<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>
$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);