Outside link for Add to Cart - magento

We want to get an outside link for a Magento-store page, that would add to cart an item that's linked from a PDF link (it's a technical drawing with some parts that can be ordered separately)
I see that Magento uses JavaScript onclick="productAddToCartForm.submit(this)", but it can not be triggered to the specific item like this.
Is there any way this can be solved?
BR-:g

This is the basic url to call:
www.example.com/checkout/cart/add?product=[id]&qty=[qty]
If you want more details on how to do it with options etc. take a look here:
http://www.magentocommerce.com/wiki/4_-_themes_and_template_customization/catalog/adding_a_product_to_the_cart_via_querystring

You can give add to cart url like:
<?php echo $this->helper('checkout/cart')->getAddUrl($_product);?>

You can always loop over your product collection and ask the checkout/cart helper for the url:
$collection = Mage::getResourceModel('catalog/product_collection');
/* #var $collection Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection */
//... add filters to collection as appropriate
$cartHelper = Mage::helper('checkout/cart');
/* #var $cartHelper Mage_Checkout_Helper_Cart */
foreach( $collection as $product ){
/* #var $product Mage_Catalog_Model_Product */
$atcUrl = $carthelper->getAddUrl($product);
//... do what you need to with the above value (echo, fwrite, etc);
}
Note that you can also pass in product-type-specific options as a second param.

Related

get check out cart attributes using magento

I was successfully getting the all the cart details(i.e total items, items in the cart, etc).
But i want only specific attributes from cart.
suppose i want to get store_id attribute, how can i achieve.
public function info($quoteId, $store = null)
{
$quote = $this->_getQuote($quoteId, $store);
$result_attr = $this->_getAttributes($quote, 'quote');
//this is giving error
$result_attr->getAttributeText('store_id');
}
if you are getting object in your $quote
you can get with below code
if($quote )
$store = $quote->getStoreId();
hope this will sure help you.

Magento add to cart object doesn't contain custom options

I'm using SCP with success, I don't think the problem is there. Basically I've got an observer that looks for the "Add to cart" event and goes from there. Here's my observer method:
public function catalogProductLoadAfter(Varien_Event_Observer $observer)
{
// set the additional options on the product
$action = Mage::app()->getFrontController()->getAction();
if ($action->getFullActionName() == 'checkout_cart_add') {
// assuming you are posting your custom form values in an array called extra_options...
if ($options = $action->getRequest()->getParam('extra_options')) {
$product = $observer->getProduct();
// add to the additional options array
$additionalOptions = array();
if ($additionalOption = $product->getCustomOption('additional_options')) {
$additionalOptions = (array)unserialize($additionalOption->getValue());
}
foreach ($options as $key => $value) {
$additionalOptions[] = array(
'label' => $key,
'value' => $value,
'value' => $value,
);
}
// add the additional options array with the option code additional_options
$observer->getProduct()->addCustomOption('additional_options', serialize($additionalOptions));
}
}
}
All looks well and functions just fine. I've dropped in some Zend_Debug::dump statements at various points and have found where I think the issue is. $product doesn't contain any custom options, or at least doesn't appear to! I've done Zend_Debug::dump($product); and this gives me the following: https://gist.github.com/720a111bc299501726d7 The important thing to see here is that the product object shown is a child product of the configurable. ALL child products have custom options (I just had to set them to get to this stage!).
In the cart page the custom options are displayed correctly, as I've just set them. So why at this midpoint when I do Zend_Debug::dump($product); just before the foreach does the above gist not show any custom options, specifically line 9. My observer fails to do it's job because $additionalOptions ends up being blank, just displays as array {}. As such the foreach doesn't fire and the script falls over. So why are no custom options shown in the gist, yet they ARE there as they show on the product page AND they show after this script executes on the cart page?
To further "prove" this, I'm getting an Invalid argument for foreach() as a result.

Update Item Product Options After Order Placed

I'm working on an observer that needs to add (a) serial key(s) to each item in the cart once an order is placed.
I'm listening to the event sales_model_service_quote_submit_success right now.
I've been able to access the order, get a list of the items, iterate through them, and get the product options. My code fails either when I try to setProductOptions or save--I'm not sure which, maybe it's both.
Here is the relevant code:
// Get access to order information
$lastOrderId = Mage::getSingleton('checkout/session')->getLastOrderId();
$order = Mage::getModel('sales/order')->load($lastOrderId);
// Get the items from the order
$items = $order->getAllItems();
foreach ($items as $item)
{
// Pretend here is the call that fetches the serial keys for this item and stores them in $keyString
// If we actually received the keys in a string, store them with the item
if (!empty($keyString))
{
$productOptions = array();
if (count($item->getProductOptions()))
{
$productOptions = $item->getProductOptions();
}
$productOptions['keys'] = $keyString;
$item->setProductOptions($productOptions);
$item->save();
}
}
Any ideas what I have forgotten or done wrong? Thanks a bunch.
no such observer , at least i didn't find it from codebase, here's what you can use
Mage::dispatchEvent('sales_model_service_quote_submit_before', array('order'=>$order, 'quote'=>$quote));
Mage::dispatchEvent('sales_model_service_quote_submit_after', array('order'=>$order, 'quote'=>$quote));
and in your observer method
/**
*
* #param Varien_Event_Observer $observer
*
*/
public function setShippingDefaults(Varien_Event_Observer $observer) {
$order = $observer->getEvent()->getOrder();
}
the idea is that if you do it in before-action then you won't need to call save and if you do it in after-action then you do and if you do it in before action you might just end in endless loop if you are not careful.

Magento Limit Number of Products in Home page

I have added this code {{block type="catalog/product_list" category_id="25" template="catalog/product/list.phtml"}} in cms home page
I want to limit no of products to display to nine to this category only.How can i do that?
I don't think there is a value you can pass into the block tag to limit it. I would suggest making a new list.phtml file that limits it there.
Let me look at the code real quick.
Ok. If you were to copy the file /app/design/frontend/default/default/template/catalog/product/list.phtml
to
/app/design/frontend/default/default/template/catalog/product/list-limit.phtml
and then edit it as follows:
LINE49: After the foreach
<?php if($_iterator >=9) { break; } ?>
LINE94: Where $_collectionSize is assigned change to:
<?php $_collectionSize = main(9, $_productCollection->count()) ?>
Line97: After the foreach
<?php if($i >= 9) { break; } ?>
It should achieve what you desire regardless of Grid or List view.
... shortly an alternative method ...
The other way would be to edit the List.php file that loads the product list that the phtml file presents. Block Type of 'catalog/product_list' means you need the file:
/app/code/core/Mage/Catalog/Block/Product/List.php
In there you will see the method getLoadedProductCollection, which calls _getProductCollection. That code could be edited to filter/limit the number of returned products. You would want to make a copy of that file though, and update the block link in your page. Don't add underscores to the name, as that will require the file be put in a subdirectory.
Hope this helped.
Following on from the previous answer, I seem to have acheived this by editing the List.php by adding the following after line 96.
return $this->_productCollection
->setPageSize($this->getProductsCount());
}
/**
* Set how much product should be displayed at once.
*
* #param $count
* #return Mage_Catalog_Block_Product_New
*/
public function setProductsCount($count)
{
$this->_productsCount = $count;
return $this;
}
/**
* Get how much products should be displayed at once.
*
* #return int
*/
public function getProductsCount()
{
if (null === $this->_productsCount) {
$this->_productsCount = self::DEFAULT_PRODUCTS_COUNT;
}
return $this->_productsCount;
}
and adding this after line 43
/**
* Default value for products count that will be shown
*/
const DEFAULT_PRODUCTS_COUNT = 100;
/**
* Products count
*
* #var null
*/
protected $_productsCount;
I got the codes from new.php

Magento: how to get the attributes that belong to an attribute set?

Having an attribute set, how can I get a list of the attributes it contains (or better yet, just the custom attributes that don't belong to the Default attribute set)?
The attribute set itself can be obtained in several ways, such as:
$entityTypeId = Mage::getModel('eav/entity')->setType('catalog_product')->getTypeId();
$attributeSet = Mage::getResourceModel('eav/entity_attribute_set_collection')->setEntityTypeFilter($entityTypeId)->addFilter('attribute_set_name', 'Default');
Note that I need to use the attribute set, so getting the list of attributes from a product is not the solution I am looking for.
Mage::getModel('catalog/product_attribute_set_api')->items();
Gets the attribute sets themselves.
Mage::getModel('catalog/product_attribute_api')->items($setId);
Gets the attributes inside the attribute sets.
I believe the answer lies in this model
Mage::getModel('catalog/product_attribute_set_api')->items($setId);
The class is Mage_Catalog_Model_Product_Attribute_Api it seems to have two methods. The items() methods seems to do what you ask i.e. "Retrieve attributes from specified attribute set"
I hope that helps :)
The right way:
$attributes = Mage::getResourceModel('catalog/product_attribute_collection')
->setAttributeSetFilter($attributeSetId)
->getItems();
var_dump($attributes);
You can change resources 'catalog/product_attribute_collection' (customer, ...)
And Set ID $attributeSetId
Regarding Attribute Set, there is a good collection of code snippets in the following blog article:
http://www.blog.magepsycho.com/playing-with-attribute-set-in-magento/
Hope you will find them useful.
Thanks
You don't necessarily need to access the API class. There is a more natural approach available. If you have a product:
/** #var Mage_Catalog_Model_Product $product **/
$attributes = $product->getTypeInstance(true)->getSetAttributes($product);
If not:
$attributes = Mage::getModel('catalog/product')->getResource()
->loadAllAttributes()
->getSortedAttributes($attributeSetId);
I've been searching for an answer, I could'nt find it and I solve my problem with these lines;
$attributeSetId = /* set id */;
$attributes = array();
$groups = Mage::getModel('eav/entity_attribute_group')
->getResourceCollection()
->setAttributeSetFilter($attributeSetId)
->setSortOrder()
->load();
foreach ($groups as $node) {
$nodeChildren = Mage::getResourceModel('catalog/product_attribute_collection')
->setAttributeGroupFilter($node->getId())
//->addFieldToFilter('is_user_defined', true) # I was trying to get user defined attributes.
->addVisibleFilter()
->load();
if ($nodeChildren->getSize() > 0) {
foreach ($nodeChildren->getItems() as $child) {
$attr = array(
'id' => $child->getAttributeId(),
'text' => $child->getAttributeCode()
);
$attributes[] = $attr;
}
}
}
var_dump($attributes);

Resources