Saltar al contenido

Jquery – Ajax y JSON

———
Archivo all.js
———



$(document).ready(function() {
	
	//Evento al presionar el boton "Comprar ahora!"
	$("#addtocart").click(function(){		
		alert("Comprar ahora!");
	});
	
	//Evento al cambiar la cantidad
	$('select#quantity').on("change", function(){
		
		alert( $(this).attr("value") );
		alert ( $('#property-group').find('li.selected').attr('jockey-variation-id') );
		
		var variation_id = $('#property-group').find('li.selected').attr('jockey-variation-id');
		var quantity = $(this).attr("value"); 
		
		//Ajax HTML
		$.post("/ajaxcart/text1",{
			variation_id:variation_id, 
			quantity:quantity
		},function(data){
			$("#pari").html(data);
		});
	//EndAjax HTML	
		
	});
	
	//Evento al li
	$("#property-group li").click(function() {
		$('#property-group').find('li.selected').removeClass('selected');
		$(this).addClass("selected");
		
		var variation_id=$(this).attr('jockey-variation-id');
		var quantity = $('select#quantity').val(); 
		
		//Ajax JSON
		$.post("/ajaxcart/text2",{
			variation_id:variation_id, 
			quantity:quantity
		},function(data){
			$("#pari").html(data.price);
			
		}, "json");
	//EndAjax JSON		
	});	

});
	


Controlador: /controller/frontend/ajaxcart.php



class Controller_Frontend_Ajaxcart extends Controller {

    public function action_text1() {
        $data = "";
        if ($this->request->method() == 'POST') {
            $variation_id = $this->request->post('variation_id');
            $quantity = $this->request->post('quantity');
            $data = "Variation : " . $variation_id . " Quantity: " . $quantity;
        }

        $this->response->body($data);
    }

    public function action_text2() {
        $data = "";
        if ($this->request->method() == 'POST') {
            $variation_id = $this->request->post('variation_id');
            $quantity = $this->request->post('quantity');

            //Recuperarar la variacion y el stock_virtual
            $rs_variation = ORM::factory('product_variation')
                    ->where('product_variation.id', '=', $variation_id)
                    ->find();

            $array_variaton['price'] = $rs_variation->price;

            $data = json_encode($array_variaton);
        }
        $this->response->body($data);
    }
}

Si te ha interesado este artículo y deseas un apoyo o asesoría en algún requerimiento, envíame un mensaje a: (info@juliopari.com) o sino a través de Linkedin: https://www.linkedin.com/in/juliopari/