to creating custom toolbar in magento get error Call to a member function setCurPage() on a non-object - toolbar

I display All Manufacturer with image with slider on Home page as block. there i put the View All link, on clicking that link its display all the manufacurer in new pahe as category page.
i created manufacturer.php as under
<?php
class Bc_Manufacturer_Block_Manufacturerpage extends Mage_Core_Block_Template
{
public function _prepareLayout()
{
return parent::_prepareLayout();
}
public function getManufacturer()
{
if (!$this->hasData('manufacturer')) {
$this->setData('manufacturer', Mage::registry('manufacturer'));
}
return $this->getData('manufacturer');
}
public function getToolbarHtml() {
$this->setToolbar($this->getLayout()->createBlock('catalog/product_list_toolbar', 'Toolbar'));
$toolbar = $this->getToolbar();
$toolbar->enableExpanded();
$toolbar->setAvailableOrders(array(
'ordered_qty' => $this->__('Position'),
'name' => $this->__('Name'),
'price' => $this->__('Price')
))
->setDefaultDirection('desc')
->setCollection($this->_productCollection);
$pager = $this->getLayout()->createBlock('page/html_pager', 'Pager');
$pager->setCollection($this->_productCollection);
$toolbar->setChild('product_list_toolbar_pager', $pager);
return $toolbar->_toHtml();
}
}
also call the getToolbarHtml() function in the manufacurerpage.phtml file as under
getToolbarHtml() ?>
<div class="grid-type-full">
<?php foreach($maufacturers as $manufacturer): ?>
<?php if(Mage::getModel('eav/entity_attribute_source_table')->setAttribute(Mage::getModel('eav/entity_attribute')->load(Mage::getModel('eav/entity_attribute')->getIdByCode('catalog_product',"manufacturer")))->getOptionText($manufacturer->getMenufecturerName())): ?>
<?php if ($i++%$_columnCount==0): ?>
<ul class="products-grid">
<?php endif ?>
<li style="position: relative; float: left; display: inline-block; " class="item<?php if(($i-1)%$_columnCount==0): ?> first<?php elseif($i%$_columnCount==0): ?> last<?php endif; ?>">
<a href="<?php echo $this->getBaseUrl()."catalogsearch/advanced/result/?manufacturer[]=".$manufacturer->getMenufecturerName() ?>">
<?php echo $this->getLayout()->createBlock('core/template')->setmanufacturerimage($manufacturer->getFilename())->setlegend($manufacturer->getLegend())->setListPageFlag(1)->setTemplate('manufacturer/manufacturer_resize.phtml')->toHtml(); ?>
<div class="manufacturer-name">
<?php echo Mage::getModel('eav/entity_attribute_source_table')->setAttribute(Mage::getModel('eav/entity_attribute')->load(Mage::getModel('eav/entity_attribute')->getIdByCode('catalog_product',"manufacturer")))->getOptionText($manufacturer->getMenufecturerName()) ?>
</div>
</a>
</li>
<?php if ($i%$_columnCount==0 && $i!=count($maufacturers)): ?>
</ul>
<?php endif ?>
<?php endif ?>
<?php endforeach ?>
<script type="text/javascript">decorateGeneric($$('ul.products-grid'), ['odd','even','first','last'])</script>
</div>
<div class="toolbar-bottom">
<?php echo $this->getToolbarHtml() ?>
</div>
after run this i got the error as under
Fatal error: Call to a member function setCurPage() on a non-object in C:\wamp\www\magento\jumbostore\app\code\core\Mage\Catalog\Block\Product\List\Toolbar.php on line 225
any one have solution for this please help me...
thank you..

Though this is a really late response , I had faced similar issue and found out that the setCollection()
was not getting the Product Collection.
You can find the Code at Your path app\code\core\Mage\Catalog\Block\Product\List\Toolbar.php at around line no 221.
<?php
public function setCollection($collection)
{
$this->_collection = $collection;
$this->_collection->setCurPage($this->getCurrentPage()); //*****HERE
// we need to set pagination only if passed value integer and more that 0
$limit = (int)$this->getLimit();
if ($limit) {
$this->_collection->setPageSize($limit);
}
if ($this->getCurrentOrder()) {
$this->_collection->setOrder($this->getCurrentOrder(), $this->getCurrentDirection());
}
return $this;
}
?>
Please try checking whether your function is getting the Collection or not.
Hope this is some help!!!

Related

Wordpress custom post type to cart not update it

I’m working on one client site, the previous developer uses WP custom post type build a product collection page, kind like woocommerce, but only can add to cart to request a quote to the company, no transaction on the cart application.
I have trouble with I can add to cart is fine, but when I view the cart page. the item won’t show up unless I refresh the page. same problem with delete item in the cart.
There’s a cookie set in the code, which I not familiar with, thinking might be the issue.
I thought I move the to the top will fix some issue, but the cart won't work. I got some unexpected end error on WP debug tool, so I tried to use PHP checker to see how many } I miss and add it back, and still not working.
Thank you so much for the help!
—–-- More Info ———-
Wordpress version: 4.9.7
PHP server: 5.6
Theme development date: unknown (Twenty thirteen theme modify version, never update)
—–-- code ———-
<?php
if(get_query_var('remove') > 0){
$r = get_query_var('remove');
$d = get_query_var('d');
if (isset($_COOKIE['cart'])) {
$cart = json_decode(base64_decode($_COOKIE['cart']), true);
unset($cart[$r][$d]);
if(empty($cart[$r])){
unset($cart[$r]);
}
if(empty($cart)){
setcookie("cart", base64_encode(json_encode($cart)), time() - 3600, '/');
} else {
setcookie("cart", base64_encode(json_encode($cart)), time() + 2592000, '/');
}
header('Location: '.get_the_permalink());
die();
} else {
header('Location: '.get_the_permalink());
die();
}
}
?>
<?php get_header();?>
<?php if(isset($ok) && $ok == 1){?>
<p class='after_cart_text'>Thank you for the opportunity to quote your project! We will endeavour to respond within 24 hours.</p>
<?php } else { ?>
<?php if(isset($ok) && $ok == -1){?>
<div class="alert alert-danger" role="alert">Please check the the captcha form.</div>
<?php } ?>
<div class="baners_categories">
<?php
$rows = get_field('banery_categories', 'options');
if($rows){
shuffle( $rows );
$image = $rows[0]['picture_categories'];
?>
<img src="<?php echo $image; ?>" />
<?php
}
?>
</div>
</fieldset>
<fieldset>
<!-- Form Name -->
<p class="legend_pe"><legend>Product Information</legend></p>
<?php if(isset($_COOKIE['cart']) && !empty($_COOKIE['cart']) && $post->post_name == 'cart'){
$cart = json_decode(base64_decode($_COOKIE['cart']), true);
//var_dump($cart);
$i = 0;
if (is_array($cart)) {
foreach($cart as $k => $row){
foreach($row as $k2 => $item){
//var_dump($item);
?>
<!-- form section code I comment it out -->
<?php }?>
<?php }?>
<div class="col-sm-4" style="margin-bottom:15px;">
<p><strong>Your cart is currently empty.</strong></p>
</div>
<?php }?>
<?php }?>
<?php }?>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
Thank you so much for any helps or tips!
I add this <?php wp_reset_postdata(); ?> end of the while loop and work now. More info here:https://codex.wordpress.org/Function_Reference/wp_reset_postdata

Add Wordpress sidebar using shortcode - conflicts with following custom meta

Trying to add a sidebar by creating and using a shortcode. I can get the sidebar to show, but it then causes issues with custom meta that creates buttons at the bottom of the page, after the_content();. This is the code to create the shortcode [sidebar]:
function sidebar_sc( $atts ) {
ob_start();
dynamic_sidebar('Sidebar');
$html = ob_get_contents();
ob_end_clean();
return '
<aside id="sidebar">'.$html.'</aside>';
}
add_shortcode('sidebar', 'sidebar_sc');
My page template has this:
<?php echo the_content(); ?>
<?php if($meta_button_t_1 || $meta_button_t_2 || $meta_button_t_3) { ?>
<?php get_template_part('section', 'pageButtons'); ?>
<?php } ?>
And the template part 'pageButtons' has this:
<div id="page_buttons" class="<?php echo $class; ?>">
<?php if($meta_button_t_1) { ?>
<?php echo $meta_button_t_1; ?>
<?php } if($meta_button_t_2) { ?>
<?php echo $meta_button_t_2; ?>
<?php } if($meta_button_t_3) { ?>
<?php echo $meta_button_t_3; ?>
<?php } ?>
</div>
Without using the [sidebar] shortcode, the buttons show fine. If I include the shortcode, then I get the div but it's empty. If I include the template part before the_content();, everything works except it's obviously in the wrong place.

Retrieving Product tag to use in query in Woocommerce

I'm trying to query posts with same tag as my product tag.
I can print my tag before my query (see first line of code), it works, but then inside the query it all goes wrong.
<?php echo $product->get_tags(); ?>
<?php
$producttags = get_tags();
if ($producttags) {
foreach($producttags as $tag) {
$tag = $tag->slug; break;
}
query_posts('tag='.$tag.'&posts_per_page=-1');
while (have_posts()) : the_post(); ?>
<div class="actucontent">
<h3>
<?php the_title(); ?>
</h3>
<?php the_content(); ?>
</div>
<?php endwhile;
wp_reset_query();
}
?>
Any idea? Problem is, I have no clue how to call $product->get_tags() inside my query.
get_tags() is a member of WP_Post, whereas $product is type WC_Product_Simple

random comments on page wordpress

my question is, how can i show random comments on a page on wordpress? in my website i got a wp page where people leaves lots of comments, i want them to show randomly and not assorted by date time, here's the code of the paginated comments, what should i do? thanks :)
<?php foreach ($comments as $comment) : ?>
<li <?php echo $oddcomment; ?>id="comment-<?php comment_ID() ?>">
<div class="paginated-comments-number" style="float: left; color: #999; width: 30px; text-align: left;"></div>
<?php
if ( function_exists('get_avatar') )
echo get_avatar( $comment, 48 );
?>
<cite><?php comment_author_link() ?>
</cite>
<?php if ($comment->comment_approved == '0') : ?>
<em>Your comment is awaiting moderation.</em>
<?php endif; ?><br />
<small class="commentmetadata"><?php comment_date('F jS, Y') ?> at <?php comment_time() ?> <?php edit_comment_link('edit',' ',''); ?></small>
<?php comment_text() ?>
</li>
<?php
/* Changes every other comment to a different class */
$oddcomment = ( empty( $oddcomment ) ) ? 'class="alt" ' : '';
?>
<?php endforeach; /* end for each comment */ ?>
</ol>
<p>
<!-- Start Paginated Comments Pages -->
<?php if ( Paginated_Comments_have_pages() ) : ?>
</p>
<p>
<?php endif; ?>
<!-- End Paginated Comments Pages -->
<?php else : // this is displayed if there are no comments so far ?>
<?php if ('open' == $post->comment_status) : ?>
<!-- If comments are open, but there are no comments. -->
<?php else : // comments are closed ?>
Here's a suggestion for a shortcode function that does what you're asking for:
add_shortcode( 'randomComment', 'randomComment_handler' );
function randomComment_handler($post_id) {
extract( shortcode_atts( array(
'post_id' => '0',
), $atts ) );
$out = "";
$comments = get_comments("post_id=$post_id&status=approve");
if ($comments) {
$ndx = mt_rand(0,sizeof($comments)) - 1;
$comment = $comments[$ndx];
$out = "<div class='randomComment'><div class='randomAuthor'>".$comment->comment_author."</div><div class='randomText'>".$comment->comment_content."</div></div>";
}
return $out;
}
This goes into your functions.php, and lets you put the shortcode on any page or post to show a random comment:
[randomComment post_id="1337"]
Just change the post_id according to the post you want to pull the random comment from.

Shortcode coming out at the top of content rather than in place where I require it

I'm aware it is probably a return problem. So I divided the content up, one in a function called thelist and the other being an actual function returning it. The code follows the question.
The actual shortcode works, except the contents appear at the top before the rest of the content. I thought the now_include_post return would fix it, however it does not. Can anybody help?
function thelist() {
if (have_posts()) : while (have_posts()) : the_post();
?>
<div id="post-<?php the_ID(); ?>" <?php post_class('thumb'); ?>>
<a href="<?php the_permalink() ?>" class="thumb-link">
<?php
if (!post_password_required()) {
if (has_post_thumbnail()) {
the_post_thumbnail();
}
} else {
?>
<img src="<?php bloginfo('template_url') ?>/img/locked.png" />
<?php } ?>
</a>
<h2>
<?php the_title(); ?>
</h2>
</div>
<?php /* end post */ ?>
<?php
endwhile;
endif;
wp_reset_query();
}
?>
<?php
function now_include_post($atts) {
$thepostid = intval($atts[id]);
query_posts("p=$thepostid");
$output .= thelist();
return $output;
}
You want to return all the text rather than outputting it then and there when you are escaping PHP.
At the start of your thelist() function start an output buffer with
ob_start();
Then at the end close this buffer and return its contents with
return ob_get_clean();
That will return the content rather than echo it straight away, which is what you want to do in the case of a WP shortcode
PHP information on Output Buffering Functions

Resources