Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //Path : app\code\local\ABC\Checkout\Helper\Cart.php
- //http://magento.stackexchange.com/questions/45132/adding-a-product-to-the-cart-via-querystring-using-sku
- class ABC_Checkout_Helper_Cart extends Mage_Checkout_Helper_Cart
- {
- public function getProductIdByParams($param, $outputFlag = true){
- $sku = trim($param['sku']);
- $_product = Mage::getModel('catalog/product')->setStoreId(Mage::app()->getStore()->getId())
- ->loadByAttribute('sku', $sku);
- if($_product){
- $productTypeId = $_product->getTypeId();
- $productId = $_product->getId();
- if($productTypeId == 'simple'){
- $result = $outputFlag ? true : $productId;
- }
- elseif($productTypeId == 'configurable'){
- $color = trim($param['color']);
- $size = trim($param['size']);
- $config = trim($param['option']);
- $configurableProduct = Mage::getModel('catalog/product_type_configurable')->setProduct($_product);
- $simpleCollections = $configurableProduct->getUsedProductCollection()
- ->addAttributeToSelect('id');
- if(! empty($color)){
- $attrColor = $_product->getResource()->getAttribute("sp_color");
- $colorId = $attrColor->usesSource() ? $attrColor->getSource()->getOptionId($color) : '';
- $simpleCollections->addAttributeToFilter('sp_color', $colorId);
- }
- if(! empty($size)){
- $attrSize = $_product->getResource()->getAttribute("sp_size");
- $sizeId = $attrSize->usesSource() ? $attrSize->getSource()->getOptionId($size) : '';
- $simpleCollections->addAttributeToFilter('sp_size', $sizeId);
- }
- if(! empty($config)){
- $attrConfig = $_product->getResource()->getAttribute("sp_config");
- $configId = $attrConfig->usesSource() ? $attrConfig->getSource()->getOptionId($config) : '';
- $simpleCollections->addAttributeToFilter('sp_config', $configId);
- }
- $simpleCollections->addFilterByRequiredOptions();
- if($simpleCollections->count() == 1){
- $productId = $simpleCollections->getFirstItem()->getId();
- $result = $outputFlag ? true : $productId;
- }
- else{
- $result = false;
- }
- }
- return $result;
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement