I am trying to get all product thumbs for a category product collection like that
$category = Mage::getModel('catalog/category')->load($cat_id);
$products = Mage::getResourceModel('catalog/product_collection')
->setStoreId(1)
->setPageSize(10)
->addAttributeToFilter(
'status', array('eq' => Mage_Catalog_Model_Product_Status::STATUS_ENABLED)
)
->addAttributeToFilter('visibility', Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)
->addCategoryFilter($category);
$productThumbs = array();
try {
foreach ($products as $product) {
$thumbUrl = $product->getThumbnailUrl(200, 60);
$productThumbs[] = array(
'entity_id' => $product->getId(),
'thumb_url' => $thumbUrl
);
}
} catch (Exception $e) {
error_log($e->getMessage());
}
It always return placeholder image urls like that...
http://magentohost/magento/media/catalog/product/cache/0/thumbnail/200x60/9df78eab33525d08d6e5fb8d27136e95/images/catalog/product/placeholder/thumbnail.jpg
Don't know what I am doing wrong... Please guide.
I found that we need to load the $product before getting its thumb, like this
$product= Mage::getModel('catalog/product')->load($prod_id);
and then we can retrieve its thumb using
$thumbUrl = $product->getThumbnailUrl(200, 60);
Related
I am trying to Add Products to Cart with respect to customer (programmatically) but getting error "Invalid request for adding product to quote". I have both Simple products (with custom options) and configurable products. Below is my code. Please help. Many thanks in advance.
public function addtocartAction(){
try {
$cusId = $this->getRequest()->getParam('cusId');
$customer = Mage::getModel('customer/customer')->load($cusId);
$quote = Mage::getModel('sales/quote')->loadByCustomer($customer);
$quoteId = $quote->getId();
//$products = $this->getRequest()->getParam('products');
$products = json_decode('[{"proId": "906","proQty": "1", "options":{"17":"wq","16":"18"}}]');
foreach($products as $product) {
/*if (!$product->getId()) {
throw new Exception();
}*/
foreach ($product->options as $optKey => $optValue) {
$optAll[$optKey] = $optValue;
}
$mainProduct = Mage::getModel('catalog/product')->load($product->proId);
$params = array(
'product' => $product->proId,
'qty' => $product->proQty,
'options' => $optAll
);
echo "<pre />"; print_r($params);
$quote->addProduct($mainProduct, $params);
$quote->setIsActive(1);
$quote->collectTotals()->save();
}
$rslt['success'] = '1';
$rslt['message'] = 'Product has been succefully added to cart';
}
catch(Exception $e){
$rslt['success'] = '0';
$rslt['message'] = $e->getMessage();
}
print_r(json_encode($rslt));
}
Try using cart instead of quote.
This works for me:
$cart = Mage::getModel('checkout/cart');
$mainProduct = Mage::getModel('catalog/product')->load($product->proId);
$params = array(
'product' => $product->proId,
'qty' => $product->proQty,
'options' => $optAll
);
$cart->init();
$cart->addProduct($mainProduct, $params);
$cart->save();
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
I need to sort region drop down in one page checkout page. I found data coming from 'Mage_Directory_Helper_Data' I need to sort this data. I tried by adding below code:
$collection = Mage::getModel('directory/region')->getResourceCollection()
->addCountryFilter($countryIds)
->addOrder('default_name', 'DESC')
->load();
But it did not work. Can anyone please help me. Thank You.
Try
if (empty($json)) {
$countryIds = array();
foreach ($this->getCountryCollection() as $country) {
$countryIds[] = $country->getCountryId();
}
$collection = Mage::getModel('directory/region')->getResourceCollection()
->addCountryFilter($countryIds)
->setOrder('default_name','DESC')
->load();
$regions = array(
'config' => array(
'show_all_regions' => $this->getShowNonRequiredState(),
'regions_required' => $this->getCountriesWithStatesRequired()
)
);
foreach ($collection as $region) {
if (!$region->getRegionId()) {
continue;
}
$regions[$region->getCountryId()][$region->getRegionId()] = array(
'code' => $region->getCode(),
'name' => $this->__($region->getName())
);
}
krsort($regions); // or ksort($regions)
$json = Mage::helper('core')->jsonEncode($regions);
}
How to add the child product to cart programmatically?
I've a child product SKU, I want to
Fetch the supper attributes like size and color id and values from the products,
Then fetch the parent product id,
Pass the super attributes on param with quantity, to add to function cart.
//$cart->addProduct($product1, $options);
Here, how to pass the supper attributes on the $option variable? Please help me!!!!
Try this. I think it may need some improvements but it works good in simple cases.
$_product = $this->getProduct();
$parentIds = Mage::getResourceSingleton('catalog/product_type_configurable')->getParentIdsByChild($_product->getId());
// check if something is returned
if (!empty(array_filter($parentIds))) {
$pid = $parentIds[0];
// Collect options applicable to the configurable product
$configurableProduct = Mage::getModel('catalog/product')->load($pid);
$productAttributeOptions = $configurableProduct->getTypeInstance(true)->getConfigurableAttributesAsArray($configurableProduct);
$options = array();
foreach ($productAttributeOptions as $productAttribute) {
$allValues = array_column($productAttribute['values'], 'value_index');
$currentProductValue = $_product->getData($productAttribute['attribute_code']);
if (in_array($currentProductValue, $allValues)) {
$options[$productAttribute['attribute_id']] = $currentProductValue;
}
}
// Get cart instance
$cart = Mage::getSingleton('checkout/cart');
$cart->init();
// Add a product with custom options
$params = array(
'product' => $configurableProduct->getId(),
'qty' => 1,
'super_attribute' => $options
);
$request = new Varien_Object();
$request->setData($params);
$cart->addProduct($configurableProduct, $request);
$session = Mage::getSingleton('customer/session');
$session->setCartWasUpdated(true);
$cart->save();
}
U can try.
$cart = Mage::getModel('checkout/cart');
$cart->init();
$productCollection = Mage::getModel('catalog/product')->load($productId);
$cart->addProduct($productCollection ,
array( 'product_id' => $productId,
'qty' => 1,
'options' => array( $optionId => $optionValue));
$cart->save();
I am using following code to set the base image as the small image of the product.
Before running the code I have following information
---
----
[image] => /s/a/santat.jpg
[small_image] =>
[thumbnail] => /s/a/santa-300.jpg
---
---
<?php
require 'app/Mage.php';
Mage::app();
$products = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('*')
->addAttributeToFilter( 'sku', array( 'in' => array( '01Santa') ) );
foreach ($products as $product)
{
if (!$product->hasImage()) continue;
if (!$product->hasSmallImage())
{
try{
$product->setSmallImage($product->getImage());
}
catch(Exception $e)
{
echo $e->getMessage();
}
}
$product->save();
}
?>
Once the code has run I have following if I print
print_r($product);
[image] => /s/a/santa.jpg
[small_image] => /s/a/santa.jpg
[thumbnail] => /s/a/santa-300.jpg
But still in admin no image is set as small image why?(I am using multi website setup)
You need to change code
$product = Mage::getModel('catalog/product')->load($id);
$mediaGallery = $product->getMediaGallery();
if (isset($mediaGallery['images'])){
foreach ($mediaGallery['images'] as $image){
Mage::getSingleton('catalog/product_action')->updateAttributes(array($product->getId()), array('small_image'=>$image['file']), 0);
break;
}
}
<?php
require 'app/Mage.php';
Mage::app();
$products = Mage::getModel('catalog/product')->getCollection()
->addAttributeToFilter( 'sku', array( 'in' => array("004XmasVacGrnShtrs") ) );
foreach($products as $product)
{
$productId = $product->getId();
echo "<br>";
//load the product
$product = Mage::getModel('catalog/product')->setStoreId("14")->load($productId);
//get all images
$image = $product->getImage(); //Get product base image
Mage::getSingleton('catalog/product_action')->updateAttributes(array($product->getId()), array('small_image'=>$image), 14); //14 is store id
}
echo "done";
?>
I am creating web service for my store. I am using magento API to collect product list from store. But it display all the 500 records. And i want it 25 records per page. What to add in API call? Or What filter will be apply for this?
//create soap object
$proxy = new SoapClient('http://localhsot/magento/api/soap/?wsdl');
// create authorized session id using api user name and api key
// $sessionId = $proxy->login('apiUser', 'apiKey');
$sessionId = $proxy->login('test_admin', '12345678');
$filters = array(
);
// Get list of product
$productlist = $proxy->call($sessionId, 'product.list', array($filters));
print_r($productlist );
that didn't work, can you limit it by id like:
$filters = array('id'=>array("gteq"=>1), 'id'=>array("lteq"=>1000));
that way you could add some paging?
I know this is an old question but I struggled with this. I've created my own SOAP API endpoint which does exactly the same as the default catalogProductList function but has an extra param. See the code below:
$collection = Mage::getModel('catalog/product')->getCollection()
->addStoreFilter($this->_getStoreId($store))
->addAttributeToSelect('name');
if($limit) {
$collection->setOrder('updated_at', 'ASC');
$collection->setPageSize($limit);
}
/** #var $apiHelper Mage_Api_Helper_Data */
$apiHelper = Mage::helper('api');
$filters = $apiHelper->parseFilters($filters, $this->_filtersMap);
try {
foreach ($filters as $field => $value) {
$collection->addFieldToFilter($field, $value);
}
} catch (Mage_Core_Exception $e) {
$this->_fault('filters_invalid', $e->getMessage());
}
$result = array();
foreach ($collection as $product) {
$result[] = array(
'product_id' => $product->getId(),
'sku' => $product->getSku(),
'name' => $product->getName(),
'set' => $product->getAttributeSetId(),
'type' => $product->getTypeId(),
'category_ids' => $product->getCategoryIds(),
'website_ids' => $product->getWebsiteIds(),
'updated_at' => $product->getUpdatedAt(),
'created_at' => $product->getCreatedAt()
);
}
return $result;
And from there we keep track of the last updated_at value and use that as a filter to get the next [LIMIT] items. By default updated_at and created_at are not in the response and the list is not orderred by updated_at so I've added this.
I am not sure of this but you could try this, as the API is working with collections and this is the way to limit a collection size
$filters = array(
'setPageSize' => 10
);