Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 我需要添加自定义查询和添加到购物车按钮在woocommerce动态代码?_Php_Mysql_Wordpress_Woocommerce - Fatal编程技术网

Php 我需要添加自定义查询和添加到购物车按钮在woocommerce动态代码?

Php 我需要添加自定义查询和添加到购物车按钮在woocommerce动态代码?,php,mysql,wordpress,woocommerce,Php,Mysql,Wordpress,Woocommerce,我正在为我的WordPress网站制作自定义模板。因此,我需要在自定义模板中动态显示我的产品。我该怎么做 这是我的html代码--> ================================================ 我也做了简单的查询,但我没有找到添加到购物车按钮的动态代码 这是我的查询代码--> 对不起,您正在查找的帖子不可用 所以我的问题是--> 我的问题是对的还是错的??如果我的查询是错误的,请给我正确的查询。。。另外,如果我的查询几乎正确,请给我“添加到购物车”(

我正在为我的WordPress网站制作自定义模板。因此,我需要在自定义模板中动态显示我的产品。我该怎么做

这是我的html代码-->


================================================

我也做了简单的查询,但我没有找到添加到购物车按钮的动态代码

这是我的查询代码-->


对不起,您正在查找的帖子不可用

所以我的问题是-->

  • 我的问题是对的还是错的??如果我的查询是错误的,请给我正确的查询。。。另外,如果我的查询几乎正确,请给我“添加到购物车”()按钮动态Ajax代码
  • 请帮帮我。。。 请帮帮我


    谢谢

    我已经对代码进行了必要的更改。确保woocommerce add-to-cart.js或add-to-cart.min.js已在页面中排队

    <div class="all_content_shop">
    <?php wc_print_notices(); ?>
    <?php $new_posts = new WP_Query( array(
        'post_type'      => 'product', //post of page of my post type
        'cat'            => 0, // category id, 0 for all categories.
        'posts_per_page' => 12,
        'offset'         => 0, //how many posts you want to eliminate from the query
        'orderby'        => '', // order by title or date ?
        'order'          => 'DESC'
    ) // order as ASC or DESC
    ); ?>
    <?php if ( $new_posts->have_posts() ) :
        while ( $new_posts->have_posts() ) :
            $new_posts->the_post();
            global $product;
            $product = get_product( get_the_ID() ); //set the global product object?>
    
            <div class="col-md-3 single_all_c_s">
                <div class="shop_product_inner">
                    <a href="<?php the_permalink() ?>">
                        <?php if ( has_post_thumbnail( get_the_ID() ) ) {
                            echo get_the_post_thumbnail( get_the_ID(), 'shop_single' );
                        } else {
                            echo '<img src="' . woocommerce_placeholder_img_src() . '" alt="Placeholder" />';
                        } ?>
                        <div class="shop_product_inner_caption">
                            <h2><?php the_title(); ?></h2>
    
                            <p><?php echo $product->get_price_html(); ?></p>
                            <?php woocommerce_template_loop_add_to_cart(); //ouptput the woocommerce loop add to cart button ?>
                        </div>
                    </a>
                </div>
            </div>
    
        <?php endwhile;//Possibility to add else statement ?>
        <?php wp_reset_postdata(); ?>
    <?php else: ?>
        <p class="not_found">Sorry, The post you are looking is unavailable!</p>
    <?php endif;
    wp_reset_query(); ?>
    
    
    

    对不起,您正在查找的帖子不可用


    您的实现目前存在哪些错误?感谢您的回复。现在我无法为“添加到购物车”按钮添加动态ajax代码html代码为=>非常感谢。但我有另一个问题。问题是,当我添加到购物车,但购物车计数和购物车价格没有更新没有刷新。请查看屏幕截图-->我需要修复它,,,。。请帮助我。。。您可以查看我的网站进行更新@sabarnix感谢解决此问题的最简单的方法是使用
    woocommerce\u template\u loop\u add\u to\u cart()使用
    echo'这将使用get请求添加到购物车,因此页面将刷新。如果你想使用ajax,你必须看看woocommerce迷你购物车是如何工作的,以及js文件是如何更新的。告诉我你想实现哪一个,我明天可能会发布一些代码。非常感谢你的回复。是的,我明白。但是我需要使用ajax来完成它。请帮帮我@sabarnix谢谢
    
                    <div class="all_content_shop">
    
                        <?php $new_posts = new WP_Query(array(
                        'post_type' => 'product', //post of page of my post type
                        'cat' => 0, // category id, 0 for all categories. 
                        'posts_per_page' => 12, 
                        'offset' => 0, //how many posts you want to eliminate from the query
                        'orderby' => '', // order by title or date ?
                        'order' => 'DESC') // order as ASC or DESC
                        ); ?>
                        <?php if ($new_posts->have_posts()) :
                            while ($new_posts->have_posts()) :
                                    $new_posts->the_post(); ?>
    
                            <div class="col-md-3 single_all_c_s"> 
                                <div class="shop_product_inner"> 
                                    <a href="<?php echo get_permalink( $loop->post->ID ) ?>">
                                        <?php if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, 'shop_single'); else echo '<img src="'.woocommerce_placeholder_img_src().'" alt="Placeholder" />'; ?>
                                        <div class="shop_product_inner_caption"> 
                                            <h2><?php the_title(); ?></h2>
                                            <p><?php if ( ! defined( 'ABSPATH' ) ) exit; global $post, $product; ?> <?php echo $product->get_price_html(); ?></p>
                                            <a href="">Add to Cart</a>
                                        </div>
                                    </a>
                                </div>
                            </div>
    
                        <?php endwhile;//Possibility to add else statement ?>
                            <?php wp_reset_postdata(); ?>
                            <?php else:?>
                            <p class="not_found">Sorry, The post you are looking is unavailable!</p>
                        <?php endif; wp_reset_query(); ?>
                </div>
    
    <div class="all_content_shop">
    <?php wc_print_notices(); ?>
    <?php $new_posts = new WP_Query( array(
        'post_type'      => 'product', //post of page of my post type
        'cat'            => 0, // category id, 0 for all categories.
        'posts_per_page' => 12,
        'offset'         => 0, //how many posts you want to eliminate from the query
        'orderby'        => '', // order by title or date ?
        'order'          => 'DESC'
    ) // order as ASC or DESC
    ); ?>
    <?php if ( $new_posts->have_posts() ) :
        while ( $new_posts->have_posts() ) :
            $new_posts->the_post();
            global $product;
            $product = get_product( get_the_ID() ); //set the global product object?>
    
            <div class="col-md-3 single_all_c_s">
                <div class="shop_product_inner">
                    <a href="<?php the_permalink() ?>">
                        <?php if ( has_post_thumbnail( get_the_ID() ) ) {
                            echo get_the_post_thumbnail( get_the_ID(), 'shop_single' );
                        } else {
                            echo '<img src="' . woocommerce_placeholder_img_src() . '" alt="Placeholder" />';
                        } ?>
                        <div class="shop_product_inner_caption">
                            <h2><?php the_title(); ?></h2>
    
                            <p><?php echo $product->get_price_html(); ?></p>
                            <?php woocommerce_template_loop_add_to_cart(); //ouptput the woocommerce loop add to cart button ?>
                        </div>
                    </a>
                </div>
            </div>
    
        <?php endwhile;//Possibility to add else statement ?>
        <?php wp_reset_postdata(); ?>
    <?php else: ?>
        <p class="not_found">Sorry, The post you are looking is unavailable!</p>
    <?php endif;
    wp_reset_query(); ?>