• I’m really in difficulties,

    I have a button:
    <a href="javascript: addtocart('<?php echo get_the_ID();?>','1')" class="button add_to_cart_button product_type_simple add-to-cart">ADD TO CART</a>

    And my js:

    function addtocart(productid,quantity){
        jQuery.ajax({
            type:"GET",
            url:"wp-content/themes/Teafull/addcart.php",
            data:"productid="+productid+"&quantity="+quantity,
            success:function(html)
            {
    			alert('Update Success');
    			var sum = html;
    			$("#total-cart span").text(html);
    			$("#right-cart").empty();
    			$("#right-cart").load("wp-content/themes/Teafull/cart-update.php");
            }
        });
    }

    My file cart-update.php:

    <?php
    /*
    Template Name: Update Cart
    */
    session_start();?>
    <h3><span>Gi? hàng</span></h3>
    	<?php
    		if($_SESSION['sizeof'] == 0){
    			echo '<div class="empty-cart"><p style="color: #49aba8;">No products in your cart<p><div class="clear"></div>';
    					}
    		else{
    				$ok = 1;
    					if (isset($_SESSION['cart']))
    					{
    						foreach ($_SESSION['cart'] as $k => $v)
    						{
    							if (isset($k))
    							{
    								$ok = 2;
    								break;
    							}
    						}
    					}
    					if($ok == 2){
    							$total = 0;
    							foreach ($_SESSION['cart'] as $key => $value)
    							{
    								$item[] = $key;
    							}
    							$str = implode(",", $item);
    							$total =0;
    							$my_query = query_posts(array('post__in' => $item,'post_type'=> 'product'));
    							global $post;
    							foreach ($my_query as $post) {
    							   $posts_by_id[$post->ID] = $post;
    							}
    							foreach ($item as $id) {
    								if (!$post = $posts_by_id[$id]) continue;
    							   setup_postdata($post);
    
    							   $price = mtbxr_val('price');
    							   $quantity = $_SESSION['cart'][$id];	
    
    								echo "<ul class='cart_list product_list_widget'><li>";
    									echo "<a href='"; echo the_permalink(); echo "'>";
    									echo the_post_thumbnail();
    									echo "<p>";echo the_title(); echo"</p></a>";
    									echo "<p class='clear' id='price-sidebar'><b>Price:</b>?"; echo $price;
    								echo "</p></li></ul>";
    							}
    
    				}
    		?>
    		<div class="clear"></div>
    		<p class="buttons">
    			<a href="https://localhost/tem5/?page_id=159" class="button">My cart →</a>
    		</p>
    		<div class="clear"></div>
    		<?php }?>

    I want when I click on the button “ADD TO CARD”, div #right-cart load file cart-update.php.
    When I click on the button “ADD TO CARD”, the following error happen:

    Fatal error: Call to undefined function query_posts() in C:\xampp\htdocs\tem5\wp-content\themes\Teafull\cart-update.php on line 32

    But, If I refresh page, Fatal error doesn’t happen.

    Could anyone please help me how to solve this problem?

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    All AJAX calls in WordPress should be routed through wp-admin/admin-ajax.php. To handle the request, you hook into a specific action related to the value for the JS array element “action” passed by the AJAX call. See AJAX in Plugins for more information.

Viewing 1 replies (of 1 total)
  • The topic ‘Load WordPress post content into DIV using AJAX’ is closed to new replies.