magento tracking pixel - magento

There are two variables which are required in the tracking pixel which needs to be placed on category, product info, cart and confirmation page.
I've managed to get the Prod list and Prod working, however, the second two are causing me problems.
I can echo out the sku in the cart, however, the products are configurable products so it's duplicating the sku in the output. The code I'm using is below:
<?php
// $items = Mage::getModel('checkout/cart')->getQuote()->getAllItems();
$items = Mage::getSingleton('checkout/session')->getQuote()->getAllItems();foreach($items as $item) { echo ''.$item->getSku().' ';}
?>
How do i change this to just display the one configurable SKU?
The second element is the Category name that the product exists in, Any one got any ideas on that? I've tried multiple variations but they've either broke the page or returned nothing.
Any help would be appreciated. If someone could also give me examples of how these would work on the confirmation page as well, that would be great.
Thanks for your help.

Check for the products visibility (simple products attached to a configurable would not be visible):
$items = Mage::getSingleton('checkout/session')->getQuote()->getAllItems();
foreach($items as $item) {
if ($item->getProduct()->isVisibleInSiteVisibility()) {
echo ''.$item->getSku().' ';
}
}
With regards to the category name, a product can appear in multiple categories so im not sure how you want to handle that. Also, there is a concern that you are beginning to duplicate code across several template files. You will want to consider moving this all out to a block.
Anyway, to get the category names that the product belongs to here is at least one method of doing this...
$categoryCollection = $item->getProduct()->getCategoryCollection()
->addAttributeToSelect('name');
foreach($categoryCollection as $category) {
echo $category->getData('name') . "<br/>";
}

Related

How to change atribute value for all products in one category

let me first explain my problem, I need to change my atribute for free shipping for all the products in a single category. I know how to read atribute value, because I am displaying banner when a product has a free shipping atribute.
Now, what if I have to set these atribute values for all products in a single category?
What will be the best way to achieve that?
It would be very usefull if, I could change values from backend.
I have found, that you can add an atribute for a category, but sometimes, those atributes won't be the same.
I am using Magento 1.9.2
Thank you!
It's not possible to make filter products by category n admin panel. Simple script will make this
<?php
require 'app/Mage.php';
$products = Mage::getModel('catalog/category')->load($category_id)
->getProductCollection()
->addAttributeToSelect('*') // add all attributes - optional
->addAttributeToFilter('status', 1) // enabled
->addAttributeToFilter('visibility', 4); //visibility in catalog,search
foreach($products as $product) {
$product->setAttribute('new value');
$product->save();
}
Just create new php file, put it in main Magento diroctory and run by cli or url.

Hide (configurable) products with attribute

I'm working on a Magento website, and what I would like to do is the following: I created an attribute to hide certain products. On a grid view page, I'm using the following code to exclude them from the list:
<?php if ($_product->getAttributeText('hideproduct')):?>
<?php else: ?>
Basically, it's just saying that when 'hideproduct' shows up, don't show anything.
This works for simple products, but for configurable products, it's a bit more complex, and it doens't seem to work with this. Let's say that I want to hide a product with a certain color, it always keeps appearing in the dropdown menu of the configurable product.
Does anyone have a solution for this?
This is what i did for one of my task (if there is a better way please let me know)
You have to extend Mage_Catalog_Block_Product_View_Type_Configurable for this purpose.
In it
public function getAllowProducts()
{
if (!$this->hasAllowProducts()) {
$products = array();
$allProducts = $this->getProduct()->getTypeInstance(true)
->getUsedProducts(null, $this->getProduct());
foreach ($allProducts as $product) {
if ($product->isSaleable()) {
if(!$product->getEcoReport())
{
$products[] = $product;
}
}
}
$this->setAllowProducts($products);
}
return $this->getData('allow_products');
}
eco_report is my attribute label.
So this is how it works... if for a simple product ( of a particular configurable product) if attribute eco_report is set then that product wont show in the configurable product's drop down list (on view page).
So all simple product's eco_report attribute has to be set so that it wont be shown on the configurable product's dropdown...

Magento: How to retrieve the minimal price of (i.e.) grouped products?

I am trying to display a grouped product's price on the product view page in Magento 1.7.0.2, just as it is being displayed in the category product listing ("Starting at: xx.xx"). I thought I could just use
$this->getPriceHtml($_product, true);
to do so because it works the same way in the category product listing, but as everything I tried to do in Magento, it is not that easy because that method doesnt return anything on the product view page.
I looked a little deeper into Magento's code and figured out that the cause of this problem is the product's minimal price data not being set on the product view page ($_product->getMinimalPrice() returns null).
Therefore I did some research on how to load a product's minimal price which brought up ideas like
$_product->getPriceModel()->getMinimalPrice($_product);
which doesnt work because apparently that method was deprecated and removed in one of the last updates or
$priceModel = Mage::getResourceModel('catalogindex/price');
$priceModel->setStoreId(Mage::app()->getStore()->getId());
$priceModel->setCustomerGroupId(Mage::getSingleton('customer/session')->getCustomerGroupId());
$minimalPrices = $priceModel->getMinimalPrices(array($_product->getId()));
$minimalPrice = $minimalPrices[0];
$_product->setData('minimal_price', $minimalPrice['value']);
$_product->setData('minimal_tax_class_id', $minimalPrice['tax_class_id']);
which doesnt work either because the table 'catalogindex_minimal_price' is empty.
So my question is, how does Magento load the minimal price in the category product listing?
I looked a little deeper into Magento's code and tracked down how Magento loads the products of the product list.
When viewing the product list, the product collection is loaded by the model Mage_Catalog_Model_Layer, which adds the minimal price and other stuff to the products in the collection by adding joins to the collection’s SQL, but I have not yet found a model that does the same thing for single products instead of collections.
Because I don’t want to use a direct SQL query, I checked out the CatalogIndex module, but that module seems not to be working at all because all its indexing tables are empty or even corrupted.
$data_grouped = Mage::getModel("catalogindex/data_grouped");
$minimal = $data_grouped->getMinimalPrice(array($_product->getId()), Mage::app()->getStore());
looked good at first but it gives me the following error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'catalog_product_index_price.value' in 'field list'
The CatalogIndex module is either deprecated or I’m just too stupid to enable it.
However, I have now created a workaround in my price.phtml template, which uses the same method to retrieve the minimal price and tax percents as the product list does:
At the beginning of the template I added the following:
$this->setProduct(
Mage::getModel("catalog/product")->getCollection()
->addAttributeToSelect(Mage::getSingleton("catalog/config")->getProductAttributes())
->addAttributeToFilter("entity_id", $this->getProduct()->getId())
->setPage(1, 1)
->addMinimalPrice()
->addFinalPrice()
->addTaxPercents()
->load()
->getFirstItem()
);
which may not be the perfect solution, but it does the job and it does it a little cleaner than iterating over all child products and finding the minimal price that way.
This isn't an answer as much as a big HTML comment on Subsurf's answer. If you're reading this, you might be implementing your own custom module to display a product list. In my case, it's a single page just for wholesalers. My goal was to get a list of my own company's products and give them a list like it was another category. I copied my template's list.phtml to my new content.phtml. But getMinimalPrice() was returning NULL, meaning that when this code calls the getPriceHtml(), price.phtml wasn't showing wholesale pricing.
I did a simple module with index controller and a content.phtml. Using the boilerplate I see all over the net, in the indexController.php file, change:
$block = $this->getLayout()->createBlock(
'Mage_Core_Block_Template',
'b2b',
array('template' => 'b2b/content.phtml')
);
to:
$block = $this->getLayout()->createBlock(
'Mage_Catalog_Block_Product_List',
'b2b',
array('template' => 'b2b/content.phtml')
);
This does a lot of the heavy lifting for you to show the products list correctly.
Now, on to the template file, in my case content.phtml, you get your product collection and then use Subsurf's code in the top of the foreach() which repeats.
$_productCollection = Mage::getModel('catalog/product')
->getCollection()
// ->addAttributeToSelect('*')
// doesn't work ->addAttributeToSelect('special_price')
->addAttributeToSelect('sku')
->addAttributeToFilter('status', 1)
->addAttributeToFilter('visibility', 4)
// http://stackoverflow.com/questions/1332742/magento-retrieve-products-with-a-specific-attribute-value
->addFieldToFilter( array(
array('attribute'=>'manufacturer','eq'=>'143')
, array('attribute'=>'manufacturer','eq'=>'139')
))
;
echo "<p>There are ".count($_productCollection)." products: </p>";
echo '<div class="category-products">';
// List mode
if($this->getMode()!='grid') {
$_iterator = 0;
echo'<ol class="products-list" id="products-list">';
foreach ($_productCollection as $_product) {
?> <li class="item<?php if( ++$_iterator == sizeof($_productCollection) ): ?> last<?php endif; ?>">
<?php
$_product=Mage::getModel("catalog/product")->getCollection()
->addAttributeToSelect(Mage::getSingleton("catalog/config")->getProductAttributes())
->addAttributeToFilter("entity_id", $_product->getId())
->setPage(1, 1)
->addMinimalPrice()
->addFinalPrice()
->addTaxPercents()
->load()
->getFirstItem()
);
echo "Minimal price: ".$_product->getMinimalPrice()."<br>\n";
Since this isn't a single product's page and I'm using other template code, $this->setProduct() didn't do anything for me. I guessed if there was a set, there might be a get, so $_product=$this->getProduct() was the magic my template's price.phtml needed to work properly. Then I noticed I could just assign the Mage:: in setProduct() directly to $_product.
Thanks, Subsurf!!!
There is another way that will still allow you to use $this->getPriceHtml($_product, true); in your template, which then handles all the complex price display rules and puts "Starting at" before grouped product prices.
I'm using this for a custom featured product block. In a block class I have this function:
class MyCompany_Catalog_Block_Product_Featured extends Mage_Catalog_Block_Product_Abstract {
public function setGroupedProductPrice($group_product) {
$prices = array();
foreach ($group_product->getTypeInstance()->getChildrenIds($group_product->getId()) as $ids) {
foreach ($ids as $id) {
$product = Mage::getModel('catalog/product')->load($id);
$prices[] = $product->getPriceModel()->getPrice($product);
}
}
ksort($prices);
$group_product->setPrice(array_pop($prices));
}
}
Then in the template:
<?php
if ($_product->getTypeId() == 'grouped')
$this->prepareGroupedProduct($_product);
echo $this->getPriceHtml($_product, true);
?>
The following does the trick without using a product collection:
$priceResource = Mage::getResourceSingleton('catalogindex/price');
$select = $priceResource->getReadConnection()->select();
$select->from(
array('price_table' => $priceResource->getMainTable())
)
->where('price_table.entity_id = ?', $_product->getId())
->where('price_table.website_id = ?', Mage::app()->getStore()->getWebsiteId())
->where('price_table.customer_group_id = ?', Mage::getSingleton('customer/session')->getCustomerGroupId());
$result = $priceResource->getReadConnection()->fetchRow($select);
This code is adapted from \Mage_CatalogIndex_Model_Resource_Price::getMinimalPrices and made suitable for one single product.

How to call mycart product names in sidebar

I'm using magento 1.6.1.0. I included new sidebar in my cart page where it'll show product title, total amount & proceed to checkout button as one by one. I included total amount by calling <?php echo $this->getChildHtml('totals'); ?> and proceed to checkout button by calling <?php echo $this->getChildHtml('methods') ?>
But i don't know how to call Product name.
Anyone know how to call my cart products name which are currently added in basket? Please share your idea to do this!
Thanks
You can get access to your cart object via Mage::getSingleton('checkout/cart').
Calling Mage::getSingleton('checkout/cart')->getItems() will get you your current cart item collection that you can iterate through to get product names as well as other details.
$items = Mage::getSingleton('checkout/cart')->getItems();
foreach ($items as $item) {
echo $item->getName();
}

Magento brands by categories: show brand link

I'm setting up a webshop for a client in Magento.
He works a lot with very specific brands, so I wanted to make specific brands pages (for SEO purposes).
I followed the suggestion mentioned here: Mangento Shop By Brand to make categories out of brands.
It all works great, I can access my pages like example.com/brands/brandname.
But now, when in a products view, I want to link to that brand page.
How can I get a list of the categories for that product, or even the specific subcategory.
I thought about filtering categories by their parent_id (my brands page itself). But haven't got a clue how to go about doing it.
I found som info here but doesn't seem to work for my Magento (1.4.1.1)
It seems to do the work: Aitoc commercial module to shop by brand
Or I tried this code with Magento 1.4.1 which display the list/url of category/ies that the product belongs to, inspired of the link you provided and it works, put it in a block to allow the template to display the url:
public function getProductUrl($productId){
$product = Mage::getModel('catalog/product')->load(productId);
$currentCatIds = $product->getCategoryIds();
if ($currentCatIds) {
$categoryCollection = Mage::getResourceModel('catalog/category_collection')->addAttributeToSelect('name')
->addAttributeToSelect('url')
->addAttributeToFilter('entity_id', $currentCatIds)
->addIsActiveFilter();
foreach ($categoryCollection->getItems() as $item) {
/*echo $item->getName();
echo $item->getUrl();
echo '<br>';*/
if($item->getUrl()) return $item->getUrl();
}
}
}

Resources