T
T
trenton2021-07-29 23:46:04
AJAX
trenton, 2021-07-29 23:46:04

How to implement it correctly so that the product is added to the cart through a self-written modal, and not redirected to the product card?

I have been using a self-written modal for a long time, but not my own, I will only improve it. It suits my needs better than similar plugins, but there is a nuance - when you select the desired variation and then click "add to cart" after that, it redirects to the product page, and does not add to the cart. What is the error and how to fix it?
I think it most likely has something to do with the woocommerce_add_to_cart_redirect() filter, but what is the right way to use it?
I just want to bring my own to the ideal, and not "just install the plugin and don't bathe," I wouldn't write this line. if as soon as I did not write it, these "advice" would not immediately pour in, it has been checked many times.
Her own conclusion.

add_action( 'wp_ajax_ajax_quick_view', 'themename_quick_view_product_callback' );
add_action( 'wp_ajax_nopriv_ajax_quick_view', 'themename_quick_view_product_callback' );
function themename_quick_view_product_callback(){
  if ( ! wp_verify_nonce( $_POST['nonce'], 'quick-nonce' ) ) {
    wp_die( 'Данные отправлены с левого адреса' );
  }
  global $post, $woocommerce, $product;
  $product = wc_get_product(esc_attr($_POST['id']));
  ob_start();	
  ?>
  <div class="modal-body row">
    <div class="modal-body-left col-md-5">
      <?php 
      $attachment_id = get_post_thumbnail_id( $product->get_id() );
      $product_thub = wp_get_attachment_image_url($attachment_id, 'shop_single'); ?>
      <img src="<?php echo $product_thub;?>" >

      <div class="modal-gal d-flex">
        <?php 
        $attachment_ids = $product->get_gallery_attachment_ids();
          
        foreach( $attachment_ids as $attachment_id ) {
          echo wp_get_attachment_image( $attachment_id, 'woocommerce_gallery_thumbnail' );
        }
 				?>
      </div>

    </div>
    <div class="modal-body-right col-md-7">
      <h2><?php echo $product->get_name();?></h2>			
      <p><?php $rating  = $product->get_average_rating();
          $count   = $product->get_rating_count();
          echo wc_get_rating_html( $rating, $count ); ?></p>	
      <div class="modal-price mb-20">
        <?php echo $product->get_price_html();?></div>
      <p class="mb-20"><?php echo $product->get_short_description(); ?></p>	
      <p class="mb-20 cat-popup"><?php echo $product->get_categories($separator = ' '); ?></p>
      <div class="modal-buttons">
        <?php
        if ( $product->is_type( 'variation' ) ) {
        ?>
              <form class="cart" action="<?php echo esc_url( apply_filters( 'woocommerce_add_to_cart_form_action', $product->get_permalink() ) ); ?>" method="post" enctype='multipart/form-data'>
                <?php woocommerce_single_variation_add_to_cart_button(); ?>
             </form>
              <?php
            } else {
              woocommerce_template_single_add_to_cart();
            }?>				
      </div>			
    </div>
  </div>
  <?php  
  $data['product'] = ob_get_clean();
  wp_send_json( $data);
  wp_die();
}

JS part
jQuery(document).ready(function ($) {
    $(document).on('click', 'a.modal-product-link', function () {
        var productId = $(this).attr('data-product-id');
        console.log(productId);
        var data = {
            id:productId,
            action:'ajax_quick_view',
            nonce: ajax_quick.nonce
        };
        $.ajax({
            url:ajax_quick.url,
            data:data,
            type: 'POST',
            dataType: 'json',
            beforeSend:function(xhr){
               $('#modal-product .modal-body').text('Загрузка');
            },
            success:function(data){
                console.log(data);
                $('#modal-product .modal-content section').html(data.product);
            }
        });
    })
});


Modal output in the theme, if important
add_action( 'woocommerce_before_shop_loop_item_title', 'themename_loop_product_div_image_close', 30);
function themename_loop_product_div_image_close(){  
  global $product;
  ?>
  <div class="loop-product_bottom">
        <a href="#" data-toggle="modal" data-target="#modal-product" data-product-id="<?php echo $product->get_id();?>" class="modal-product-link">
         <span class="fa-eye"></span>          
        </a>
 </div>

<?php }


/*MODAL*/
 add_action( 'wp_footer', 'themename_modal_window');
 function themename_modal_window(){
  ?>
    <div class="modal video-modal fade" id="modal-product" tabindex="-1" role="dialog" aria-labelledby="modal-product">
        <div class="modal-dialog modal-dialog-product-review" role="document">
          <div class="modal-content">
            <div class="modal-header">
              <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            </div>
            <section class="modal-section mb-20">
            <div class="modal-body">              
            </div>
            </section>
          </div>
        </div>
      </div>
 <?php 
 }

I have already posted it many times, I hope they will help me in this last detail, which still separates it from the ideal.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question