Ajaxコールで製品を入手してください
-
16-10-2019 - |
質問
私のプロジェクトでは、このようなURLがあります localhost/oldedge/catalog/category/view/id/37
Ajaxリクエストとして合格しようとしています。それで、これを行うために、私は過剰になりました viewAction()
の CategoryController
私の新しいモジュールからファイル。
誰かが私に新しいViewactionメソッドに何を書くかを提案してもらえますか。 id
のカテゴリー? (つまり、正しく理解していれば)
私はajaxとphpの両方で良くありません。
古い「viewaction()メソッド」
public function viewAction()
{
if ($category = $this->_initCatagory()) {
$design = Mage::getSingleton('catalog/design');
$settings = $design->getDesignSettings($category);
// apply custom design
if ($settings->getCustomDesign()) {
$design->applyCustomDesign($settings->getCustomDesign());
}
Mage::getSingleton('catalog/session')->setLastViewedCategoryId($category->getId());
$update = $this->getLayout()->getUpdate();
$update->addHandle('default');
if (!$category->hasChildren()) {
$update->addHandle('catalog_category_layered_nochildren');
}
$this->addActionLayoutHandles();
$update->addHandle($category->getLayoutUpdateHandle());
$update->addHandle('CATEGORY_' . $category->getId());
$this->loadLayoutUpdates();
// apply custom layout update once layout is loaded
if ($layoutUpdates = $settings->getLayoutUpdates()) {
if (is_array($layoutUpdates)) {
foreach($layoutUpdates as $layoutUpdate) {
$update->addUpdate($layoutUpdate);
}
}
}
$this->generateLayoutXml()->generateLayoutBlocks();
// apply custom layout (page) template once the blocks are generated
if ($settings->getPageLayout()) {
$this->getLayout()->helper('page/layout')->applyTemplate($settings->getPageLayout());
}
if ($root = $this->getLayout()->getBlock('root')) {
$root->addBodyClass('categorypath-' . $category->getUrlPath())
->addBodyClass('category-' . $category->getUrlKey());
}
$this->_initLayoutMessages('catalog/session');
$this->_initLayoutMessages('checkout/session');
$this->renderLayout();
}
elseif (!$this->getResponse()->isRedirect()) {
$this->_forward('noRoute');
}
}
新しい「viewaction()メソッド」
public function viewAction()
{
if ($category = $this->_initCatagory()) {
//Here I am trying to send response as only the list of products related to that category.
}
}
今、私が見せようとしているのは、PHPからの応答を伴うアラートボックスです。
main.js
jQuery(function(){
jQuery('#nav a').on('click',function(){
var url = jQuery(this).attr('data-href');
alert(url);
//ajax call here
jQuery.ajax({
url: url,
success: function(response) {
result = response;
alert(response);
}
});
});
});
上記のjQueryコードは、CategoryController.phpから古い「ViewAction」メソッドを試してみると、完全なHTMLページとして応答を示しています。
解決
さて、短い答えはそうです
$response = array();
$products = $category->getProductCollection()->addAttributeToSelect('*')->addPriceData();
foreach ($products as $product) {
$response[] = array(
'id' => $product->getId(),
'media' => $product->getMediaGalleryImages(),
'name' => $product->getName(),
'image' => $product->getImage(),
'thumbnail' => $product->getThumbnail(),
'small_image' => $product->getSmallImage(),
'short_desc' => $product->getShortDescription(),
'price' => $product->getPrice(),
'special_price' => $product->getSpecialPrice(),
'final_price' => $product->getFinalPrice(),
// and so on for all you need...
);
}
$this->getResponse()->setBody(Mage::helper('core')->jsonEncode($response));
所属していません magento.stackexchange