gpt4 book ai didi

wordpress - 在woo-commerce产品页面添加产品搜索字段

转载 作者:行者123 更新时间:2023-12-01 09:54:34 24 4
gpt4 key购买 nike

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

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

最佳答案

我最近试图为我的一个客户找出同样的问题。我最初使用相同的教程在后端为他添加自定义产品搜索输入字段,当我们将他更新到 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>';
}
}

关于wordpress - 在woo-commerce产品页面添加产品搜索字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30973651/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com