Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Page 1 of 2
How to embed a block from magento shop on another website
 
maciek79
Jr. Member
 
Total Posts:  15
Joined:  2007-11-20
 

Hello,

I have Magento shop installed at e.g mywebsite.com/shop
On main page of mywebiste.com (which is powered by Wordpress) I would like to embed a block with few products from certain category.
How could I achieve it programatically?

Regards,
Maciek

 
Magento Community Magento Community
Magento Community
Magento Community
 
kohril
Member
 
Total Posts:  44
Joined:  2007-12-23
Lille, northernFrance
 

Basically, I have two ideas, but maybe it is not the best way of doing, moreover I have never used wordpress.

- develop a module on your website, reading in Magento’s database
- develop a Magento’s module that returns products informations in an plugable way : an RSS feed for example. So, you may design an interface to configure that feed’s products. Then, include a (custom?) feed-burner widget in your wordpress.

 
Magento Community Magento Community
Magento Community
Magento Community
 
maciek79
Jr. Member
 
Total Posts:  15
Joined:  2007-11-20
 

Well,

I would like to know what to do (in php) in order to access magento’s core and fetch products as php arrays or php objects - avoiding direct reading from database.

Wordpress - I mentioned it just for illustration - It can be blank php page - what’s important: it must be outside of main magento path.

Regards,
Maciek

 
Magento Community Magento Community
Magento Community
Magento Community
 
harry12bar
Sr. Member
 
Avatar
Total Posts:  168
Joined:  2008-01-07
 

The easiest Basic way would be RSS… When Magento releases Ver 1 I expect it to be there.. I reckon this would be a start .

 
Magento Community Magento Community
Magento Community
Magento Community
 
zburnham
Member
 
Avatar
Total Posts:  69
Joined:  2007-11-07
Westborough, MA
 
maciek79 - 27 February 2008 04:39 AM

Well,

I would like to know what to do (in php) in order to access magento’s core and fetch products as php arrays or php objects - avoiding direct reading from database.

Wordpress - I mentioned it just for illustration - It can be blank php page - what’s important: it must be outside of main magento path.

Regards,
Maciek

How do you make those arrays/objects available to the PHP process on the other webserver?  There has to be a translation layer somewhere.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Mark_Kimsal
Sr. Member
 
Total Posts:  186
Joined:  2007-09-12
Michigan, USA
 
maciek79 - 27 February 2008 03:52 AM

Hello,

I have Magento shop installed at e.g mywebsite.com/shop
On main page of mywebiste.com (which is powered by Wordpress) I would like to embed a block with few products from certain category.
How could I achieve it programatically?

Regards,
Maciek

As with all data transfer problems you have 2 options, push or pull.

Push would involve making changes to magento, as your update your static blocks or categories, you would write out a chunk of static HTML to a spot on the drive.  The WP page can simply include this file, or transfer it across servers and include a local copy (if the WP and Mage are on different servers).

A Pull solution is probably more what you were thinking.  It would involve making a new module that would only create a catalog/category_list type block and transfer the data to your WP server.  With this type of solution you can give out all different kinds of data.  You can spit out complete HTML, as in the push solution, you can give an RSS stream and use php in WP to arrange it into HTML, you can give SOAP, or generic XML, JSON, WDDX, or whatever serialization you want.

I can pretty much guarantee you that any pull solution will not scale and you will see it as being slow after a while.  A pull with a significant cache timeout would probably be the best of both worlds since it wouldn’t involve hooking into admin events to watch for changes to categories or static blocks.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Mark_Kimsal
Sr. Member
 
Total Posts:  186
Joined:  2007-09-12
Michigan, USA
 

Here is a technical follow up to my explanation.

Create a new CMS page called “WP export” and do not link to it anywhere.

inside this page, put this in the content

{{block type="catalog/product_list" template="mystuff/wp_export/product_list.phtml" category_id="3"}}
Replace the category_id="3" with your category ID of the products that you want to spit across.

Create this file and/or directories.

app/design/frontend/[yourtheme]/default/template/mystuff/wp_export/product_list.phtml

Inside this product_list.phtml file, you can add whatever code you want to format your products as HTML.  Start with this code:

<?php
$prods 
$this->getLoadedProductCollection();
foreach (
$prods as $_prod):?>

  
<h3><?$_prod->getName();?></h3>
  <
div>
    
<?=$this->helper('review/product')->getSummaryHtml($_product);?>
  
</div>
<? endforeach;?>

On the WP-side, you can get this HTML with a network include or fsockopen like this:

include("http://mymagentoinstall/cms/wp_export.html");
That is just a guess as to which URL the CMS page would have from the first step.

You can also create your own file and activate this block in a shell magento script.  This solution is too complicated to detail here, but this can get you started.

$app Mage::app('base');
$app->getLayout()->setLayout("frontend");
$block $app->getLayout()->createBlock("catalog/product_list""wp_export", array('category_id'=>3));
$app->getLayout()->addBlock($block"wp_export");
$app->getLayout()->addOutputBlock("wp_export");
echo 
$app->getLayout()->getOutput();
 
Magento Community Magento Community
Magento Community
Magento Community
 
maciek79
Jr. Member
 
Total Posts:  15
Joined:  2007-11-20
 

Hi Mark!
Thanks for a tip.  I created new page and used iframe on second page.

You can also create your own file and activate this block in a shell magento script.  This solution is too complicated to detail here, but this can get you started. ...

Hmm. I created test.php outside of magento path:

require_once '[......]/app/Mage.php';

$app Mage::app('base');
$app->getLayout()->setLayout("frontend");
$block $app->getLayout()->createBlock("catalog/product_list""featured", array('category_id'=>3));
$app->getLayout()->addBlock($block"featured");
$app->getLayout()->addOutputBlock("featured");
echo 
$app->getLayout()->getOutput();
and I get following error:
Call to undefined method Mage_Core_Model_App::getLayout() on line 4

So, how to get Mage object working in external script?

Regards,
Maciek

 
Magento Community Magento Community
Magento Community
Magento Community
 
Hadifarnoud
Sr. Member
 
Total Posts:  210
Joined:  2007-10-07
Guildford, Surrey, UK
 

use Ajax for parsing a page and cut the exact block you want
note: you can’t use php instead because of Zend Autodetection of locale.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
                    "http://www.w3.org/TR/html4/loose.dtd"
>
<
html>
<
head>
<
link media="all" type="text/css" href="http://www.zigorat.com/skin/frontend/default/custom_theme/css/styles.css" rel="stylesheet"/>
  <
script src="http://code.jquery.com/jquery-latest.js"></script>
  
<script>
  $(
document).ready(function(){
    
$("#docs").load("/catalog/category/view/limit/all/s/notebook/id/4/ #product-list-table");
  
});
$(
"#loading").ajaxStart(function(){
   
$(this).show();
});
$(
"#loading").ajaxStop(function(){
   
$(this).hide();
}); 
</script>
<body>
<
div id="loading" class="loading">loading...</div>
<
div id="docs"></div>
</
body>
</
html>

#product-list-table is id of ur block
and #docs means you want whole block

 
Magento Community Magento Community
Magento Community
Magento Community
 
Moshe
Magento Team
 
Avatar
Total Posts:  1770
Joined:  2007-08-07
Los Angeles
 

@maciek79: try this:

require_once '[......]/app/Mage.php';

Mage::app('base');
$block Mage::getSingleton('core/layout')
    ->
createBlock("catalog/product_list""featured", array('category_id'=>3))
    ->
setTemplate("mystuff/wp_export/product_list.phtml");
echo 
$block->toHtml();
 
Magento Community Magento Community
Magento Community
Magento Community
 
maciek79
Jr. Member
 
Total Posts:  15
Joined:  2007-11-20
 

@Moshe:

After createBlock I get: “Can’t retrieve request object” error.

 
Magento Community Magento Community
Magento Community
Magento Community
 
iguru
Guru
 
Avatar
Total Posts:  331
Joined:  2007-11-01
Děčín, Czech Republic
 

Hm, i have pok.php width this code:

<?php

require_once 'app/Mage.php';

Mage::app('base');
$block Mage::getSingleton('core/layout')
    ->
createBlock("catalog/product_list""wp_export", array('category_id'=>3))
    ->
setTemplate("mystuff/wp_export/product_list.phtml");
echo 
$block->toHtml();
?>

and i got this bug:

Fatal error: Uncaught exception ‘Exception’ with message ‘Can’t retrieve request object’ in /home/mimo/Documents/www/app/code/core/Mage/Core/Block/Abstract.php:126 Stack trace: #0 /home/mimo/Documents/www/app/code/core/Mage/Core/Block/Template.php(47): Mage_Core_Block_Abstract->__construct(Array) #1 /home/mimo/Documents/www/app/code/core/Mage/Catalog/Block/Product/List.php(33): Mage_Core_Block_Template->__construct() #2 /home/mimo/Documents/www/app/code/core/Mage/Core/Model/Layout.php(345): Mage_Catalog_Block_Product_List->__construct() #3 /home/mimo/Documents/www/pok.php(7): Mage_Core_Model_Layout->createBlock(’catalog/product...’, ‘wp_export’, Array) #4 {main} thrown in /home/mimo/Documents/www/app/code/core/Mage/Core/Block/Abstract.php on line 126

 
Magento Community Magento Community
Magento Community
Magento Community
 
Moshe
Magento Team
 
Avatar
Total Posts:  1770
Joined:  2007-08-07
Los Angeles
 

That’s a bug, please edit Mage/Core/Block/Abstract.php on approx line 126 and replace

if (Mage::registry('controller')) {
    $this
->_request Mage::registry('controller')->getRequest();
with:
$controller Mage::app()->getFrontController();
if (
$controller{
    $this
->_request $controller->getRequest();

PLEASE NOTE THE UPDATE

 
Magento Community Magento Community
Magento Community
Magento Community
 
maciek79
Jr. Member
 
Total Posts:  15
Joined:  2007-11-20
 

@Moshe:
Thank you for great support. It works!

 
Magento Community Magento Community
Magento Community
Magento Community
 
iguru
Guru
 
Avatar
Total Posts:  331
Joined:  2007-11-01
Děčín, Czech Republic
 

hm hm, it’s better. I have a name of product. But:

Book 2008 (SP 60)

Notice: Undefined variable: _product in /home/mimo/Documents/www/app/design/frontend/default/default/template/mystuff/wp_export/product_list.phtml on line 7
[0] in include("/home/mimo/Documents/www/app/design/frontend/default/default/template/mystuff/wp_export/product_list") in /home/mimo/Documents/www/app/code/core/Mage/Core/Block/Template.php on line 112
[1] in Mage_Core_Block_Template->fetchView("frontend/default/default/template/mystuff/wp_export/product_list.phtml") in /home/mimo/Documents/www/app/code/core/Mage/Core/Block/Template.php on line 144
[2] in Mage_Core_Block_Template->renderView() in /home/mimo/Documents/www/app/code/core/Mage/Core/Block/Template.php on line 183
[3] in Mage_Core_Block_Template->_toHtml() in /home/mimo/Documents/www/app/code/core/Mage/Core/Block/Abstract.php on line 552
[4] in Mage_Core_Block_Abstract->toHtml() in /home/mimo/Documents/www/pok.php on line 9

Fatal error: Call to a member function getRatingSummary() on a non-object in /home/mimo/Documents/www/app/code/core/Mage/Review/Helper/Product.php on line 35

The Book is a simply product without special attributes (Default product).

 
Magento Community Magento Community
Magento Community
Magento Community
 
iguru
Guru
 
Avatar
Total Posts:  331
Joined:  2007-11-01
Děčín, Czech Republic
 

Sorry, is my bug.
When i change code from Mark_Kimsal

<?php
$prods 
$this->getLoadedProductCollection();
foreach (
$prods as $_prod):?>

  
<h3><?$_prod->getName();?></h3>
  <
div>
    
<?=$this->helper('review/product')->getSummaryHtml($_product);?>
  
</div>
<? endforeach;?>
to
<?php
$prods 
$this->getLoadedProductCollection();
foreach (
$prods as $_prod):?>

  
<h3><?$_prod->getName();?></h3>
  <
div>
    
<?=$this->helper('review/product')->getSummaryHtml($_prod);?>
  
</div>
<? endforeach;?>
it’s work perfect. Sorry for my inattention.
 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top
Page 1 of 2