【问题标题】:WooCommerce display product thumbnail and add to cart buttonWooCommerce 显示产品缩略图并添加到购物车按钮
【发布时间】:2021-12-29 22:11:54
【问题描述】:

我创建了一个使用 AJAX 获取产品标题的实时搜索字段。 它可以正常工作,但是,我想获取产品缩略图和“添加到购物车”按钮。

**编辑:我添加了缩略图的输出并添加到购物车按钮。它可以工作,但它只显示在单行中。如何更新我的输出,每行 4 行,如下所示?

前端代码

<input type="text" name="keyword" id="keyword" onkeyup="fetch()">

<div id="datafetch">Your numbers will show here</div>



<script>
function fetch(){

    $.post('<?php echo admin_url('admin-ajax.php'); ?>',{'action':'my_action'},
    function(response){
        $('#datafetch').append(response);
        console.log(result);
    });
}
</script>

Functions.php 中的代码

         <?php
}// LOTTERY start the ajax function
add_action('wp_ajax_data_fetch' , 'data_fetch');
add_action('wp_ajax_nopriv_data_fetch','data_fetch');
function data_fetch(){

        $the_query = new WP_Query( array( 'posts_per_page' => -1, 's' => esc_attr( $_POST['keyword'] ), 'post_type' => 'product' ) );


    if( $the_query->have_posts() ) :
        while( $the_query->have_posts() ): $the_query->the_post();
    global $product;
        $product = get_product( get_the_ID() ); //set the global product object

$myquery = esc_attr( $_POST['keyword'] );
$a = $myquery;
$search = get_the_title();
if( stripos("/{$search}/", $a) !== false) {?>
            <h4><a href="<?php echo esc_url( post_permalink() ); ?>"><?php the_title();?></a></h4>
<h4><a href="<?php echo esc_url( post_permalink() ); ?>"><?php the_post_thumbnail();?></a></h4>
<p><?php echo $product->get_price_html(); ?></p>
                        <?php woocommerce_template_loop_add_to_cart(); //ouptput the woocommerce loop add to cart button ?>

        <?php
                                  }
    endwhile;
        wp_reset_postdata();  
    endif;

    die();
}

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    您可以使用此get_the_post_thumbnail_url(get_the_ID()) 获取缩略图 URL 并添加图片。

    您可以使用此https://yourdomain.com/?add-to-cart=&lt;product_id&gt; 将产品添加到购物车 URL。

    https://developer.wordpress.org/reference/functions/get_the_post_thumbnail_url/

    【讨论】:

    • 我试过 get_the_post_thumbnail_url() 没有输出任何东西。对于添加到购物车,此解决方案如何对搜索中的产品数组变得动态?
    • 你能把代码分享给get_the_post_thumbnail_url(get_the_ID())
    • 好的——我可以输出帖子缩略图了。但是,如何在此处动态添加添加到购物车按钮?
    • 嗨,你试过了吗? stackoverflow.com/a/61058748/8328727?此外,我在您的 ajax 调用中发现了更多安全漏洞。请也解决这个问题。
    【解决方案2】:

    试试这个方法

    //Ajax
    add_action('wp_ajax_data_fetch' , 'data_fetch');
    add_action('wp_ajax_nopriv_data_fetch','data_fetch');
    function data_fetch(){
    
        $the_query = new WP_Query( array( 'posts_per_page' => -1, 's' => esc_attr( $_POST['keyword'] ), 'post_type' => 'product' ) );
    
        if( $the_query->have_posts() ) :
            while( $the_query->have_posts() ): $the_query->the_post();
    
                $myquery = esc_attr( $_POST['keyword'] );
                $a = $myquery;
                $search = get_the_title();
                if( stripos("/{$search}/", $a) !== false) {
                    
                    //get image and add-to-cart buttom here
                    $query->the_post(); 
                    global $product;
                    wc_get_template_part('content', 'product');
                    //End get image and add-to-cart buttom here
    
                }
    
        endwhile;
            wp_reset_postdata();  
        endif;
    
        die();
    }
    

    【讨论】:

    • 这对我不起作用,我添加后它根本没有显示任何内容。
    猜你喜欢
    • 1970-01-01
    • 2018-08-11
    • 1970-01-01
    • 2020-09-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-21
    • 2017-11-23
    • 2021-07-13
    相关资源
    最近更新 更多