I have created a new product type in Magento. However, I am having difficulty adding all of its associated products to the sales_flat_quote_item table. I only want the associated products added to the table and only the main parent product visible in the cart.
I am real close to achieving this. Right now only the parent item is visible in the cart when adding it to the cart. However, only one of it's associated products are being listed in the above mentioned table.
Here is a snippet of my code:
class Namespace_Module_Model_Product_Type_Custom extends Mage_Catalog_Model_Product_Type_Abstract {
........
protected function _prepareProduct(Varien_Object $buyRequest, $product, $processMode)
{
$qty = $buyRequest['qty'];
$associatedQty = $buyRequest['associated_qty'];
if($qty >= 1) {
$result = parent::_prepareProduct($buyRequest, $product, $processMode);
if (is_array($result)) {
$product = $this->getProduct($product);
foreach($buyRequest['associated'] as $associated){
if($associated){
$subProducts[] = Mage::getModel('catalog/product')->load($associated);
}
}
foreach($subProducts as $subProduct){
if($subProduct){
$product->setCartQty($qty);
$product->addCustomOption('product_qty_'.$subProduct->getId(), $associatedQty[$subProduct->getId()], $subProduct);
$product->addCustomOption('associated_product_' . $subProduct->getId(), $associatedQty[$subProduct->getId()]);
}
}
$_result = $subProduct->getTypeInstance(true)->_prepareProduct(
$buyRequest,
$subProduct,
$processMode
);
if (!isset($_result[0])) {
return Mage::helper('checkout')->__('Cannot add the item to shopping cart');
}
$_result[0]->setParentProductId($product->getId())
->addCustomOption('parent_product_id', $product->getId());
$result[] = $_result[0];
return $result;
} else {
return $this->getSpecifyOptionMessage();
}
} else {
return $this->getQtyMessage();
}
}
........
}
Right now only the associated product '53' is being added as a child product. I am still missing the other two. Basically, the foreach($subProducts as $subProduct) loop will loop 3 three times with the three associated products. I am assuming somewhere along the lines in Magento it is only using the last looped product.
Any advice or help with this would be great. Thanks in advance!
Got it figured out. I just had to shift the following into the foreach loop instead of outside of it.
$_result[0]->setParentProductId($product->getId())
->addCustomOption('parent_product_id', $product->getId());
$result[] = $_result[0];
Related
I need to change the productname visible for the customer after adding it to the cart. Also it should show on invoice, order email and everything else visible for the customer.
I made an extension with observer checkout_cart_product_add_after
$item = $observer->getEvent()->getData('quote_item');
$item = ( $item->getParentItem() ? $item->getParentItem() : $item );
$item->getProduct()->setName('this is the productname');
$item->getProduct()->setIsSuperMode(true);
But the original productname is still visible on minicart, cartpage, etc.
The 'add to cart message' is the only place where the new changed productname is visible. What should I change?
use this event "sales_quote_item_set_product"
and in observer set name like this:
/* For minicart and cart */
$item->getProduct()->setName('Custom Name');
/* For checkout page cart items */
$item->setName('Custom Name');
$item->getProduct()->setIsSuperMode(true);
Found the solutions myself.
Hope this helps anyone searching for it, or if you have comments how it could be done a better way, please let me know.
Created a plugin with this code:
class Item
{
public function afterSetProduct(
\Magento\Quote\Model\Quote\Item $subject,
$result
){
$subject->setName('New Name');
}
}
So this sets a new name to the quote item, visible in everything, excepting cart and mini cart.
Now I have got my new productname visible in:
- Added to cart message
- Quote-item, even in admin and invoices
But not in:
- Mini cart
- Cartpage
For the cart page:
I changed output of function getProduct in Magento\Checkout\Block\Cart\Item\Renderer:
class Renderer
{
public function afterGetProductName(
\Magento\Checkout\Block\Cart\Item\Renderer $subject,
$result
){
return 'this is the name';
}
}
For minicart, also with plugin:
class DefaultItem
{
public function aroundGetItemData(
\Magento\Checkout\CustomerData\AbstractItem $subject,
\Closure $proceed,
\Magento\Quote\Model\Quote\Item $item
){
$data = $proceed($item);
$data['product_name'] = 'this is the productname';
return $data;
}
Ideally we would like to prevent any items on an order from being shipped if they do not have an invoice created.
At the moment Magento (1.7.0.2) allows the creation of a shipment even if there has been no invoices created for the order. I realise this is by design for site owners who wish to ship first and invoice afterwards but our workflow requires payment first on creation of the invoice before allowing a shipment.
Could anyone please offer any suggestions code wise on what to change? I think the correct file would be /app/code/local/Mage/Sales/Model/Order/Shipment.php and the code we require to change would be below but im not sure how to check each item for an invoice individually.
Edit - Would the following code work well?
protected function _beforeSave()
{
if ((!$this->getId() || null !== $this->_items) && !count($this->getAllItems())) {
Mage::throwException(
Mage::helper('sales')->__('Cannot create an empty shipment.')
);
}
$order = $this->getOrder();
if ($order->canInvoice()) {
Mage::throwException(
Mage::helper('sales')->__('Cannot ship items without an Invoice.')
);
}
You can get some hints about the code to use by looking at this piece of code from: \app\code\core\Mage\Sales\Model\Order.php in the public function canInvoice():
public function canInvoice()
{
if ($this->canUnhold() || $this->isPaymentReview()) {
return false;
}
$state = $this->getState();
if ($this->isCanceled() || $state === self::STATE_COMPLETE || $state === self::STATE_CLOSED) {
return false;
}
if ($this->getActionFlag(self::ACTION_FLAG_INVOICE) === false) {
return false;
}
foreach ($this->getAllItems() as $item) {
if ($item->getQtyToInvoice()>0 && !$item->getLockedDoInvoice()) {
return true;
}
}
return false;
}
How do I add custom sort option in Magento. I want to add Best Sellers, Top rated and exclusive in addition to sort by Price. Please help
For Best Sellers
haneged in code/local/Mage/Catalog/Block/Product/List/Toolbar.php method setCollection to
public function setCollection($collection) {
parent::setCollection($collection);
if ($this->getCurrentOrder()) {
if($this->getCurrentOrder() == 'saleability') {
$this->getCollection()->getSelect()
->joinLeft('sales_flat_order_item AS sfoi', 'e.entity_id = sfoi.product_id', 'SUM(sfoi.qty_ordered) AS ordered_qty')
->group('e.entity_id')->order('ordered_qty' . $this->getCurrentDirectionReverse());
} else {
$this->getCollection()
->setOrder($this->getCurrentOrder(), $this->getCurrentDirection());
}
}
return $this;
}
After setCollection I added this method:
public function getCurrentDirectionReverse() {
if ($this->getCurrentDirection() == 'asc') {
return 'desc';
} elseif ($this->getCurrentDirection() == 'desc') {
return 'asc';
} else {
return $this->getCurrentDirection();
}
}
And finally I changed mehod setDefaultOrder to
public function setDefaultOrder($field) {
if (isset($this->_availableOrder[$field])) {
$this->_availableOrder = array(
'name' => $this->__('Name'),
'price' => $this->__('Price'),
'position' => $this->__('Position'),
'saleability' => $this->__('Saleability'),
);
$this->_orderField = $field;
}
return $this;
}
for Top rated
http://www.fontis.com.au/blog/magento/sort-products-rating
try above code.
for date added
Magento - Sort by Date Added
i am not associate with any of the above link for any work or concern it is just for knowledge purpose and to solve your issue.
hope this will sure help you.
Thanks for your answer, Anuj, that was the best working module I could find so far.
Just add an extra bit to your code in order to solve no pagination caused by 'group by'
Copy '/lib/varien/data/collection/Db.php'
To 'local/varien/data/collection/Db.php'.
Change the getSize function to
public function getSize()
{
if (is_null($this->_totalRecords)) {
$sql = $this->getSelectCountSql();
//$this->_totalRecords = $this->getConnection()->fetchOne($sql, $this->_bindParams); //============================>change behave of fetchOne to fetchAll
//got array of all COUNT(DISTINCT e.entity_id), then sum
$result = $this->getConnection()->fetchAll($sql, $this->_bindParams);
foreach ($result as $row) {//echo'<pre>'; print_r($row);
$this->_totalRecords += reset($row);
}
}
return intval($this->_totalRecords);
}
Hope it could help anyone.
update
The filter section need to be updated as well, otherwise just showing 1 item on all filter.
and the price filter will not be accurate.
What you need to do it to modify core/mage/catalog/model/layer/filter/attribute.php and price.php
attribute.php getCount() on bottom
$countArr = array();
//print_r($connection->fetchall($select));
foreach ($connection->fetchall($select) as $single)
{
if (isset($countArr[$single['value']]))
{
$countArr[$single['value']] += $single['count'];
}
else
{
$countArr[$single['value']] = $single['count'];
}
}
//print_r($countArr);//exit;
return $countArr;
//return $connection->fetchPairs($select);
Price.php getMaxPrice
$maxPrice = 0;
foreach ($connection->fetchall($select) as $value)
{
if (reset($value) > $maxPrice)
{
$maxPrice = reset($value);
}
}
return $maxPrice;
If you are having the same problem and looking for the question, you will know what I meant.
Good luck, spent 8 hours on that best sell function.
Update again,
just found another method to implement
using cron to collect best sale data daily saved in a table that includes product_id and calculated base sale figure.
then simply left join, without applying 'group by'
that means core functions do not need to changed and speed up the whole sorting process.
Finally finished! hehe.
To sort out pagination issue for custom sorting collection rewrite the resource model of it's collection from
app\code\core\Mage\Catalog\Model\Resource\Product\Collection.php
And modify below method from core
protected function _getSelectCountSql($select = null, $resetLeftJoins = true)
{
$this->_renderFilters();
$countSelect = (is_null($select)) ?
$this->_getClearSelect() :
$this->_buildClearSelect($select);
/*Added to reset count filters for Group*/
if(count($countSelect->getPart(Zend_Db_Select::GROUP)) > 0) {
$countSelect->reset(Zend_Db_Select::GROUP);
}
/*Added to reset count filters for Group*/
$countSelect->columns('COUNT(DISTINCT e.entity_id)');
if ($resetLeftJoins) {
$countSelect->resetJoinLeft();
}
return $countSelect;
}
Above will solve count issue for custom sorting collection.
I have developed a custom module to meet my project requirements using Alan Storms tutorial for creating modules in magento.
I had the requirement of changing the price attribute dynamically on frontend based on a livefeed. Everysecond the feed is updated so every time the page refreshes a new price must be displayed for each product on the site.
I have override the product module and the price modules for this purpose. The issue is with tier pricing. When tier pricing comes into place I need to calculate the tier-price based on the live price.
For this also I managed to change using the price_type class override.
Now whenever an item is added to cart the tier-pricing was not working for that I wrote event_trigger ie an Observer which updates the tier_pricing on the event "checkout_cart_save_before" and here's my code
class My_Custom_Model_Observer extends Varien_Event_Observer
{
public function __construct()
{
}
public function updateCartBasedOnLiveFeed($observer)
{
foreach ($observer->getCart()->getQuote()->getAllVisibleItems() as $item /* #var $item Mage_Sales_Model_Quote_Item */)
{
$tierPrices = array();
$tierPrices = $item->getProduct()->getTierPrice();
$itemPrice = $item->getProduct()->getPrice();
$i=0;
foreach($tierPrices as $key => $tierPrice)
{
if(!is_numeric($key))
{
$updatedTierPrice = $itemPrice - ($itemPrice * ($tierPrice['price']/100));
$tierPrices[$key]['price'] = $updatedTierPrice;
$tierPrices[$key]['website_price'] = $updatedTierPrice;
}
else
{
if($tierPrice['price'] > 0)
{
$updatedTierPrice = $itemPrice - ($itemPrice * ($tierPrice['price']/100));
$tierPrice['price'] = $updatedTierPrice;
$tierPrice['website_price'] = $updatedTierPrice;
$tierPrices[$i] = $tierPrice;
$i++;
}
}
}
$item->getProduct()->setData('tier_price',$tierPrices);
}
}
}
The above code works excellently in cart page. But when it comes to checkout page. It works for a single item and when tier-pricing comes into play it does apply cart prices.
Please help me with this.
I also tried using other events along with the above event.
Event: sales_quote_save_before
public function updateQuoteLive($observer)
{
$tierPrices = array();
$quote_item = $observer->getEvent()->getQuote;
$itemPrice = $quote_item->getProduct()->getPrice();
$tierPrices = $quote_item->getProduct()->getTierPrice();
$tierPricesSize = sizeof($tierPrices);
for($i=0;$i<$tierPricesSize;$i++)
{
$updatedTierPrice = $itemPrice - ($itemPrice * ($tierPrices[$i]['price']/100));
$tierPrices[$i]['price'] = $updatedTierPrice;
$tierPrices[$i]['website_price'] = $updatedTierPrice;
}
$quote_item->getProduct()->setData('tier_price',$tierPrices);
}
When I tried to print the getQuote() function available in Quote.php I find that the tier prices there are not the ones which I updated using the first event. So I think I need to update the price before saving the quote. Please any one help me and show the correct direction.
Please help me with this I am missing some important step. Any help is greatly appreciated.
Thanks in advance.
It might be better off "saving" the new price in to the database when you update.
Try something along the lines of:
$product = $observer->getProduct();
$procuct->setPrice($updatedPrice);
This way when it comes to checkout it will be pulling in the correct price from the database (and avoids the headache of correcting it "mid-flight"
i realized such a project like you. I have no sales_quote_save_before Observer. I only use the checkout_cart_save_before. Based on the session the price will be setted.
I realized that like this way:
public function updatePrice( $observer )
{
try {
$cart = $observer->getCart();
$items = $cart->getItems();
foreach($items as $item)
{
$item->setCustomPrice($price);
$item->setOriginalCustomPrice($price);
}
} catch ( Exception $e )
{
Mage::log( "checkout_cart_save_before: " . $e->getMessage() );
}
}
I calcute the tierprices on the fly and with this Observer. All prices will be set up correct in the qoute.
Maybe you should try this way.
Regards boti
At last figured out the issue and got the solution.
The problem was that in cart page or checkout page when the getTierPrice() function is called, which is present in /app/code/core/Mage/Catalog/Product.php. It takes one parameter named $qty which is by default null. This function in turn calls the function getTierPrice which is present in /app/code/core/Mage/Type/Price.php file which takes two parameters $qty and $productObject. By default $qty is null and when it is null the function returns an array of tier_prices. But when the $qty value is passed then the function returns a single for that particular quantity.
So, I wrote my own custom function which calculates the tier prices based no my requirements like
I overridden both the core files with my custom module following Alan Storm's tutorials.
I've extended Mage_Catalog_Model_Product with My_CustomModule_Model_Product class and
then
Mage_Catalog_Model_Product_Type_Price with My_CustomModule_Model_Price
And then in /app/code/local/My/Custommodule/Model/Product.php
I added my custom code like
public function getTierPrice($qty=null)
{
if($qty)
{
return $this->getPriceModel()->getCustomTierPrice($qty, $this);
}
return $this->getPriceModel()->getTierPrice($qty, $this);
}
Then in /app/code/local/My/Custommodule/Model/Price.php
<?php
public function getCustomTierPrice($qty = null, $product)
{
$allGroups = Mage_Customer_Model_Group::CUST_GROUP_ALL;
$prices = $product->getData('tier_price');
if (is_null($prices)) {
$attribute = $product->getResource()->getAttribute('tier_price');
if ($attribute) {
$attribute->getBackend()->afterLoad($product);
$prices = $product->getData('tier_price');
}
}
foreach($prices as $key => $customPrices)
{
if($prices[$key]['price'] < 1)
{
$prices[$key]['price'] = abs($product->getPrice() - ($productPrice * ($customPrices['price']/100)));
$prices[$key]['website_price'] = $prices[$key]['price'];
}
}
}
which retured a customized value when $qty is passed and voila it worked.
I just posed this answer so that any one else who has similar requirement may get benefited with this.
?>
I'm getting this error after add/remove item to/from cart. Add to Cart button does ajax call to add item to the cart. This kind of json string which will be used in top cart:
I'm stuck. Can you tell me where should I start from to debug?
the "Add to Cart" button does not work asynchrounisly in Magento's default behaviour. That means that you installed/developped a module to do this. For us to help you, we need to know what is it.
Anyways, this looks like a Zend_Dump or maybe a die: make a search in your files for these strings and see what comes up
Use Netbeans and Xdebug.
http://wiki.netbeans.org/HowToConfigureXDebug
Place a breakpoint on the /app/code/core/Mage/Sales/Model/Quote.php inside this function:
/**
* Adding catalog product object data to quote
*
* #param Mage_Catalog_Model_Product $product
* #return Mage_Sales_Model_Quote_Item
*/
protected function _addCatalogProduct(Mage_Catalog_Model_Product $product, $qty = 1)
{
$newItem = false;
$item = $this->getItemByProduct($product);
if (!$item) {
$item = Mage::getModel('sales/quote_item');
$item->setQuote($this);
if (Mage::app()->getStore()->isAdmin()) {
$item->setStoreId($this->getStore()->getId());
}
else {
$item->setStoreId(Mage::app()->getStore()->getId());
}
$newItem = true;
}
/**
* We can't modify existing child items
*/
if ($item->getId() && $product->getParentProductId()) {
return $item;
}
$item->setOptions($product->getCustomOptions())
->setProduct($product);
// Add only item that is not in quote already (there can be other new or already saved item
if ($newItem) {
$this->addItem($item);
}
return $item;
}
HTH