2014-07-16 33 views
3

我爲自己的WordPress網站製作了自定義模板。所以我需要動態地在自定義模板中顯示我的產品。我怎樣才能做到這一點?我需要添加自定義查詢並添加到購物車按鈕動態代碼在woocommerce?

這是我的HTML代碼 - >

   <div class="all_content_shop"> 
       <div class="col-md-3 single_all_c_s"> 
        <div class="shop_product_inner"> 
         <a href="<?php echo get_permalink($loop->post->ID) ?>"> 
          <img src="http://jetsetbabies.com/wp-content/uploads/2006/07/100Juice_Organic_Apple_MAIN_v2.jpg" alt="Prduct Image" /> 
          <div class="shop_product_inner_caption"> 
           <h2>Product Title</h2> 
           <p>$200</p> 
           <a href="">Add to Cart</a> 
          </div> 
         </a> 
        </div> 
       </div> 
      </div> 

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

另外我是讓簡單的查詢,但我沒有找到添加到購物車按鈕動態代碼?

這是我的查詢代碼---->

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

所以我的問題是--->

  1. 我的查詢是對還是錯?如果我的查詢是錯了,請給我正確的查詢......還有,如果我的查詢是差不多吧,所以請給我公正「添加到購物車」(http://prntscr.com/439lxj)按鈕動態Ajax代碼...

請幫我... 請幫我...

謝謝!!!

+0

? –

+0

感謝您的回覆。 現在我不能添加動態AJAX代碼爲「添加到購物車」按鈕 HTML代碼是=>Add to Cart

回答

5

我已經爲代碼工作做了必要的修改。確保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(); ?> 

+0

非常感謝。 但我有另一個問題。問題是,當我加入購物車時,購物車數量和購物車價格沒有刷新就沒有更新。請看截圖 - > http://prntscr.com/44gw92 我需要修復它,,, ..請幫助我...你可以看到我的網站更新。 http://pixiefy.com/arnob/ @sabarnix 謝謝 –

+1

最簡單的解決方案將取代'woocommerce_template_loop_add_to_cart();'使用'echo'Add to cart';'這將使用get請求添加到購物車,因此頁面將會刷新。如果你想用ajax來做,你必須看看woocommerce mini cart是如何工作的,以及js文件如何更新。告訴我你想實施哪一個,我明天可能會發布一些代碼。 – sabarnix

+0

非常感謝您的回覆。 是的,我明白。但我需要用ajax來做。請幫幫我。 @sabarnix 謝謝 –

相關問題