Skip to content

Instantly share code, notes, and snippets.

@traviswaelbro
Last active December 18, 2015 16:35
Show Gist options
  • Save traviswaelbro/9da816182097a9f3ae79 to your computer and use it in GitHub Desktop.
Save traviswaelbro/9da816182097a9f3ae79 to your computer and use it in GitHub Desktop.
Simple Magento that hooks onto the Add to Cart event. Can be used to redirect somewhere, but we use to apply relational logic for our products. Example: If product has attribute "length" over 24", then add an extra packaging fee to the cart. Updated version includes more complex business logic to add packaging fee for every 8 items for specific …
<!-- File location: /app/code/local/Mbs/Addtocart/etc/config.xml -->
<?xml version="1.0"?>
<config>
<modules>
<Mbs_Addtocartredirect>
<version>1.0</version>
</Mbs_Addtocartredirect>
</modules>
<global>
<events>
<checkout_cart_add_product_complete>
<observers>
<addtocartredirect>
<type>singleton</type>
<class>Mbs_Addtocartredirect_Model_Observer</class>
<method>addToCartRedirect</method>
</addtocartredirect>
</observers>
</checkout_cart_add_product_complete>
</events>
</global>
</config>
<?php
// File location: /app/code/local/Mbs/Addtocart/Model/Observer.php
class Mbs_Addtocartredirect_Model_Observer {
public function addToCartRedirect($observer) {
$event = $observer->getEvent();
$product = $event->getProduct();
$productId = $product->getId();
// Add hand packing fee if product fits criteria
// Checks if hand pack fee is required & if there is already a fee applied in the cart
$quote = Mage::getSingleton('checkout/session')->getQuote();
$handPackProduct = Mage::getModel('catalog/product')->load(1621);
$needsHandPack = false; // Hand packing fee charged for objects that require special packaging
$hasHandPack = false; // Does customer have Hand packing fee in cart already?
$handPackQty = 0; // How many HandPack fees are charged?
$handPackQtyNeed = 0; // How many should we have?
$multPackArray = array("ZCP1-3","ZCP1-6","ZCP2-3","ZCP2-6","ZCP3-3","ZCP3-6","SRG1900-1","SRG1900-2","SRG1900-3","SRG1900-4",); // What products require one HandPack fee per each item?
foreach ($quote->getAllItems() as $item) {
$storeId = 1; // Mbs Standoffs store code
$attributeId = 'height'; // Height (BE) attribute code
$productId = $item->getProduct()->getId();
$productSku = $item->getProduct()->getSku();
$productLength = Mage::getResourceModel('catalog/product')->getAttributeRawValue($productId, $attributeId, $storeId);
// If length is more than 24", product requires a HandPack fee
if($productLength >= 24) {
$needsHandPack = true;
$handPackQtyNeed += 1;
}
// If product has a HandPack fee already, we want to know how much
if($productId == 1621) {
$hasHandPack = true;
$handPackQty = $item->getQty();
}
// If product is one of a select few, they need a HandPack fee for each 8 items.
// This keeps track of how many we should have
if( in_array($productSku, $multPackArray) ) {
$handPackQtyNeed += $item->getQty() - 1;
}
}
// If we need more than we have already, add the difference to the cart
if($handPackQtyNeed > $handPackQty) {
$handPackQtyNeed = ceil( ($handPackQtyNeed - $handPackQty) / 8 );
$quote->addProduct($handPackProduct, $handPackQtyNeed);
}
// Else If
elseif($needsHandPack && !$hasHandPack) {
$quote->addProduct($product,1);
}
$quote->collectTotals()->save();
// $goToUrl = Mage::getBaseUrl()."accessories_section?id=".$productId."&sec=grid";
// Mage::app()->getResponse()->setRedirect($goToUrl)->sendResponse();
// exit;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment