1

I'm trying insert price block in my module. My Controller.php

public function indexAction() {
    $sku = $this->getRequest()->getParam('sku');
    if (!$sku) { return false; }
    $product = Mage::getModel('catalog/product')->loadByAttribute('sku',$sku);
    if (!$product) { return false; }
    $response = [
        $product->getPrice(),
        $product->getName(),
        $product->getThumbnail(),
        $product->getIsSalable(),
        $product->getProductUrl(),
    ];

    $productBlock = $this->getLayout()->createBlock('catalog/product_price');
    $data = $productBlock->getPriceHtml($product, true);

    $this->getResponse()->setHeader('Content-type', 'application/json');
    $this->getResponse()->setBody(json_encode($response, $data));
}

And my Ajax section in template.phtml:

jQuery.ajax({
            url: url,
            type: 'POST',
            data: params,
            dataType: 'json',
            success: function (response, data) {
                if (response) {
                    var array = response;                    
                    var name = array[1];
                    var image = array[2];
                    var stock1 = array[3];
                    var url = array[4];

                    $j(idPrice).html(data);
                    $j(idName).text(name);
                    $j(idName).attr('href', url);
                    $j(idName).attr('title', name);
                    $j(idStock).text(stock);
                    $j(idImage).attr('src', "/media/catalog/product/" + image);
                }
            }
        });

But this leads to an error Ajax request.

  • What's the error you're getting ? – Raphael at Digital Pianism Feb 23 '16 at 9:21
  • By mistake, I mean the wrong work. In this case, the blank page. Although if I remove the code relating to the price block everything works fine. – Demi_Cogni Feb 23 '16 at 9:24
  • 1
    If you're getting a blank page, there must be an error somewhere. Try accessing your controller route directly via URL instead of AJAX and see what it gives you. – Raphael at Digital Pianism Feb 23 '16 at 9:26
  • I tried "site.com/ajaxproductinfo?sku=PR-24DC-TN" but again it gives me just a blank page :( – Demi_Cogni Feb 23 '16 at 9:55
  • Check your webserver error log and check the source code of the page – Raphael at Digital Pianism Feb 23 '16 at 10:06
1

The problem was solved.

public function indexAction() {
    $sku = $this->getRequest()->getParam('sku');
    if (!$sku) { return false; }
    $product = Mage::getModel('catalog/product')->loadByAttribute('sku',$sku);
    if (!$product) { return false; }
    $productBlock = $this->getLayout()->createBlock('catalog/product_price');
    $response = [
        $product->getPrice(),
        $product->getName(),
        $product->getThumbnail(),
        $product->getIsSalable(),
        $product->getProductUrl(),
        $product = $productBlock->getPriceHtml($product, true),
    ];

    $this->getResponse()->setHeader('Content-type', 'application/json');
    $this->getResponse()->setBody(json_encode($response));
}

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.