How to display top sellers by date range in Magento - php

Is there anyway to display best sellers by date range? In example 30 days, 60 days, etc
This is my current bestsellers.phtml and we have no idea what range this data is pulling from
<?php
$params = $this->getData();
if(array_key_exists('limit', $params)) {
$limit = $this->getData('limit');
} else {
$limit = 3;
}
$products = $this->getBestSellers($limit);
?>
<script type="text/javascript" src="<?php echo $this->getSkinUrl('js/overlay_jquery.js');?>"></script>
<script type="text/javascript" src="<?php echo $this->getSkinUrl('msslider/slider2/stepcarousel.js') ?>"></script>
<script type="text/javascript">
stepcarousel.setup({
galleryid: 'mygallery', //id of carousel DIV
beltclass: 'belt', //class of inner "belt" DIV containing all the panel DIVs
panelclass: 'panel', //class of panel DIVs each holding content
autostep: {enable:false, moveby:1, pause:2000},
panelbehavior: {speed:500, wraparound:true, wrapbehavior:'slide', persist:true},
defaultbuttons: {enable: false, moveby: 1, leftnav: ['<?php echo $this->getSkinUrl('msslider/slider2/arrowl.png')?>', -15, 100], rightnav: ['<?php echo $this->getSkinUrl('msslider/slider2/arrowr.png')?>', -25, 100]},
contenttype: ['inline'] //content setting ['inline'] or ['ajax', 'path_to_external_file']
})
</script>
<div class="category-products home-page-listing">
<div id="mygallery" class="stepcarousel">
<div class="belt products-grid">
<?php if(is_array($products) && count($products)): ?>
<?php $i = 0; foreach($products as $product): ?>
<div class="panel">
<div class="product-topbg">
<div class="product-bottombg">
<div class="product-midbg">
<h2 class="product-name" style="margin: 5px 0px 15px;">
<?php echo $product['category_name']; ?>
</h2>
<div class="overlay"
id="overlay-<?php echo $i; ?>"
>
<a href="<?php echo $product['url']; ?>"
title="<?php echo $product['name']; ?>"
>
<img src="<?php echo $product['image_url']; ?>"
width="160" height="160"
alt="<?php echo $product['name']; ?>"
/>
</a>
</div>
<h2 class="product-name" style="margin: 15px 0px 5px;">
<a href="<?php echo $product['url']; ?>"
title="<?php echo $product['name']; ?>">
<?php echo $product['name']; ?>
</a>
</h2>
<div class="overlay-show"
id="overlay-show-<?php echo $i;?>"
>
<div class="product-price">
<?php echo $product['price']; ?>
</div>
<div class="price-bottombg"></div>
</div>
</div>
</div>
</div>
<script type="text/javascript">
var $i = jQuery.noConflict();
$i("#overlay-<?php echo $i;?>").hover(function () {
if ($i("#overlay-show-<?php echo $i;?>").is(":hidden")) {
$i("#overlay-show-<?php echo $i;?>").slideDown("fast");
} else {
$i("#overlay-show-<?php echo $i;?>").slideUp("fast");
}
});
</script>
</div>
<?php $i++; endforeach;?>
<?php endif; ?>
</div>
</div>
<script type="text/javascript">
decorateGeneric($$('ul.products-grid'), ['odd', 'even', 'first', 'last']);
</script>
</div>
Thank you for any help with this code.

As I can see, this is the block that belongs to some 3-d party extension (or smb might have written it). That's why you should locate the block and correct the collection of products it receives via the getBestSellers method.
If you want to see only the last 3 updated products, the easiest way is to add 'order DESC' by 'updated_at' to the collection
Thus, the first 3 products will be the last updated on the list. It will look like that:
$collection->setOrder('update_at', Varien_Data_Collection_Db::SORT_ORDER_DESC);
If you want to get populated some other field, that is different from the 'updated_at', just use its name instead of 'updated_at'.
Note, that you should implement all the changes before the collection gets downloaded.
The block can be find via 'updated_at' or by means of the layout. Additionally, you can search for updated_at inside of the template. Use the most convenient for you method.

Related

Dynamically change background image in the CSS during WordPress loop (media queries)

I currently have a carousel on my page inside a Wordpress loop. So as it loops through it changes the background image according to which post I am currently on in the loop. However, I want to use media queries to load smaller images if for instance I the screen size is smaller.
This is what I currently have:
while( $loop->have_posts() ) : $loop->the_post();
$class = '';
// Custom Posts
$carousel_image_1 = get_field('carousel_image_1');
$image_1_title = get_field('image_1_title');
if( $slide_counter == 0 ) { $class .= ' active'; }
?>
<div class="item <?php echo $class ?> <?php echo "slide_" . $slide_counter ?>">
<!-- Set the first background image using inline CSS below. -->
<a href="<?php echo get_post_permalink(); ?>" target="_blank">
<div class="fill" style="background-image:url(<?php echo $carousel_image_1['url']; ?>); background-size:cover;" alt="<?php echo $carousel_image_1['alt']; ?>"> </div>
<div class="carousel-caption">
<h2><?php echo $image_1_title; ?></h2>
<img width="80" class="book" src="<?php bloginfo('stylesheet_directory'); ?>/assets/img/book.png" alt="">
</div>
</a>
</div>
<?php $slide_counter++; ?>
<?php endwhile; wp_reset_query(); ?>
You can't use media queries inline, and you won't be able to have the dynamic flexibility of your variables in CSS, because PHP is server-side.
But, you could do this with JavaScript as long as the JS is able to get the PHP variables (my example uses jQuery):
<?php
while( $loop->have_posts() ) : $loop->the_post();
$class = '';
// Custom Posts
$carousel_image_1 = get_field('carousel_image_1');
$carousel_image_2 = get_field('carousel_image_2'); // added this, for different size image
$image_1_title = get_field('image_1_title');
if( $slide_counter == 0 ) { $class .= ' active'; }
?>
<div class="item <?php echo $class ?> <?php echo "slide_" . $slide_counter ?>">
<a href="<?php echo get_post_permalink(); ?>" target="_blank">
<div class="fill" style="background-size:cover;" alt="<?php echo $carousel_image_1['alt']; ?>"> </div>
<div class="carousel-caption">
<h2><?php echo $image_1_title; ?></h2>
<img width="80" class="book" src="<?php bloginfo('stylesheet_directory'); ?>/assets/img/book.png" alt="">
</div>
</a>
</div>
<script>
var resizeTimer;
function resizer() {
clearTimeout(resizeTimer);
resizeTimer = setTimeout(function() {
var windowWidth = $(window).width();
if (windowWidth >= 992) { // for width 992 pixels and up
$('.fill').css({
'background-image': 'url(<?php echo $carousel_image_1["url"]; ?>)';
});
} else { // smaller sizes
$('.fill').css({
'background-image': 'url(<?php echo $carousel_image_2["url"]; ?>)';
});
}
}, 200);
}
resizer();
$(window).on('resize', resizer);
</script>
<?php $slide_counter++; ?>
<?php endwhile; wp_reset_query(); ?>
I haven't tested it but I think it should work. You can also adjust the timeout to your preference (right now it's 200ms).
Or if you wanted to not make it truly responsive, and just set the background on page load, you could just write:
<script>
if (windowWidth >= 992) { // for medium size width and up
$('.fill').css({
'background-image': 'url(<?php echo $carousel_image_1["url"]; ?>)';
});
} else { // smaller sizes
$('.fill').css({
'background-image': 'url(<?php echo $carousel_image_2["url"]; ?>)';
});
}
</script>
The following Html CSS will show image in mobile view only
Html:
foreach($data as $item):
echo "
<div class='col-lg-4'>
<div class='panel panel-primary backImg' style='background-image:url({$item['imageLink']}); background-size:100% 100%;'>
Some Text
</div>
</div>";
endforeach;
Css:
#media(min-width: 480px){
.backImg{
background-image:none !important;
}
}

Arrange divs on new row every third time

I have this most annoying problem; I'm trying to arrange three divs on a row, and then new row, and another three divs and so on, like this:
<div class="container">
<div class="row">
<div class="col-sm-1">1</div>
<div class="col-sm-1">2</div>
<div class="col-sm-1">3</div>
</div>
<div class="row">
<div class="col-sm-1">4</div>
<div class="col-sm-1">5</div>
<div class="col-sm-1">6</div>
</div>
</div>
As for this accepted answer,
There is one catch: 0 % 3 is equal to 0. This could result in
unexpected results if your counter starts at 0.
So how would i implement this into this code:
<div class="col-md-8">
<?php
foreach($this->movies->movie_data as $key => $movie){
$string = file_get_contents("http://example.com/?t=" . urlencode($movie->movie_titel). "&y=&plot=short&r=json");
$result = json_decode($string);
if($result->Response == 'True'){
?>
<div class="col-sm-4">
<?php if($result->Poster == 'N/A') : ?>
<a href="<?php echo Config::get('URL')?>ladybug/day/<?php echo $this->city ?>/<?php echo $movie->movie_id ?>">
<img src="<?php echo Config::get('URL')?>/images/na.png" class="img-responsive img-thumbnail"></a>
<?php else: ?>
<a href="<?php echo Config::get('URL')?>ladybug/day/<?php echo $this->city ?>/<?php echo $movie->movie_id ?>">
<img src="<?php echo $result->Poster; ?>" class="img-responsive img-thumbnail"></a>
<?php endif; ?>
<div><b><?php echo $result->Title; ?></b></div>
<div><i><?php // echo $result->Plot; ?></i></div>
</div>
<?php }else{ ?>
<div class="col-sm-4">
<a href="<?php echo Config::get('URL')?>ladybug/day/<?php echo $this->city ?>/<?php echo $movie->movie_id ?>">
<img src="<?php echo Config::get('URL')?>/images/na.png" class="img-responsive img-thumbnail"></a>
<div><b><?php echo $movie->movie_titel ?></b></div>
<div class="plot"><i><?php //echo 'N/A' ?></i></div>
</div>
<?php }}} ?
</div>
For some reason, divs is arranged like this:
My question: How do I arrange thumbnails on a new row, every third time?
Found the answer in the other Q... Didn't read, sorry about that.
<?php }
if (($key + 1) % 3 == 0) { ?>
</div>
<?php }
}} ?>

Change Magento's Upsell Slider to use Related Products

I have a client who has accidentally added the products in the Related Products section rather than the Upsell Section. I have a file named upsell_slider.phtml that spits out the following:
<?php if(count($this->getItemCollection()->getItems())): ?>
<div class="box-collateral box-up-sell upsell-slider">
<h2><?php echo $this->__('Fashion Statement') ?></h2>
<?php $this->setColumnCount(4); // uncomment this line if you want to have another number of columns. also can be changed in layout ?>
<?php $this->resetItemsIterator(); ?>
<?php
$products_count = 0;
while($this->getIterableItem()){
$products_count++;
}
?>
<div <?php if ($products_count > 1): ?>id="block-upsell-slider"<?php else:?> class="no-slider" <?php endif; ?>>
<ul class="products-grid carousel-ul" id="upsell-product-table">
<?php $this->resetItemsIterator(); while ($_item=$this->getIterableItem()) : ?>
<li class="item grid_3 alpha">
<a class="product-image" href="<?php echo $_item->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_item->getName()) ?>"><img src="<?php echo $this->helper('catalog/image')->init($_item, 'thumbnail')->constrainOnly(TRUE)->keepAspectRatio(TRUE)->keepFrame(FALSE)->resize(420, null); ?>" <?php echo MAGE::helper('ThemeOptions/Retina')->getRetinaData('upsell', $_item); ?> alt="<?php echo $this->htmlEscape($_item->getName()) ?>" /></a>
<div class="product-details">
<h3 class="product-name"><?php echo $this->htmlEscape($_item->getName()) ?></h3>
</div>
</li>
<?php endwhile; ?>
</ul>
</div>
<?php if ($products_count > 1): ?>
<div class = 'next'></div>
<div class = 'prev unselectable'></div>
<?php endif; ?>
</div>
<script type="text/javascript">
/* Upsell Block Slider */
if(jQuery('#block-upsell-slider').length) {
jQuery('#block-upsell-slider').iosSlider({
responsiveSlideWidth: true,
snapToChildren: true,
desktopClickDrag: true,
infiniteSlider: true,
/* navSlideSelector: '.sliderNavi .naviItem', */
navNextSelector: '.box-up-sell .next',
navPrevSelector: '.box-up-sell .prev'
});
}
function upsell_set_height(){
var upsell_height = 0;
jQuery('#block-upsell-slider li.item').each(function(){
if(jQuery(this).height() > upsell_height){
upsell_height = jQuery(this).height();
}
})
jQuery('#block-upsell-slider').css('min-height', upsell_height+2);
}
setTimeout(function(){
upsell_set_height();
}, 1000);
jQuery(window).resize(function(){upsell_set_height();});
/* Upsell Block Slider */
</script>
<?php endif ?>
And I can't for the life of me get it to look at the related products, rather than the upsell products.
I have a better solution. You can transform the related products in upsells with one query.
UPDATE `catalog_product_link` SET `link_type_id` = 4 WHERE `link_type_id` = 1;
Note. This will make move all the related products to upsells.
...and back up the table before trying it.

Magento 1.7.0.0 FishPig - Excluding Category

EDIT 01-28-13: Revised to clarify my question.
I'm using Magento 1.7.0.0 and FishPig WP full integration. We have successfully listed products from specific category and from all categories but was wondering if we can exclude in some cases from specified category. I found solutions to do this in functions.php within WordPress but this does not seem to work.
Here is the current code that displays posts from all categories. We'd like to add an exception so WordPress 1 Category can be excluded.
Here is the code that displays from all categories which I'd like to NOT include category "Press_HomePage":
$col_posts = Mage::getResourceModel('wordpress/post_collection')->addIsPublishedFilter();
$posttotal = count($col_posts->getAllIds());
$posttotid = $col_posts->getAllIds();
//i<=2 means displays last 2 posts
//display latest 2 posts...
for ( $i=1; $i<=2; $i++ )
{
?>
<div class="blog">
<h2><?php echo $col_posts->getItemById($posttotid[$posttotal-$i])->getPostDate(); ?></h2>
<h1><?php echo $col_posts->getItemById($posttotid[$posttotal-$i])->getPostTitle(); ?></h1>
<div style="float:left; margin-top:15px; margin-bottom:25px;">
<?php
$featured_img = $this->getSkinUrl('images/pree_emty.png');
if($featuredImage = $col_posts->getItemById($posttotid[$posttotal-$i])->getFeaturedImage())
{
$featured_img = $featuredImage->getAvailableImage();
}
?>
<img style="float: left;" src="<?php echo $featured_img; ?>" width="204" height="204" alt="" />
<div style="float: left; width: 580px; padding: 10px;">
<p><?php echo substr(strip_tags($col_posts->getItemById($posttotid[$posttotal-$i])->getPostContent()), 0, 400); ?></p>
<p>
<a href="<?php echo $col_posts->getItemById($posttotid[$posttotal-$i])->getUrl(); ?>">
<img src="<?php echo $this->getSkinUrl('images/view_btn.jpg'); ?>" width="170" height="32" alt="" />
</a>
</p>
</div>
</div>
</div>
<?php
}
?>
Let me know if I need to clarify myself. I appreciate your time.
//loki - get all the post ids
$col_posts = Mage::getResourceModel('wordpress/post_collection')->addIsPublishedFilter();
$posttotid = $col_posts->getAllIds();
//loki - get all the press ids
$col_posts_press = Mage::getResourceModel('wordpress/post_collection')->addIsPublishedFilter()->addCategorySlugFilter('press_homepage');
$posttotid_press = $col_posts_press->getAllIds();
//loki - removing the press_homepage category from array and reindexing
$blogposts = array_diff($posttotid, $posttotid_press);
$blogposts = array_values($blogposts);
$posttotal = count($blogposts);
//i<=2 means displays last 2 posts
//display latest 2 posts...
for ( $i=1; $i<=2; $i++ )
{
?>
<div class="blog">
<h2><?php echo $col_posts->getItemById($blogposts[$posttotal-$i])->getPostDate(); ?></h2>
<h1><?php echo $col_posts->getItemById($blogposts[$posttotal-$i])->getPostTitle(); ?></h1>
<div style="float:left; margin-top:15px; margin-bottom:25px;">
<?php
$featured_img = $this->getSkinUrl('images/pree_emty.png');
if($featuredImage = $col_posts->getItemById($blogposts[$posttotal-$i])->getFeaturedImage())
{
$featured_img = $featuredImage->getAvailableImage();
}
?>
<img style="float: left;" src="<?php echo $featured_img; ?>" width="204" height="204" alt="" />
<div style="float: left; width: 580px; padding: 10px;">
<p><?php echo substr(strip_tags($col_posts->getItemById($blogposts[$posttotal-$i])->getPostContent()), 0, 400); ?></p>
<p>
<a href="<?php echo $col_posts->getItemById($blogposts[$posttotal-$i])->getUrl(); ?>">
<img src="<?php echo $this->getSkinUrl('images/view_btn.jpg'); ?>" width="170" height="32" alt="" />
</a>
</p>
</div>
</div>
</div>
<?php
}
?>

Magento Related Product don't show up

I am trying to get the related products block to show up on my product detail page.
I have the folling code in the respective .phtml file
<?php
<?php echo "Related product block"?>
<?php if($this->getItems()->getSize()): ?>
<div class="block block-related">
<div class="block-title">
<strong><span><?php echo $this->__('Related Products') ?></span></strong>
</div>
<div class="block-content">
<p class="block-subtitle"><?php echo $this->__('Check items to add to the cart or') ?> <?php echo $this->__('select all') ?></p>
<ol class="mini-products-list" id="block-related">
<?php foreach($this->getItems() as $_item): ?>
<li class="item">
<?php if(!$_item->isComposite() && $_item->isSaleable()): ?>
<?php if (!$_item->getRequiredOptions()): ?>
<input type="checkbox" class="checkbox related-checkbox" id="related-checkbox<?php echo $_item->getId() ?>" name="related_products[]" value="<?php echo $_item->getId() ?>" />
<?php endif; ?>
<?php endif; ?>
<div class="product">
<img src="<?php echo $this->helper('catalog/image')->init($_item, 'thumbnail')->resize(50) ?>" width="50" height="50" alt="<?php echo $this->htmlEscape($_item->getName()) ?>" />
<div class="product-details">
<p class="product-name"><?php echo $this->htmlEscape($_item->getName()) ?></p>
<?php echo $this->getPriceHtml($_item, true, '-related') ?>
<?php if ($this->helper('wishlist')->isAllow()) : ?>
<?php echo $this->__('Add to Wishlist') ?>
<?php endif; ?>
</div>
</div>
</li>
<?php endforeach ?>
</ol>
<script type="text/javascript">decorateList('block-related', 'none-recursive')</script>
</div>
<script type="text/javascript">
//<![CDATA[
$$('.related-checkbox').each(function(elem){
Event.observe(elem, 'click', addRelatedToProduct)
});
var relatedProductsCheckFlag = false;
function selectAllRelated(txt){
if (relatedProductsCheckFlag == false) {
$$('.related-checkbox').each(function(elem){
elem.checked = true;
});
relatedProductsCheckFlag = true;
txt.innerHTML="<?php echo $this->__('unselect all') ?>";
} else {
$$('.related-checkbox').each(function(elem){
elem.checked = false;
});
relatedProductsCheckFlag = false;
txt.innerHTML="<?php echo $this->__('select all') ?>";
}
addRelatedToProduct();
}
function addRelatedToProduct(){
var checkboxes = $$('.related-checkbox');
var values = [];
for(var i=0;i<checkboxes.length;i++){
if(checkboxes[i].checked) values.push(checkboxes[i].value);
}
if($('related-products-field')){
$('related-products-field').value = values.join(',');
}
}
//]]>
</script>
The echo above the code shows up on my page. Which of course proves that i implemented the block correctly.
Just everything in the if-statement doesn't show.
I spend some time looking for solutions and i tried rebuilding the indexes and my related product is visible on the frontend.
Anyone know how i can fix this?
Magento will always return a low number or 0 for the condition:
$this->getItems()->getSize()
if some/all of the related products are in the user's cart, and consequently items will appear as though they are missing.
To prevent this behaviour, duplicate the core Magento 'Related' class:
/app/code/core/Mage/Catalog/Block/Product/List/Related.php
to local:
/app/code/local/Mage/Catalog/Block/Product/List/Related.php
Then comment out the following lines in the if statement:
if (Mage::helper('catalog')->isModuleEnabled('Mage_Checkout')) {
//mod - show all related products, even if they have been added to the cart
//Mage::getResourceSingleton('checkout/cart')->addExcludeProductFilter($this->_itemCollection,
// Mage::getSingleton('checkout/session')->getQuoteId()
//);
$this->_addProductAttributesAndPrices($this->_itemCollection);
}
On your 4 line:
<?php if($this->getItems()->getSize() > 0 ? true : false) ?>
Try it.
At the top of your code type
var_dump($this->getItems()->getSize()) // does this print NUll, False or > 0
If the code above print NULL or false or less than 1, then do
print_r($this->getItems());
If none of the above print the information you expected then check your block getItems() method
Also where is the closing IF for if($this->getItems()->getSize()):

Categories