I am using PrestaShop version 1.5.4.1
Currently, my cart has separate delete buttons for each product.
How can I remove all the products in one action? I just need to empty the cart in one click.
I have used this code in ordercontroller and call the function from themes/defaulte/shoopin-cart.tpl
public function emptybag()
{
$products = $this->getProducts();
foreach ($products as $product) {
$this->deleteProduct($product->id);
}
}
Many things :
$this->getProducts() won't work in the order controler. Use get it with the context instead
getProducts() method doesn't return product object, but a collection of product array. You can't get informations with -> use [] instead
There is your correct function :
public function emptybag()
{
$products = $this->context->cart->getProducts();
foreach ($products as $product) {
$this->context->cart->deleteProduct($product["id_product"]);
}
}
To make it easier, add your function to your overrided file of the front controler, like that you will be able to call it from everywhere in the front. Then override the init function and add these line to the end of the function (not before because we need the cart attribute to be initialised) :
if (isset($_GET['emptybag'])){
$this->emptybag();
}
Then, add a link to your template where you want :
{l s='Clear cart'}
And this is it!
To have a clean url in your navigation you can add this line after your condition "emptybag"
Tools::redirect($this->context->link->getPageLink('order', true, NULL));
to redirect page on order.
$this->context->cart->delete();
Simple!
Related
First of all I am new to Magento and tried to create a module in it. It displays some products and sort them in particular order base on a attribute .Here is the code of the block
class Your_Block_Name extends Mage_Core_Block_Template {
public function getProducts() {
$collection = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect("*")
->addFieldToFilter('status', '1')
->addFieldToFilter('my_attribute', array("notnull" => true))
->setOrder('my_attribute', 'whatever is the order');
return $collection;
}
}
And in the .phtml file I call the above function and display the products and everything worked fine. Here is the code of .phtml file
<?php
$product_collection = $this->getProducts($sort_order);
foreach ($product_collection as $product) {
//your code
}
?>
Now I need to place a link or a button or a dropdown in the template and after clicking that link/button/dropdown I want to change the order of display. I do not know how can I achieve this. I do not know how can I pass the value(s) from that link to BLOCK or controller so that it change the order of display. And I do not know what is the best possible way to achieve this.
By the way I am using Magento community version.
used ->addAttributeToSort('attribute_code','sortorder') instead of setOrder().
sortorder should be DESC Or ASC
and
public function getProducts($sortOrder='DESC') {
$collection = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect("*")
->addFieldToFilter('status', '1')
->addFieldToFilter('my_attribute', array("notnull" => true))
->addAttributeToSort('attribute_code',$sortOrder);
return $collection;
}
On my product.tpl in Opencart, I am trying to display a "Warranty Included" icon.
My problem is, every product on my store doesnt have a Warranty, only specific Products do. On each of my products I have a whole list of Attributes (Specifications). I was wondering if there was some PHP code that I could wrap around the image that I would be able to use an IF statement to say something like this:
Single Statement:
<?php if (products attributes "Product type = Computer") {
//Do this
} ?>
Multiple Statement:
<?php if (products attributes "Product type = Computer" & "Screen Size = 56") {
//Do this
} ?>
I know thats not the code above, but I was wondering if there was away to use PHP if statements with Opencart Attributes.
Thanks in Advance!!
Method 1 : (Dirty method)
if ($attribute_groups) {
foreach ($attribute_groups as $attribute_group) {
foreach ($attribute_group['attribute'] as $attribute) {
if($attribute['name']=='warranty' && $attribute['text']=="yes")
{
// display image
// stop the loop if you don't need it further
}
}
}
}
Method 2 : create a function in model/catalog/product.php like checkProductWarranty() which may return a Boolean value
In this method basically you need to join tables product_attribute and attribute_description on attribute_id put name="warranty" in where clause and do your thing
for reference you can see public function getProductAttributes($product_id) in the same model file
Method 1 works but it will loop through all attributes which might not be efficient thing to do
I have create module for testing in Magento and i call change action url from browser and there SQL query for catalog/product is printed.
I user this code
<?php
class Test_Demo_IndexController extends Mage_Core_Controller_Front_Action
{
public function indexAction()
{
$this->loadLayout();
$block = $this->getLayout()->createBlock('capacityweb/Test','Test',array('template' => 'capacity/web/test.phtml'));
$this->getLayout()->getBlock('content')->append($block);
$this->renderLayout();
}
public function changeAction()
{
$this->loadLayout();
$this->renderLayout();
$action=$this->getRequest()->getParam('action');
$id=$this->getRequest()->getParam('id');
if($action!=null && $id!=null)
{
$relContact = Mage::getModel('catalog/product')->getCollection()->load($id);
}
}
}
if i use
$relContact = Mage::getModel('catalog/product')->getCollection()->load($id);
then magento display SQL query but instead of this
i use
$relContact = Mage::getModel('catalog/product')->getCollection();
then magento not display anything.
So how to fix issue with
$relContact = Mage::getModel('catalog/product')->getCollection()->load($id);
After reading your code, I think what you are trying to do is load one product, in this case you have the $id.
In this case you should use:
$product = Mage::getModel('catalog/product')->load($id)
And then you can access the desire data through:
$product->getName();
$product->getDescription()
... and so on.
Greetings
This is not an issue.
Magento uses the Lazy Loading design pattern for collections.
The sql for a collection is not executed until the collection is loaded or until you iterate through the results.
So Mage::getModel('catalog/product')->getCollection() will return an object that is an instance of Mage_Catalog_Model_Resource_Product_Collection that you can use how you need, but you don't have the products yet.
When calling Mage::getModel('catalog/product')->getCollection()->load() (by the way...the $id parameter is useless), then the query SELECT FROM catalog_product_entity..... is exectuted and in $collection->getIterator() you will have the records from the database as instances of Mage_Catalog_Model_Product.
[EDIT]
If you want a single product skip the getCollection call. You can get the instance of a product like this
$product = Mage::getModel('catalog/product')->load($id);
//changes some attributes
$product->setData('description', $description);
//or
//$product->setDescription($description);
$product->save().
See more details about it here.
You can add product to compare. I have to show the link "Add to Compare" if the product is not added already otherwise show "Compare". I have to check if the product is in comparison list.
I have list.phtml file.
I tried this but this gives all the products added in comparison list.
$_productCollection = Mage::helper('catalog/product_compare')->getItemCollection()
I can loop through the returned products and can check if the product is in this collection but I am looking for a single call which take the product id or sku and return true or false accordingly.
I also added the filter like this but does not work
$_productCollection = Mage::helper('catalog/product_compare')->getItemCollection()
->addAttributeToFilter('sku', $item->getSku());
Try to use
Mage_Catalog_Model_Product_Compare_List
and its method:
getItemCollection
Like this:
$collection = Mage::getModel('catalog/product_compare_list')->getItemCollection();
$collection->.....Additional filters go here.
Why helper didn't worked? Because collection is already loaded there:
v 1.6
public function getItemCollection()
{
if (!$this->_itemCollection) {
$this->_itemCollection = Mage::getResourceModel('catalog/product_compare_item_collection')
->useProductItem(true)
->setStoreId(Mage::app()->getStore()->getId());
if (Mage::getSingleton('customer/session')->isLoggedIn()) {
$this->_itemCollection->setCustomerId(Mage::getSingleton('customer/session')->getCustomerId());
} elseif ($this->_customerId) {
$this->_itemCollection->setCustomerId($this->_customerId);
} else {
$this->_itemCollection->setVisitorId(Mage::getSingleton('log/visitor')->getId());
}
Mage::getSingleton('catalog/product_visibility')
->addVisibleInSiteFilterToCollection($this->_itemCollection);
/* Price data is added to consider item stock status using price index */
$this->_itemCollection->addPriceData();
$this->_itemCollection->addAttributeToSelect('name')
->addUrlRewrite()
->load();
/* update compare items count */
$this->_getSession()->setCatalogCompareItemsCount(count($this->_itemCollection));
}
return $this->_itemCollection;
}
So you can load collection by model and filter itself in template or in your own custom helper - model.
In Magento, if you need to get / fetch the Shopping Cart's Item details, you can do it in any of the two possible ways, which will provide you with all the shopped Items in an array:-
$cartItems1 = $cart->getQuote()->getAllItems();
$cartItems2 = $cart->getItems()->getData();
But before using any one of the above two methods, you need to initialize the shopping cart object as:-
$cart = new Mage_Checkout_Model_Cart();
$cart->init();
Can anyone please describe in details as to what the two options provide & their differences between each other, along with their possible usage.
In any more such option is available in Magento, can anyone please highlight it?
If you look at the code of the Cart and Quote classes everything will become clear.
Here's the code for $cart->getItems():
public function getItems()
{
return $this->getQuote()->getAllVisibleItems();
}
Plain and simple - it just calls a method of the Quote object. So the question now is: What is the difference between getAllVisibleItems() and getAllItems()?
Let's look at the code of both methods:
public function getAllItems()
{
$items = array();
foreach ($this->getItemsCollection() as $item) {
if (!$item->isDeleted()) {
$items[] = $item;
}
}
return $items;
}
public function getAllVisibleItems()
{
$items = array();
foreach ($this->getItemsCollection() as $item) {
if (!$item->isDeleted() && !$item->getParentItemId()) {
$items[] = $item;
}
}
return $items;
}
The only difference: getAllVisibleItems() has an additional check for each item:
!$item->getParentItemId()
which tests if the product has a parent (in other words, it tests if it is a simple product). So this method's return array will be missing simple products, as opposed to getAllItems().
Are there any other ways to retrieve items?
One would be to directly get the product collection from the quote object:
$productCollection = $cart->getQuote()->getItemsCollection();