2016-04-26 16 views
17

Sto creando un sito Web di e-commerce personalizzato utilizzando woocommerce e sto riscontrando qualche problema nel correggere il pulsante "aggiungi al carrello". Ogni volta che aggiungo più importi nella casella di input/quantità, incrementa o aggiunge un solo oggetto al carrello. Questo succede solo quando creo un ciclo personalizzato.AJAX aggiungi al carrello il pulsante non funziona sul prodotto del ciclo di query personalizzato woocommerce

Nella pagina del negozio e del singolo prodotto, funziona perfettamente. Se aggiungo 10 articoli e premere il pulsante Aggiungi al carrello. Aggiunge esattamente 10 articoli al carrello.

Ecco il modello che ho lavorato.

<?php 

/* 
* Template Name: Home 
*/ 

get_header(); ?> 

<section class="full-width home-template"> 

    <div class="full-width shop-section"> 

     <div class="container"> 

      <?php 
      $args = array(
        'post_type' => 'product', 
        'meta_query' => array(
         array(
          'key' => '_stock_status', 
          'value' => 'instock' 
         ) 
        ) 
      ); 

      $crate_products = new WP_Query ($args); 
      if ($crate_products->have_posts()) : while ($crate_products->have_posts()) : 
       $crate_products->the_post(); 

      ?> 

      <div id="post-<?php the_ID() ?>" class="three columns product-post"> 

       <?php // wc_get_template_part('content', 'product'); ?> 


       <figure class="featured-image"> 
        <?php 
        //Display Product Thumbnail 
        $product_thumbnail = woocommerce_get_product_thumbnail(); 

        ?> 

        <a href="<?php the_permalink()?>" ><?php echo $product_thumbnail ?></a> 
       </figure> 


       <h2 class="product-price"><a href="<?php the_permalink(); ?>"><?php wc_get_template('single-product/price.php'); ?></a></h2> 
       <span class="product-name"><?php the_title(); ?></span> 

       <?php // woocommerce_quantity_input(); ?> 


       <div class="add-to-cart-btn"> 
       <?php woocommerce_template_loop_add_to_cart($crate_products->post, $product); ?> 
       <?php // do_action('woocommerce_after_shop_loop_item'); ?> 
       </div> 


      </div> 

      <?php wp_reset_postdata(); ?> 

      <?php endwhile; else: ?> 

      <?php endif; ?> 

      <?php wp_reset_query(); ?> 


     </div> 

    </div> 


</section> 


<?php get_footer(); ?> 

Cosa c'è confusione anche è che la funzionalità AJAX funziona sul modello upsells (up-sells.php), che è un modello di woocommerce e funziona benissimo.

<?php 
/** 
* Single Product Up-Sells 
* 
* This template can be overridden by copying it to yourtheme/woocommerce/single-product/up-sells.php. 
* 
*/ 

if (! defined('ABSPATH')) { 
    exit; // Exit if accessed directly 
} 

global $product, $woocommerce_loop; 

$upsells = $product->get_upsells(); 

if (sizeof($upsells) === 0) { 
    return; 
} 

$meta_query = WC()->query->get_meta_query(); 

$args = array(
    'post_type'   => 'product', 
    'ignore_sticky_posts' => 1, 
    'no_found_rows'  => 1, 
    'posts_per_page'  => $posts_per_page, 
    'orderby'    => $orderby, 
    'post__in'   => $upsells, 
    'post__not_in'  => array($product->id), 
    'meta_query'   => $meta_query 
); 

$products = new WP_Query($args); 

$woocommerce_loop['columns'] = $columns; 

if ($products->have_posts()) : ?> 

    <div class="upsells products"> 

     <div class="twelve columns"> 
      <h2><?php // _e('You may also like&hellip;', 'woocommerce') ?></h2> 
     </div> 

     <?php woocommerce_product_loop_start(); ?> 

      <?php while ($products->have_posts()) : $products->the_post(); ?> 

       <div id="post-<?php the_ID() ?>" class="three columns product-post"> 

        <?php wc_get_template_part('content', 'product'); ?> 





       </div> 

      <?php endwhile; // end of the loop. ?> 

     <?php woocommerce_product_loop_end(); ?> 

    </div> 

<?php endif; 

wp_reset_postdata(); 

Ho già provato l'applicazione delle soluzioni da questo sviluppatore

e anche questa

Ma produce ancora la stessa uscita. Non so davvero perché accresce solo un articolo nel carrello. Ho controllato la console del browser per eventuali errori e hanno anche commentato alcune parti del codice per garantire o farvi sapere che ho provato metodi o diverse opzioni per rendere il lavoro funzionalità

+0

Non vedo l'input quantità ... – Reigel

+0

Qui è l'ingresso quantità clestcruz

+0

se è possibile rintracciarlo, quell'azione avrà sempre la quantità 1. – Reigel

risposta

0
<?php // woocommerce_quantity_input(); ?> 

Dovrebbe essere

<?php woocommerce_quantity_input(); ?> 
+0

Il motivo per cui viene commentato è perché ho provato a utilizzare tale metodo e non ha funzionato. – clestcruz

1

le seguenti operazioni

  1. Decommentare woocommerce_quantity_input();
  2. Arrivo browser Console, se ci ci sono errori nella console o no. Se sì, per favore condividi i tuoi errori qui.
  3. Se non ci sono errori quindi sostituire

woocommerce_template_loop_add_to_cart($crate_products->post, $product);
con
print_r(woocommerce_template_loop_add_to_cart($crate_products->post, $product));

e verificare se restituisce alcun dato o no.

provare anche decommentando do_action('woocommerce_after_shop_loop_item');

+0

Ho applicato la soluzione e ho ancora lo stesso risultato, incrementa solo 1 elemento nel carrello. Controllo la console del browser e non ci sono errori. – clestcruz

+0

Ok allora qual è il risultato per print_r (woocommerce_template_loop_add_to_cart ($ crate_products-> post, $ product)); –

+0

Come quello che ho detto aumenta ancora solo 1 nel carrello. Ho anche provato a non commentare do_action ('woocommerce_after_shop_loop_item'); e anche lo stesso output – clestcruz

0

Ecco una versione aggiornata. ?

<?php 
/* 
* Template Name: Home 
*/ 
get_header(); ?> 

<section class="full-width home-template"> 
    <div class="full-width shop-section"> 
     <div class="container"> 

      <?php 

      global $product; 

      $args = array(
       'post_type' => 'product', 
       'meta_query' => array(
        array(
         'key' => '_stock_status', 
         'value' => 'instock' 
        ) 
       ) 
      );    

      $posts = get_posts($args);    
      foreach($posts as $post) : 
       setup_postdata($post); 
       wc_setup_product_data($post); 
       $product = wc_get_product($post->ID); ?> 

       <div id="post-<?php the_ID() ?>" class="three columns product-post"> 

        <figure class="featured-image"> 
         <a href="<?php the_permalink()?>" ><?php echo woocommerce_get_product_thumbnail(); ?></a> 
        </figure> 

        <h2 class="product-price"><a href="<?php the_permalink(); ?>"><?php wc_get_template('single-product/price.php'); ?></a></h2> 
        <span class="product-name"><?php the_title(); ?></span> 

        <?php woocommerce_quantity_input(); ?> 

        <div class="add-to-cart-btn"> 
         <?php woocommerce_template_loop_add_to_cart(); ?>     
        </div> 

       </div> 

      <?php endforeach; ?> 

      <script type="text/javascript"> 

       (function($){ 
        $(document).ready(function(){ 
         $(document).on("keyup", "input.qty", function(){      
          $(this).parent().next().find("a").attr("data-quantity", $(this).val()); 
         }); 
        }); 
       })(jQuery); 

      </script> 

     </div> 
    </div> 
</section> 

<?php get_footer(); ?> 
Problemi correlati