Sunday, June 12, 2022
HomeWordPress Developmentajax - easy methods to get a product id on clicking the...

ajax – easy methods to get a product id on clicking the add to cart button in class web page – Woocomerce


i need to get the product id when a buyer click on’s on the add to cart button in a class web page.
i looked for an answer a couple of hours and that is what i attempted however it dos not work (it get’s in to a ioop).
I attempted to loop within the almonds tree to get to the textual content of the product however I do not handle and one it would not do a loop so I get an undefined end result.
i assume the issue is that I must discover a method to loop within the elementary or fireplace the product id if out there.

how am i able to try this?

(i would like after it to redirect the client to a web page in line with the product he added to the cart – that i understand how to do…)

that is my code:

//redirect to picture add after add to cart
add_action('wp_footer', 'redirect_to_image_upload_after_add_to_cart');
    perform redirect_to_image_upload_after_add_to_cart(){
    ?>
    <script>
        (perform($){ 
                    var product_name;
                    $( doc.physique ).on( 'added_to_cart', perform(){
                            product_name = $(this).dad or mum.dad or mum.querySelectorAll('.title').little one.textContent;
                            alert(product_name);
                        });
                    })(jQuery);
            </script>
    <?php
            }

additionally $(this).getByLableText("sum textual content that's presupposed to be there")didn’t work.

i’ve this code for product web page (it’s not vital for the query) :

perform single_added_to_cart_event()
{
   if( (isset($_POST['add-to-cart']) || isset($_POST['added-to-cart'])) && isset($_POST['quantity']) ) :

   // Get added to cart product ID (or variation ID) and amount (if wanted)
   $id_to_check   = isset($_POST['variation_id']) ? esc_attr($_POST['variation_id']) : esc_attr($_POST['add-to-cart']);
   $product_id   = isset($_POST['variation_id']) ? esc_attr($_POST['variation_id']) : esc_attr($_POST['add-to-cart']);
   $variation_id  = isset($_POST['variation_id']) ? esc_attr($_POST['variation_id']) : 0;
   $amount      = esc_attr($_POST['quantity']);
   $found_in_cart = false; // Initializing

   // Examine cart gadgets to make certain that the product has been added to cart (and get product information)
   foreach( WC()->cart->get_cart() as $merchandise ) {
       $product = $merchandise['data']; // The WC_Product Object
       if( $product->get_id() == $id_to_check ) {
           $product_name = $product->get_name(); // Product identify
           $sku          = $product->get_sku(); // Product sku
           $sort         = $product->get_type(); // Product sku
           $value        = wc_get_price_to_display($product); // Product value for show
           $stock_qty    = $product->get_stock_quantity(); // Product sku
           $stock_status = $product->get_stock_status(); // Product sku
           $attributes   = $variation_id > 0 ? json_encode($product->get_attributes()) : "''";

           $found_in_cart = true;
           break; // Cease the loop
       }
   }


   if( $found_in_cart ) :

   // The displayed message (instance)
   if ( $variation_id > 0 ) {
       $message = sprintf( __('Product "%s" has been added to cart. rnProduct sort: %s rnProduct value: %s rnProduct_id: %s rnVariation_id: %s rnQuantity: %s'),
           $product_name, $sort, $value, $product_id, $variation_id, $amount );
   } else {
       $message = sprintf( __('Product "%s" has been added to cart. rnProduct sort: %s rnProduct value: %s rnProduct_id: %s rnQuantity: %s'),
           $product_name, $sort, $value, $product_id, $amount );
   }
   
   world $product;
   $id = $product->get_id();
   // JS code goes right here under
   ?>
   <script id='yehonathan-redirect-to-image-upload'>
   jQuery(perform($){
       // All product information
       var product_id   = <?php echo esc_attr($_POST['add-to-cart']); ?>,
           variation_id = <?php echo $variation_id; ?>,
           amount     = <?php echo $amount; ?>,
           attributes   = <?php echo $attributes; ?>,
           identify="<?php echo $product_name; ?>",
           sort="<?php echo $sort; ?>",
           sku          = '<?php echo $sku; ?>',
           value="<?php echo $value; ?>",
           stock_qty    = '<?php echo $stock_qty; ?>',
           stock_status="<?php echo $stock_status; ?>",
           message="<?php echo $message; ?>";
           location     = '<?php echo get_permalink( $product_id );?>#image-upload';
   });
   </script>
   <?php
   endif; endif;
   }

however i have no idea easy methods to make it work on class web page.

thank’s.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments