Insert a k2 article into joomla database with php - joomla

I want to add an K2 article to a joomla DB with php. Can I just add a record to the article-table? Or do I I need to update any other table as well?
I have some fundamental knowledge of PHP and mySQL, but I would appreciate a nudge in the right direction for the correct syntax to do this.
Many thanks,
HÃ¥kan

I am doing the same but on K2 item save (not for the already saved items in K2). So that I can duplicate the items in K2 into articles in joomla, in one click on save on the K2 item creation panel.
An "item" in K2 is an "article" in Joomla.
I am overriding core K2 using the process I have mentioned in this post-> Joomla - Overriding getItem method
Then I have found out where the K2 saves the article, as in this post-> Joomla - Where is the code in K2 which saves a new item's Title and alias
Now, my next step will be to add a piece code to the override file's (K2's models\item.php) save() function, so as to save the same K2 item into joomla com_content table as well. You will find all the info in the posts I have mentioned. If something is not clear, leave a comment so I can revert to the query.
Good Luck!

For an article in Joomla 1.6 or later you must have an entry in the asset table as well and it has to be correctly created using the methods from JTableContent.

I've done this several times in Joomla 2.5 and later. Simply insert properly structured rows into the k2_items table in your Joomla database. You'll want to make sure each new row that gets added has a properly incremented id, though. Here's an example:
$data =new stdClass();
$data->id = null;
$data->title = $title;
$temp = strtolower($data->title);
$data->alias = str_replace(' ', '-', $temp);
$data->catid = $catid;
$data->published = 1;
$data->introtext = $introtext;
$data->fulltext = $fulltext;
Do the same with all the other K2 fields (there's a bunch of them) until you finally can write the following code:
$db = JFactory::getDBO();
$db->insertObject( '#__k2_items', $data, id );

Related

Programmatically modify related products in magento

I'm trying to programmatically manipulate the product relations in a Magento store.
From what I've read, setRelatedLinkData should be the way to go.
As I simple test, I'm just trying to replace a products related products with nothing (i.e. an empty array), however it's not working - the product in question is still showing the related product in the backend.
The test code I'm working with is:
$product = Mage::getModel('catalog/product')->load($product->getId());
$linkData = array();
print_r($linkData);
$product->setRelatedLinkData($linkData);
echo "Save\n";
$r = $product->save();
As mentioned above however the product still has a related product when I reload it in the backend.
NOTE: I don't only want to remove related products, eventually I want to be able to add new ones as well, so a DELTE FROM... SQL query isn't what I am looking for. However if I can't get it to work to remove products, then it's certainly not going to work to add them, so one step at a time :-)
The quickest way I can think of is to use the Link Resource:
app/code/core/Mage/Catalog/Model/Resource/Product/Link.php saveProductLinks
// sample code
$product = Mage::getModel('catalog/product')->load(147);
$linkData = array();
Mage::getResourceModel('catalog/product_link')->saveProductLinks(
$product, $linkData, Mage_Catalog_Model_Product_Link::LINK_TYPE_RELATED
);
and if you want to assign products use the same code but provide this as $linkData:
$linkData = array(
'145' => array('position' => 1),
'146' => array('position' => 2)
);

Magento 1.7: Add Configurable Product To Cart Via Query String

The Magento Wiki has a resource for adding a product to cart via Query String for Magento < 1.3 HERE
This quotes a method using this example:
http://www.your_domain.com/checkout/cart/add?product=68&qty=1&super_attribute[528]=55&super_attribute[525]=56
It also mentions that this was valid up to version 1.3.
I have been playing around with this in 1.7 and have noticed a Major difference in 1.7 is the encrypted key in the ->getAddUrl() method for the Form Action Attribtue so now the URLs look more like
http://www.your_domain.com.au/checkout/cart/add/uenc/aHR0cDovL3d3dy5jdWx0dXJla2luZ3MuY29tLmF1L2FjY2Vzc29yaWVzL3NvbC1yZXB1YmxpYy90cmFja3Mtb24tZWFyLWJsYWNrLTM1OTg5Lmh0bWw_X19fU0lEPVU,/product/35900/
With the product ID being the 35900.
If I use this URL in the browser it will direct me to the product page with a message saying Please specify the product's option(s).
I have been trying to pass the desired attribute options value in the URL to add the product to the cart with no success. (For the sake of saving space I'm omitting the URL up to and including the encrypted key) I've tried methods these to no avail:
/product/35900/super_attribute/49265/4834
/product/35900/super_attribute/49265=4834
/product/35900/49265=4834
/product/35900/49265/4834
My question is: Is it possible to add a configurable product via URL to the cart in Magento and if so, what is the format for passing the super_attribute id and Attribute Option Value?
You can use something like this :
$_typeInstance = $_product->getTypeInstance(true);
$_children = $_typeInstance->getUsedProducts(null, $_product);
$_attributes = $_typeInstance->getUsedProductAttributes($_product);
$_cartHelper = Mage::helper('checkout/cart');
foreach ($_children as $_child) {
$_superAttributes = array();
foreach ($_attributes as $_attribute) {
$_superAttributes[$_attribute->getAttributeId()] = $_child->getData($_attribute->getAttributeCode());
}
$_addUrl = $_cartHelper->getAddUrl($_product, array(
'_query' => array(
'super_attribute' => $_superAttributes
)));
}
This question was also posted on magento.stackexchange and user Marius kindly gave me the solution...
This has worked for me on CE 1.7.0.2 (with sample data):
/checkout/cart/add/product/126?super_attribute[525]=100&super_attribute[272]=22
NOTE (this puzzles me a bit):
There is a difference between calling:
/checkout/cart/add/product/126?super_attribute[525]=100&super_attribute[272]=22
and
/checkout/cart/add/product/126?super_attribute[272]=22&super_attribute[525]=100
I mean the order of the super_attribute parameters is important. After calling the 2 URLs above I ended up with 2 cart lines of the same product with the same options. one looked like this:
Size Small Color Green
and the other was
Color Green Size Small
I guess if you add the products to cart via URL you should keep the order of the attributes as shown in the product view page for consistency.
As per his suggestion, you can build the add to cart link using that method.
In latest magento we need to add form_key also:
https://{site-name}/checkout/cart/add/product/{product_id}/form_key/{form_key}?super_attribute[{attribute_id}]={attribute_value}&super_attribute[{attribute_id}]={attribute_value}

Magento - get results view HTML for a collection of products

I get a list of magento ids from a web service. I load these into and array $product_ids, so I have something like this:
Array
(
[0] => 1965
[1] => 3371
[2] => 1052
)
I can then make this into a collection:
$collection = Mage::getModel('catalog/product')->getCollection()
->addIdFilter($product_ids);
Using my Magento inspector, I've seen that the category pages use the class Mage_Catalog_Block_Product_List to display lists of products. I'd like to do something similar in my class. I've tried loading:
$ProductList = new Mage_Catalog_Block_Product_List();
$ProductList->setCollection($collection);
And then I've tried to load the HTML of the results as follows:
$CollectionHTML = $ProductList->_toHtml();
But $CollectionHTML is empty.
How would I get the HTML of what you see in the list view (i.e. the generated output of frontend/base/default/template/catalog/product/list.phtml, but given my collection)?
Making the code work the right way is much more easier in Magento than trying to work with ugly legacy code. I would gladly help you make the code the proper way when you have specific questions. Also, in the longterm, technical debt is gonna cost alot more.
Anyway, back to your issue.
In Magento block are not instantiated like in any app $myvar = new className ... almost never. This tutorial can help you understand better Magento's layout and blocks.
But if you want to create a block a way to do it is:
$block = Mage::getSingleton('core/layout')->createBlock('catalog/product_list')
Now related to your product collection you should check how Mage_Catalog_Block_Product_List::_getProductCollection actually works, because it uses the layered navigation, not a simple product collection.
Further, assuming that at least you are using a Magento controller and you are within a function, the following code will display the first page of products for a specified category:
//$category_id needs to be set
$layout = Mage::getSingleton('core/layout');
$toolbar = $layout->createBlock('catalog/product_list_toolbar');
$block = $layout->createBlock('catalog/product_list');
$block->setChild('toolbar', $toolbar);
$block->setCategoryId($category_id);
$block->setTemplate('catalog/product/list.phtml');
$collection = $block->getLoadedProductCollection();
$toolbar->setCollection($collection);
//render block object
echo $block->renderView();
Displaying specific ids:
you use root category id for $category_id variable (also make sure that display root category is set (or another category id that contains your product ids)
you can hook into catalog_block_product_list_collection event to add your ID Filter to the collection (this is called in _beforeToHtml function)
But, all this construction is not solid and there are still some points that require attention (other child blocks, filters and so on)

Echo Specific Category Description on Magento Frontend

I want to create a page in the Magento CMS, then echo specific category descriptions on it. What is the code snippet i will need to accomplish this. I am assuming I will need to reference the categories' unique id within the database to echo their description...
thanks for the help!
john
Using PHP:
$categoryId = 15;
$category = Mage::getModel('catalog/category')->load($categoryId);
if($category->getId()) {
echo $category->getDescription(); // Should escape this, blocks have $this->escapeHtml()
}
I don't know how to do this using magentos email/cms template markup (I don't think its possible) - unless you create a block or widget.

Magento Product Insert Function

I need to add a custom option to all products as they get saved. For that I need to find the function that inserts the products into the database, which I'm not able to find.
Please, any help would be appreciated.
thanx
$client = new SoapClient('http://www.magentolocal.it/api/?wsdl');
$session = $client->login('productloader', '1234567890');
$sku = "123456";
$attrs['name'] = "Template #1";
$attrs['description'] = "This is the first template.";
$attrs['short_description'] = "This is the short description of the template";
$attrs['websites'] = array('1');
$attrs['price'] = "11.53";
$attrs['categories'] = array('35');
$attrs['images'] = array()
$result = $client->call($session, 'catalog_product.create', array('simple', '63', $sku, $attrs));
echo $result;
$client->endSession($session);
Magento's EAV system is pretty strung out among several files, so you won't find a single function that accomplishes what you want. If you did go looking for it, and changed it, you would also be changing the same save method that mostly every other object in Magento uses, which is probably not what you want.
To do what you want, try setting up an observer/listener on the events that catalog products use when saving, namely catalog_product_save_before or catalog_product_save_after. That way, you don't have to hack the framework.
Hope that helps!
Thanks,
Joe
How about http://www.magentocommerce.com/wiki/doc/webservices-api/api/catalog_product#catalog_product.create?

Resources