How to add pagination for wishlist products in magento 1.9 - magento-1.9

I am trying to get products on the page-wise for wishlist collection. I was getting a count for product collections in the below code. I have set the page size to one but it's showing the product count two. Please help me to fix this. Thanks.
$wishlist1 = Mage::getModel('wishlist/wishlist')->loadByCustomer($customer, true)
->setCurPage($page_num)
->setPageSize(1)
->load();
foreach($wishlist1->getItemCollection() as $product){
$_product = Mage::getModel('catalog/product')->load($product['product_id']);//product id here
echo $product['product_id'];
}

Please try with this code:
<?php
$customer = Mage::getModel("customer/customer")->setWebsiteId(Mage::app()->getStore()->getWebsiteId())->loadByEmail('mohit#streammarket.co.uk');
$wishlistItems = Mage::getModel('wishlist/item')->getCollection()
->setWebsiteId($customer->getWebsiteId())
->setCustomerGroupId($customer->getGroupId())
->setCurPage(1)
->setPageSize(1)
->load();
?>

Related

show subcategories of parent category when no subcategories in category page

In magento when we click on a category it takes us to category page and shows
(if exists) subcategories of the clicked category in layered navigation on left.But when we click on a category that has no subcategories it show empty on that category page in layered navigation on left. What I want is to show subcategories of the parent if the current category has no subcategories.For this I have done the following but did not work for me.
I have tried to add the following to to
app/code/core/Mage/Catalog/Model/Layer/Filter/Category.php
if(count($categoty->getChildrenCategories())){
$categories = $categoty->getChildrenCategories();
}else{
$categories = $categoty->getParentCategory()->getChildrenCategories();
}
and removed line #163
$categories = $categoty->getChildrenCategories();
Please suggest me a solution. Any help will be greatly appreciated.
I have found the solution. I have to put the above login in left.phtml
app/design/frontend/theme/template/catalog/navigation/left.phtml
$categoty = Mage::registry('current_category');
$categories = $category->getChildrenCategories();
//$_categories = $this->getCurrentChildCategories();
if(count($categoty->getChildrenCategories())){
$_categories = $categoty->getChildrenCategories();
}else{
$_categories = $categoty->getParentCategory()->getChildrenCategories();
}
and it worked like a charm! Hope it will help someone else!

Get number of swatches of an attribute that has a product

I have configurable products with a swatch attribute, in this case is color.
I need to know how many colors (the number) has a product, or maybe how many single products compose this configurable product.
As a matter of fact, I need to know when there is more than one color in a product.
Finally I found it, maybe it will help somebody in the future:
$productAttributeOptions = $_product->getTypeInstance(true)->getConfigurableAttributesAsArray($_product);
//in this case the attribute color that I needed is in [0] position
$available_colors = sizeof($productAttributeOptions[0]["values"]);
if ($available_colors >1):
//custom code
endif;
Edit: This solution works for one product, or at least for a few products, but if you need it in a list of products it is really slow to run this code in each of the products. Sometimes it drives to a timeout and closes the db connections, so the web crashes with an error.
Finally I got a solution, maybe it is not the best one, but it is pretty fast compared to the one I was using before:
$_idsForTheQuery = $_productCollection->getAllIds();
$read = Mage::getSingleton('core/resource')->getConnection('core_read');
$sql_query = "SELECT parent_id, COUNT(parent_id) AS colors FROM
(SELECT cpr.parent_id FROM `eav_attribute` a
LEFT JOIN `catalog_product_entity_int` cpei ON cpei.attribute_id=a.attribute_id
LEFT JOIN `catalog_product_relation` cpr ON cpr.child_id=cpei.entity_id
WHERE attribute_code = 'color' AND cpr.parent_id IN (".implode (", ", $_idsForTheQuery).")
GROUP BY cpr.parent_id, cpei.value) colors
GROUP BY parent_id";
$results = $read->query($sql_query);
$number_of_colors_by_id_array = array();
foreach($results as $r)
{
$number_of_colors_by_id_array[$r["parent_id"]] = $r["colors"];
}
and then in the foreach loop of the products
<?php if ($number_of_colors_by_id_array[$_product->getId()]>1): ?>
<div class="aditional-colors-message">
<?php echo __('more colors available'); ?>
</div>
<?php endif; ?>

How to implement 15% off on first order in Magento

I am trying to implement the 15% off your first order. And user can be guest or registered.
Please share any helpful URL or guidance or extension for Magento CE 1.9
Please follow the below code:
<?php
if(Mage::getSingleton('customer/session')->isLoggedIn()){
$customerData = Mage::getSingleton('customer/session')->getCustomer();
$customer_id = $customerData->getId();
$_orders = Mage::getModel('sales/order')->getCollection()->addFieldToFilter('customer_id',$customer_id);
$_orderCnt = $_orders->count(); //orders count
if ($_orderCnt<1){
// Display your discount coupon code for apply in cart page
// Or play with your discount code
}
}
Hope It's work for you.

How can I echo out an Item's price in Wordpress/ Woocmerce?

lets say I am in the Page section where you put the content in WYSIWYG, my plan is to display one woocomerce item's price using php to pull out the info. I downloaded this Plugin Insert PHP where it allows you to put php code in the WYSIWYG section using a special shortcode. The thing is that I am not an expert on php whatsoever, so how would I even start?
so far I've researched this:
<?php echo $product->get_price_html(); ?>
lets say its the item with an SKU: 6-501
how would you do an php to pull the out the price ONLY of that item?
Use the get_price() method instead:
<?php echo $product->get_price(); ?>
WooCommerce version 2.3 and up has a function wc_get_product_id_by_sku( $sku ).
so you could do something like,
$sku = '6-501';
$_product_id = wc_get_product_id_by_sku( $sku );
$_product = new WC_Product( $_product_id );
echo $_product->get_price_html();
Assuming that you are using Insert PHP plugin, Place the following code between [insert_php] and [/insert_php]:
$sku = '6-501';
global $wpdb;
$id = $wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key='_sku' AND meta_value='%s' LIMIT 1", $sku ) );
if ( $id ){
$product = new WC_Product( $id );
echo $product->get_price();
}
That should work.

Magento: Get simple product color from custom table

I'm really struggling with this in Magento 1.10 Enterprise. I have a array of simple products color ids and I want to use this id to query the atb_color table. Raw query:
SELECT description FROM atb_colors WHERE option_id = 'my_color_id'
Here is a method I was trying to build:
public function getColorData($product){
$ids = $product->getTypeInstance()->getUsedProductIds();
foreach($ids as $id){
$simpleproduct = Mage::getModel('catalog/product')->load($id);
-->Query using my_color_id
}
}
I can use this to get name and quantity. If I put this in the foreach loop:
echo $simpleproduct->getName()." - ".(int)Mage::getModel('cataloginventory/stock_item')->loadByProduct($simpleproduct)->getQty() . '<br />';
How would I run this query. Forgive me I am very new to Magento. It's somewhat difficult to grasp some of it. But I'm on a deadline to finish this one section of displaying color and size. Any help? Please, please!!
Thanks in advance
This is the weirdest hack, but if you are really in big hurry
public function getProductCustomColor($product)
{
$ids = $product->getTypeInstance()->getUsedProductIds();
foreach($ids as $id){
$simpleProduct = Mage::getModel('catalog/product')->load($id);
$select = $product->getResource()->getReadConnection()->select()
->from('atb_colors', array('description'))
->where('option_id = :my_color_id');
$colorDescription = $product->getResource()->getReadConnection()
->fetchOne($select, array('option_id' => $simpleProduct->getYourColorId()));
// ...
}
}
To everyone: never write code for magento in this way.