1

I make a request in ajax at the click of the button passing as parameter the id of a certain product to the controller. This controller directs the product id to a helper that adds the same to the cart, but that question is not that important in this case. In this helper I create an array of data with the name of the product, price, the url of the image and the image of that product and return that array to the controller. The controller returns this array to my ajax request. I need this array to be displayed in certain fields in HTML code.

Ajax request:

    function addCartao(product_id){
        alert(product_id);
        $j.ajax({
          type: "POST",
          url: "<?php echo Mage::getUrl('fol_carousel/ajax/teste') ?>",
          data: {
            product_id: product_id
          },
          dataType: 'json',
          cache : false,
          beforeSend: function () {
            alert("beforeSend");
          },
          success: function (retorno) {
            alert('success');
            alert(retorno['name']);
            alert(retorno['price']);
            alert(retorno['media']);
            alert(retorno['imagem']);

            //alert(retorno);
            $j('.item-custom').append('<tr><td class="a-center lc-thumbnails"><img src="Here would be the image of the product" width="50" height="50" alt="Here would be the product name"></td><td><h3 class="product-name">Here would be the product name</h3></td><td class="a-center">1</td><td class="a-right"><span class="cart-price"><span class="price">Here would be the price of the product</span></span></td></tr>');
          },
          complete: function () {
            alert("complete");
          },
          error: function (x,y,z) {
            alert("error");
            alert(x);
            alert(y);
            alert(z);
          }
      });
    }

Controller code:

$product_id = $this->getRequest()->getParams();
$dados = Mage::helper('fol_carousel/cartao')->addCustom($product_id);
echo json_encode($dados);

Helper code along with the array I create:

public function addCustom($product_id)
{
    // Get customer session
    $session = Mage::getSingleton('customer/session'); 
    // Get cart instance
    $cart = Mage::getSingleton('checkout/cart'); 
    $cart->init();
    // Add a product with custom options
    $product = Mage::getModel('catalog/product')->load($product_id);

    $param = array(
        'product' => $product->getId(),
        'qty' => 1
    );
    $request = new Varien_Object();
    $request->setData($param);
    $cart->addProduct($product, $param);            
    // update session
    $session->setCartWasUpdated(true);
    // save the cart
    $cart->save();      

    $nome = $product->getName();
    $preco = $product->getPrice();
    $media = Mage::getModel('catalog/product_media_config')->getMediaUrl($product->getThumbnail());
    $img = $media;

    $dados['name'] = $nome;
    $dados['price'] = $preco;
    $dados['media'] = $media;
    $dados['imagem'] = $img;

    return $dados;
}

Note: I do not know if the .append() function would be the correct one in this case, so I'm in need of some help.

1

The solution I found to the problem was by changing the .append() function of the ajax request, concatenating with the array positions in the required places.

Changed ajax request code:

    function addCartao(product_id){
        alert(product_id);
        $j.ajax({
          type: "POST",
          url: "<?php echo Mage::getUrl('fol_carousel/ajax/teste') ?>",
          data: {
            product_id: product_id
          },
          dataType: 'json',
          cache : false,
          beforeSend: function () {
            alert("beforeSend");
          },
          success: function (retorno) {
            alert('success');
            alert(retorno['name']);
            alert(retorno['price']);
            alert(retorno['media']);
            alert(retorno['imagem']);

            $j('.item-custom').append('<tr><td class="a-center lc-thumbnails"><img src="' + retorno['imagem'] + '" width="50" height="50" alt="' + retorno['name'] + '"></td><td><h3 class="product-name">' + retorno['name'] + '</h3></td><td class="a-center">1</td><td class="a-right"><span class="cart-price"><span class="price"> R$ ' + retorno['price'] + '</span></span></td></tr>');
          },
          complete: function () {
            alert("complete");
          },
          error: function (x,y,z) {
            alert("error");
            alert(x);
            alert(y);
            alert(z);
          }
      });
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.