Magento - How to check if Cross Sell products exist. - magento

In view.phtml I want to check if the product has any cross sell products associated with it.
I've done the same with related products:
$relatedProductsId=$_product->getRelatedProductIds();
$relatedProducts=array();
$i=0;
foreach($relatedProductsId as $relatedProductId)
{
$relatedProducts[$i] = array(Mage::getModel('catalog/product')- >load($relatedProductId)->getProductUrl(),
Mage::getModel('catalog/product')->load($relatedProductId)->getName(),
Mage::getModel('catalog/product')->load($relatedProductId)->getImageUrl(),
Mage::getModel('catalog/product')->load($relatedProductId)->getFormat()
);
$i++;
}
I'm not sure what the function used is for the Cross Sells products.
Please can someone help.

Did you try
$crossSellProducts = $_product->getCrossSellProducts()
?
It will return an array of cross-sell product objects.

Related

Get the tier prices of associated product in Magento 1.8.0

I'm using Magento 1.8.0
How can I get the tier prices of the associated product?
I'm only getting the price of the configurable product. Below is my site example:
Example: Product Apple is a configurable product thas has tier prices, $10,$20,$30. Product Apple has also an associated product like Green Apple, it has tier prices, $15,$20,$30.
My question here, is how can I get the value of my Associated products.
Thanks and Have a good Day!
You have to firstly get associated products
$product = Mage::getModel('catalog/product')->load(1); //your_product_id
$childProducts = Mage::getModel('catalog/product_type_configurable')
->getUsedProducts(null,$product);
foreach($childProducts as $child) {
$id = $child->getId();
$pro = Mage::getModel('catalog/product')->load($id); //load associated product id
if($pro['tier_price'] != NULL) {
foreach($pro['tier_price'] as $tier){
echo $tier['price'].'<br/>';
}
}
}

Magento Configurable Import CSV

I've got the spreadsheet below...
https://docs.google.com/spreadsheet/pub?key=0AjZAunFJlooudFhaQjZFLWZHTGZRc0dwZUMtel9BNmc&output=html
All of the simple products are uploading fine (I've added one as an example). But the configurable is not 'associating' to the products correctly.
When I go into the back-end of Magento after the import and click on the configurable product it asks me which Attributes I would like to assign against the configurable product.
Does anyone know why I cannot associate the configurable product with the simple products? There's obviously an issue with configurable product on the import.
With this function you can Assign/Unassign (merge) list of simple productids to existing configurable product
private function _attachProductToConfigurable( $_childProduct, $_configurableProduct ) {
$loader = Mage::getResourceModel( 'catalog/product_type_configurable' )->load( $_configurableProduct );
$ids = $_configurableProduct->getTypeInstance()->getUsedProductIds();
$newids = array();
foreach ( $ids as $id ) {
$newids[$id] = 1;
}
$newids[$_childProduct->getId()] = 1;
$loader->saveProducts( $_configurableProduct->getId(), array_keys( $newids ) );
}
Thanks

Magento - Promotions don't apply to downloadable files prices

Initial conditions:
Magento 1.7 installed (haven't tried with previous versions)
One (downloadable) product with multiple downloadable files, with prices added to the default product (let's say product that costs 50$ + 2 downloadable files, one free, the other an extra 50$ )
A new promotion (Catalog price rule) that applies to all products (let's say -20%)
More info about promotion:
Applies to all products, all groups, is active and applied, applies 'by percentage of original price', enable discount for subproducts -> Yes, stop further rule for processing -> No
Expected result:
Price for the product with the 50$ file: 80$ (80% from 100$)
Actual result:
Price for the product with the 50$ file: 90$ (80% from the initial 50$, and the full price for the downloadable file).
Conclusion:
The promotion doesn't apply to the extra prices that downloadable files have.
Question(s):
Is this the desired behavior for downloadable files? Or is this a bug ?
Any tips on how to modify the code (eventually create a module) to make it work as expected ? (Just tips, ie. what to extend)
Links / downloadable files its not products entities ( so it doesn't have price_index table and it doesn't treated as products )
There is 2 Ways to apply promotion in products
Catalog Price Rules
Shopping Cart Price Rules
As your question stated that you used Catalog Price Rules I have solved your question using Catalog Price Rules.
Create Module and rewrite the Model
Mage_Downloadable_Model_Product_Type
======
<global>
<models>
<downloadable>
<rewrite>
<product_type>Web_Eproduct_Model_Downloadable_Product_Type</product_type>
</rewrite>
</downloadable>
</models>
</global>
and the Code Below calculate the price of each Link on the fly ( even if you have more than one rule applied to the same product )
class Namespace_Modulename_Model_Downloadable_Product_Type extends Mage_Downloadable_Model_Product_Type {
public function getLinks($product = null)
{
$product = $this->getProduct($product);
$wId = Mage::app()->getWebsite()->getId();
$gId = Mage::getSingleton('customer/session')->getCustomerGroupId();
$catalogRules = Mage::getSingleton('catalogrule/resource_rule')->getRulesFromProduct('',$wId,$gId,$product->getId());
/* #var Mage_Catalog_Model_Product $product */
if (is_null($product->getDownloadableLinks())) {
$_linkCollection = Mage::getModel('downloadable/link')->getCollection()
->addProductToFilter($product->getId())
->addTitleToResult($product->getStoreId())
->addPriceToResult($product->getStore()->getWebsiteId());
$linksCollectionById = array();
foreach ($_linkCollection as $link) {
/* #var Mage_Downloadable_Model_Link $link */
$link->setProduct($product);
$link->setPrice($this->calcLinkPrice($catalogRules,$link->getPrice()));
$linksCollectionById[$link->getId()] = $link;
}
$product->setDownloadableLinks($linksCollectionById);
}
return $product->getDownloadableLinks();
}
public function calcLinkPrice(array $rules = array(),$productPrice = 0 )
{
foreach($rules as $ruleData)
{
$productPrice = Mage::helper('catalogrule')->calcPriceRule(
$ruleData['action_operator'],
$ruleData['action_amount'],
$productPrice);
}
return $productPrice;
}
}
I have tested it and confirmed its working as you expect :)
Try it and let me know your thoughts :)
There is another way to achieve this if you will use Shopping Cart Price Rules i will post it later.
There are 2 types of price rules in Magento, Catalog and Shopping Cart Price Rules. Catalog Rules are enacted on products before they are added to the cart, while Shopping Cart Price Rules are applied in the shopping cart.
You should set this promo as a Shopping Cart Price Rule.

Magento : Get all Shipping Rates

How can I get an array/object with the shipping rates in magento such as Flat Rate, Free Delivery etc ?
Irrelevant of the address or products selected.
Here's another way. You need to set a zip and country - even if that doesn't matter for your shipping methods.
// Change to your postcode / country.
$zipcode = '2000';
$country = 'AU';
// Update the cart's quote.
$cart = Mage::getSingleton('checkout/cart');
$address = $cart->getQuote()->getShippingAddress();
$address->setCountryId($country)
->setPostcode($zipcode)
->setCollectShippingrates(true);
$cart->save();
// Find if our shipping has been included.
$rates = $address->collectShippingRates()
->getGroupedAllShippingRates();
foreach ($rates as $carrier) {
foreach ($carrier as $rate) {
print_r($rate->getData());
}
}
you can't do it "irrelevant" as you need address data (billing or shipping if only billing then shipping set same as billing: country, zip, region dependant of your shipping methods) to be set and quote to have at least one simple item (quote existing, virtual and downloadable products don't need shipping). After that you can call on your quote object
$quote->getShippingAddress()->getGroupedAllShippingRates();
I figured it out...
$carriers = Mage::getStoreConfig('carriers', Mage::app()->getStore()->getId());
foreach ($carriers as $carrierCode => $carrierConfig) {
print_R($carrierConfig);
}
Thanks for the help

Magento Shipping Module: Get all items currently in cart

I'm in the middle of developing a shipping module for Magento but get stuck on How to get the items that's currently in the cart for that session.
I follow some tutorial on the internet, they use:
if ($request->getAllItems()) {
foreach ($request->getAllItems() as $item) {
//do something here.....
}
}
My problem is that I don't know exactly what info/data that's on the $item variable??.
I want to get the weight and the price of the product that's currently on the cart to calculate the shipping fee. I tried to print the $item value by using Mage::log or printing it to the screen using print_r or var_dump but it's not successful. The log is empty and the variable won't be printed on screen.
Can somebody inform me of how to get the $item attributes/method or is there any other way to get the product information that's currently in cart?
you can achive this by using one of three methods which are available in Mage::getSingleton('checkout/session')->getQuote();
getItemsCollection() - retrive sales/quote_items collection
getAllItems() - retrive all items
getAllVisibleItems() - retrive items which aren't deleted and have parent_item_id != null
I was searching for this solution and found below. but not tested.
$CartSession = Mage::getSingleton('checkout/session');
foreach($CartSession->getQuote()->getAllItems() as $item)
{
$productWeight = $item->getWeight();
$productExPrice = $item->getPrice(); // price excluding tax
$productIncPrice = $item->getPriceInclTax(); // price excluding tax
}
If you want to know information about $item, you can do this:
print_r($item->getData());
If you want to get item weight, here is it:
$item->getWeight();

Resources