• Hello everyone! this is my first post!! haha I’m sure it will be useful.

    I am working with WooCommerce and I want to create a new page in my wordpress to show the history of ordered products with an input text for quantity and an AJAX button to add to the cart. I know that there is a history order in the Client Account but i need a product history with the input and button to add in order to make an easy and direct tool for the Client if he wants to repeat some products.

    What i am doing is a Custom Plugin who creates a Shortcode that i use in the History page.(I dont know if it is the right way to do it, but it works).

    the code is like this:

    <?php
    /*
    Plugin Name: Historial Productos
    Plugin URI:  http://URI_Of_Page_Describing_Plugin_and_Updates
    Description: Funcionalidad adicional
    Version:     0
    Author:      Javier Rodriguez Perez
    Author URI:  http://URI_Of_The_Plugin_Author
    License:     GPL2
    License URI: https://www.gnu.org/licenses/gpl-2.0.html
    Domain Path: /languages
    Text Domain: my-toolset
    */
    
    function historial_creation(){
        global $wpdb;    
    
    ?>
    <h2>A continuación se detallan los productos más pedidos:</h2>
    
    <?php
    
    	if (is_user_logged_in()){
    
    		$filters = array(
    		'post_status' => 'any',
    		'post_type' => 'shop_order',
    		'posts_per_page' => 200,
    		'meta_key'    => '_customer_user',
            'meta_value'  => get_current_user_id(),
    		'paged' => 1,
    		'orderby' => 'date',
    		'order' => 'ASC'
    		);
    
    		$loop = new WP_Query($filters);
    
    		while ($loop->have_posts()) {
    			$loop->the_post();
    			$order = new WC_Order($loop->post->ID);
    
                            //echo '<pre>';
    			//print_r($order->order_date);
    			//echo '</pre>';
    			?>
    
                            <h3>Pedido nº <?php echo $order->post->ID ?></h3><br>
    
    			<table class="shop_table my_account_orders" width="100%">
    
    			<thead>
    				<tr>
                        <th class="order-number"><span class="nobr">Codigo</span></th>
    					<th class="order-number"><span class="nobr">Producto</span></th>
    					<th class="order-date"><span class="nobr">Fecha</span></th>
    					<th class="order-status"><span class="nobr">Cantidad</span></th>
    					<th class="order-total"><span class="nobr"><?php _e( 'Total', 'woocommerce' ); ?></span></th>
    					<th class="order-actions">Acciones</th>
    				</tr>
    			</thead>
    			<tbody>  
    
    			<?php
    			foreach ($order->get_items() as $key => $lineItem) {
    				$id=$lineItem['product_id'];
    				//uncomment the following to see the full data
    					  //echo '<pre>';
    					  //print_r($lineItem);
    					  //echo '</pre>';
    				echo '<tr>';
    				echo '<td>'. $id . '</td>';
    				echo '<td>'. $lineItem['name'] . '</td>';
    				echo '<td>'. $order->order_date . '</td>';
    				echo '<td>'.  $lineItem['qty'] . '</td>';
    				echo '<td>'. '<div class="quantity buttons_added"><input type="button" value="-" class="minus"><input type="number" step="1" min="0" max="100" name="quantity$id" value="1" title="Cantidad" class="input-text qty text" size="4"><input type="button" value="+" class="plus"></div>'  . '</td>';
    				echo '</tr>';
    				/*
    				if ($lineItem['variation_id']) {
    					echo 'Product Type : Variable Product' . '<br>';
    				} else {
    					echo 'Product Type : Simple Product' . '<br>';
    				}*/
    				//echo do_shortcode('[product id=$id]');
    			}
    			?>
    			</tbody>
                </table>
                <?php
    		}
    
    	}
    
    } //funcion
    
    add_shortcode('historial_productos', 'historial_creation');

    THANKS A LOT!!!!

    P.D. Sorry for my bad English

    https://wordpress.org/plugins/woocommerce/

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘Quantity and AJAX add to cart button in a listing products’ is closed to new replies.