Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating several blocks via Ajax #14

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ DEMO: http://staging.e-tailors.nl/magentodemo/121-170-ladies-wallet-chamonix.htm

This Module adds an Ajax event to the new ColorSwatches in Magento 1.9.1.0
When the event ConfigurableMediaImages.updateImage fires up, the original updateImage() function is executed.
After this we make an AJAX request to [baseurl]+'ajaxswatches/ajax/update' requesting the MediaGallery images.
After this we make an AJAX request to [baseurl]+'ajaxswatches/ajax/update' requesting the MediaGallery images and specified blocks.

The ID used to fetch the MediaGallery uses the original code from the RWD theme:

Expand All @@ -57,6 +57,21 @@ The ID used to fetch the MediaGallery uses the original code from the RWD theme:

Once we've retrieved the new MediaGallery images, we remove the old thumbs and large images (for the sake of memory consumption). We might argue that it would be better to keep the downloaded images, but I chose to remove them. Second time you load the images it *should* come from browser-cache anyway.

In your layout xml file you can specify which blocks should be updated when switching:

<ajaxswatches_ajax_update>
<block name="updateBlocks" type="core/text_list">
<action method="setBlocks">
<blocks>
<product_info_tabs>
<selector>#product-tabs</selector> <!-- HTML element id/class selector of block to be updated -->
<name>product.info.tabs</name> <!-- Layout update block name (same as in XML) -->
</product_info_tabs>
</blocks>
</action>
</block>
</ajaxswatches_ajax_update>

The whole code is pretty simple and does not touch any default code. All extra images are loaded after clicking the ColorSwatches, so no extra load on page-render.

### 3. Sort attributes by admin position and hide out of stock products if set from admin
Expand Down
215 changes: 118 additions & 97 deletions app/code/community/Wigman/AjaxSwatches/controllers/AjaxController.php
Original file line number Diff line number Diff line change
@@ -1,99 +1,120 @@
<?php
class Wigman_AjaxSwatches_AjaxController extends Mage_Core_Controller_Front_Action {

public function updateAction(){


if(!isset($_REQUEST['pid'])) { exit; }

$pid = $_REQUEST['pid'];

$_product = Mage::getModel('catalog/product')->load($pid);
//get Product


$mediaImages= $_product->getMediaGalleryImages();

$images = array();
$i=1;
foreach ($mediaImages as $_image){
//var_dump($image);
if(!$_image['disabled_default']){

//echo Mage::helper('catalog/image')->init($_product, 'thumbnail', $_image->getFile())->resize(75);
$newImage = array(
'thumb' => (string) Mage::helper('catalog/image')->init($_product, 'thumbnail', $_image->getFile())->resize(75),
'image' => (string) Mage::helper('catalog/image')->init($_product, 'image', $_image->getFile())
);


$images[$i] = $newImage;
$i++;
} else {
//echo 'image disabled';
}

}


$this->getResponse()->clearHeaders()->setHeader('Content-type','application/json',true);
$this->getResponse()->setBody(Mage::helper('core')->jsonEncode($images));
return;

//return $images;

}

public function getlistdataAction(){

if(!isset($_REQUEST['pids'])) { exit; }

if (!Mage::helper('configurableswatches')->isEnabled()) { // check if functionality disabled
exit; // exit without loading swatch functionality
}

$pids = explode(',',$_REQUEST['pids']);

$response = $swatches = $jsons = array();
$this->loadLayout();

$viewMode = (isset($_REQUEST['viewMode']))? $_REQUEST['viewMode'] : 'grid';
$keepFrame = ($viewMode == 'grid')? true : false;

foreach($pids as $pid){

Mage::unregister('catViewKeepFrame');
Mage::register('catViewKeepFrame',$keepFrame);


$swatches[] = array('id' => $pid, 'value' => $this->getLayout()
->createBlock('Wigman_AjaxSwatches/swatchlist','swatchlist-'.$pid)
->setPid($pid)
->setViewMode($viewMode)
->setTemplate('configurableswatches/catalog/product/list/swatches.phtml')
->toHtml());

$productsCollection = $this->getLayout()->getBlock('swatchlist-'.$pid)->getCollection();

//Mage::log($productsCollection);

$jsons[$pid] = $this->getLayout()
->createBlock('Wigman_AjaxSwatches/catalog_media_js_list','mediajslist-'.$pid)
->setPid($pid)
->setViewMode($viewMode)
->setProductCollection($productsCollection)
->setTemplate('wigman/ajaxswatches/media/js.phtml')
->toHtml();

}

$response['swatches'] = $swatches;

$response['jsons'] = $jsons;

$this->getResponse()->clearHeaders()->setHeader('Content-type','application/json',true);
$this->getResponse()->setBody(Mage::helper('core')->jsonEncode($response));
return;
}
<?php

class Wigman_AjaxSwatches_AjaxController extends Mage_Core_Controller_Front_Action
{

public function updateAction()
{
$pid = $this->getRequest()->getParam('pid', null);
if (is_null($pid)) {
return;
}

// get Product
$_product = Mage::getModel('catalog/product')->load($pid);

// register product otherwise each child template won't work
Mage::unregister('product');
Mage::register('product', $_product);

// load layout for catalog product view page
$this->loadLayout('catalog_product_view');
$layout = $this->getLayout();
$response = array();

// get blocks which should be updated
$updateBlocks = $layout->getBlock('updateBlocks')->getData('blocks');
$_blockHtml = array();

if($updateBlocks) {
foreach ($updateBlocks as $key => $blockInfo) {
$block = $layout->getBlock($blockInfo['name']);
if($block) {
$_blockHtml[] = array(
'key' => $blockInfo['selector'],
'value' => $block->setProduct($_product)->toHtml()
);
}
}
$response['update_blocks'] = $_blockHtml;
}

$mediaImages= $_product->getMediaGalleryImages();
if($mediaImages->count()) {
$images = array();
$i=1;
foreach ($mediaImages as $_image){
if(!$_image['disabled_default']){

$newImage = array(
'thumb' => (string) Mage::helper('catalog/image')->init($_product, 'thumbnail', $_image->getFile())->resize(75),
'image' => (string) Mage::helper('catalog/image')->init($_product, 'image', $_image->getFile())
);

$images[$i] = $newImage;
$i++;
}
}
$response['media_images'] = $images;
}

$this->getResponse()->clearHeaders()->setHeader('Content-type', 'application/json', true);
return $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($response));
}

public function getlistdataAction()
{

if (!isset($_REQUEST['pids'])) {
exit;
}

if (!Mage::helper('configurableswatches')->isEnabled()) { // check if functionality disabled
exit; // exit without loading swatch functionality
}

$pids = explode(',', $_REQUEST['pids']);

$response = $swatches = $jsons = array();
$this->loadLayout();

$viewMode = (isset($_REQUEST['viewMode'])) ? $_REQUEST['viewMode'] : 'grid';
$keepFrame = ($viewMode == 'grid') ? true : false;

foreach ($pids as $pid) {

Mage::unregister('catViewKeepFrame');
Mage::register('catViewKeepFrame', $keepFrame);


$swatches[] = array('id' => $pid, 'value' => $this->getLayout()
->createBlock('Wigman_AjaxSwatches/swatchlist', 'swatchlist-' . $pid)
->setPid($pid)
->setViewMode($viewMode)
->setTemplate('configurableswatches/catalog/product/list/swatches.phtml')
->toHtml());

$productsCollection = $this->getLayout()->getBlock('swatchlist-' . $pid)->getCollection();

//Mage::log($productsCollection);

$jsons[$pid] = $this->getLayout()
->createBlock('Wigman_AjaxSwatches/catalog_media_js_list', 'mediajslist-' . $pid)
->setPid($pid)
->setViewMode($viewMode)
->setProductCollection($productsCollection)
->setTemplate('wigman/ajaxswatches/media/js.phtml')
->toHtml();

}

$response['swatches'] = $swatches;

$response['jsons'] = $jsons;

$this->getResponse()->clearHeaders()->setHeader('Content-type', 'application/json', true);
$this->getResponse()->setBody(Mage::helper('core')->jsonEncode($response));
return;
}

}
Loading