I am rather new to opencart and I'm currently developing a shop where I need to be able to display product attributes inside the cart. I searched allover but apparently no one had the need for this feature.
I short, i tried to duplicate the way attributes are retrieved in the product page but no success.
I modified the controller adding this line:
$this->data['attribute_groups'] = $this->model_catalog_product->getProductAttributes($this->request->get['product_id']);
I also added:
<?php foreach ($attribute_groups as $attribute_group) { ?>
<?php echo $attribute_group['name']; ?>
<?php foreach ($attribute_group['attribute'] as $attribute) { ?>
<?php echo $attribute['name']; ?>
<?php echo $attribute['text']; ?>
<?php } ?>
<?php } ?>
<?php } ?>
with no succes.
I guess I have to modify somehow system/library/cart.php. The problem is that I am not savvy enough to know how to do it!
At this point I decided to ask for help here!
Any ideas please?
This isn't as hard as it would seem on the surface you'll only need to edit 2 files and open 3.
--1-- Add the method for retrieving attributes to the cart class.
Open catalog/model/catalog/product.php
Find the method getProductAttributes($product_id) and copy the entire method to your clipboard.
Open system/library/cart.php and after the getProducts() method paste your copied method.
--2-- Just above where you pasted the code, at the end of the getProducts() method you'll see where the products array is built for the view, it looks similar to this:
$this->data[$key] = array(
'key' => $key,
'product_id' => $product_query->row['product_id'],
'name' => $product_query->row['name'],
'model' => $product_query->row['model'],
'shipping' => $product_query->row['shipping'],
'image' => $product_query->row['image'],
'option' => $option_data,
'download' => $download_data,
'quantity' => $quantity,
'minimum' => $product_query->row['minimum'],
'subtract' => $product_query->row['subtract'],
'stock' => $stock,
'price' => ($price + $option_price),
'total' => ($price + $option_price) * $quantity,
'reward' => $reward * $quantity,
'points' => ($product_query->row['points'] ? ($product_query->row['points'] + $option_points) * $quantity : 0),
'tax_class_id' => $product_query->row['tax_class_id'],
'weight' => ($product_query->row['weight'] + $option_weight) * $quantity,
'weight_class_id' => $product_query->row['weight_class_id'],
'length' => $product_query->row['length'],
'width' => $product_query->row['width'],
'height' => $product_query->row['height'],
'length_class_id' => $product_query->row['length_class_id']
);
Now simply add a call to that getAttributes method to the array:
'attributes' => $this->getProductAttributes($product_query->row['product_id'])
Now open your cart template: catalog/view/theme/yourtheme/common/cart.tpl and right where the product option loop is, you can now loop through your attributes just like the options.
Related
I'm working on a project with Fuel. I created a pagination , first page has data truly in it but when I click on other pages , url changes but page never refresh and it doesn't show other pages.
codes:
code in Controller :
$config = array(
'pagination_url' => 'http://localhost/body-app/public/app/list/',
'total_items' => 12,
'per_page' => 3,
'uri_segment' => 5,
// or if you prefer pagination by query string
//'uri_segment' => 'page',
);
$pagination = Pagination::forge('mypagination', $config);
$data['example_data'] = DB::select('*')
->from('bodies')
->limit($pagination->per_page)
->offset($pagination->offset)
->execute()
->as_array();
$data['pagination'] = $pagination;
$view=View::forge('app/list',$data);
and my foreach in View is something like this :
<?php foreach($example_data as $per) {
echo $per['name'] ;
} ?>
and finally code to show pagination :
<?php echo Pagination::instance('mypagination')->render(); ?>
If you work with segment number, you should change the route.php file, and add the example 'prod/page/(:page)?' => 'prod' , or add in the you controller one segment string example 'uri_segment' => 'examplenumberpage'.
I have a post type called 'faq' and taxonomy called 'type' within my template and a few taxonomy terms created "Design", "Print", "Display" etc.
The idea I am trying to implement is to display only the posts that belong to assigned taxonomies (types) without duplication. Each post may be assigned to multiple taxonomies (types).
My current code works fine as long as the post have got only one taxonomy assigned to it. As soon as I assign more then one taxonomy it shows duplicate posts like this:
Question 6
Question 5
Question 1
Question 1
Here is my current code:
<?php
$post_type = 'faq';
$tax = 'type';
$faq_types = get_field('types');
$filtered = array();
$termargs = array( 'include' => $faq_types );
$tax_terms = get_terms($tax, $termargs);
if ($tax_terms) {
$i = 1;
foreach ($tax_terms as $tax_term) {
$args=array(
'post_type' => $post_type,
$tax => $tax_term->slug,
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<div class="accordion-section">
<a class="accordion-section-title" href="#accordion-<?php echo $i; ?>"><i class="fa fa-chevron-right"></i> <?php the_title(); ?></a>
<div id="accordion-<?php echo $i; ?>" class="accordion-section-content">
<?php the_content(); ?>
</div>
</div>
<?php
$i++;
endwhile;
}
wp_reset_query();
}
}
?>
I'd really appreciate any help with getting this working the way I need.
Your current loop is saying "For each taxonomy term, show all posts associated with that term", so of course it will duplicate if there is one post associated with multiple terms. Take your query out of the foreach loop and use a single tax query with an array of terms:
$args = array(
'post_type' => $post_type,
'tax_query' => array(
array(
'taxonomy' => $tax,
'field' => 'slug',
'terms' => $term_slugs,
),
),
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1
);
EDIT
By the way, you'll need to convert your array of term objects to an array of term slugs for this to work properly:
$term_slugs = array();
foreach( $tax_terms as $term ) {
$terms_slugs[] = $term->slug;
}
My main menu is appearing through print theme function but its sub menu is not appearing..What php code is written in template.php and then in page.tpl.php to get sub menus. I am trying for two days but no positive output.
If by sub-menu you mean a secondary menu on a particular page;
create the menu in structure >> menus >> Add Menu and add your links. Then go to structure >> blocks and find the block that corresponds to your menu and set it to appear on the relevant page and position within your theme.
If you want to add it within the .tpl file you can try something like this;
<nav class="header__secondary-menu" id="secondary-menu" role="navigation">
<?php print theme('links__system_secondary_menu', array(
'links' => $secondary_menu,
'attributes' => array(
'class' => array('links', 'inline', 'clearfix'),
),
'heading' => array(
'text' => $secondary_menu_heading,
'level' => 'h2',
'class' => array('element-invisible'),
),
)); ?>
</nav>
<?php endif; ?>
I'd like to display custom posts from specific category in Wordpress in custom page.
My code is:
<?php $loop = new WP_Query( array( 'post_type' => 'product', 'posts_per_page' => -1 ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php
$thumb_id = get_post_thumbnail_id();
$thumb_url = wp_get_attachment_image_src($thumb_id,'thumbnail-size', true);
?>
I'd like to display post from "loze" category.
Greetings
You can use category_name for this. Amend your WP_Query as follows:
$loop = new WP_Query(
array(
'post_type' => 'product',
'category_name' => 'loze',
'posts_per_page' => -1
)
);
However, I would recommend using the category ID instead (referenced using cat instead of category_name) as it's future-proofed somewhat (you could change the name of the category).
There is lots of information on this in the category section of the WordPress Codex for WP_Query.
I have a navigation and use it for breadcrumbs. I want to use it for top menu too , but I don't want to show my products in top menu.
How Can I achieve that ?
Add a custom attribute while defining the array in the module.config.php -
Eg:
array(
'label' => 'Product List',
'route' => 'product',
'action' => 'index',
'resource' => 'Product\Controller\Product:index',
'top_menu' => '0',
),
For the rest of the arrays set the top_menu attribute to 1.
In the partial file of top-menu, check -
<?php foreach ($this->container as $page) {
[...]
if($page->get('top_menu') == '1') {
//Code to display the menu.
}
[...]
} ?>
I hope it gives some idea.