Skip to content

Instantly share code, notes, and snippets.

@ashleydw
Created July 24, 2014 13:12
Show Gist options
  • Save ashleydw/5676894d366c5251a6a0 to your computer and use it in GitHub Desktop.
Save ashleydw/5676894d366c5251a6a0 to your computer and use it in GitHub Desktop.
Magento finding a product and options based on an arbitory string
$_varien = new Varien_Object(array_merge(array(
'product' => $product->getId(),
'qty' => 1,
), $optionsToUse));
/** @var Mage_Customer_Model_Customer $_customer */
foreach ($filters['customers'] as $_customer) {
/**
* try to find a cart for the customer
*/
/** @var Mage_Sales_Model_Resource_Quote_Collection $_quoteLookup */
$_quoteLookup = Mage::getModel('sales/quote')->getCollection();
$_quoteLookup->addFieldToSelect('entity_id');
$_quoteLookup->addFieldToFilter('customer_id', $_customer->getId());
$_quoteLookup->addFieldToFilter('is_active', 1);
$_quoteLookup->addOrder('entity_id', 'DESC');
$_customerQuoteId = $_quoteLookup->load()->getFirstItem()->getId();
/**
* no cart found so create one
*/
if (!$_customerQuoteId) {
/** @var Mage_Checkout_Model_Cart_Api $cart_api */
$cart_api = Mage::getModel('checkout/cart_api');
$_customerQuoteId = $cart_api->create($this->_getStoreId());
}
$_currentCart = Mage::getModel('sales/quote')->loadByIdWithoutStore($_customerQuoteId);
$_currentCart->setIsActive(true);
/**
* Cloning the product is required as it gets modified somewhere down the code pile
*/
$_temp = clone $product;
$res = $_currentCart->addProduct($_temp, $_varien);
if(is_string($res)) // a string thrown is an error
$this->_fault('cart_error', $res);
$_currentCart->setCustomer($_customer);
$_currentCart->assignCustomer($_customer);
$_currentCart->collectTotals();
$_currentCart->save();
}
$filters['options'] = 'large blue';
/** @var Mage_Catalog_Model_Product $product */
if($product->isConfigurable()) {
$_bestSuitedAttributes = [];
$_tmpOptions = $filters['options'];
/** @var Mage_Catalog_Model_Product_Type_Configurable_Attribute $_attr */
foreach ($product->getTypeInstance(true)->getConfigurableAttributes($product) as $_attr) {
$_prices = $_attr->getPrices();
if (is_array($_prices))
foreach ($_prices as $_item) {
foreach ($_tmpOptions as $_opt) {
if ($_opt == strtolower($_item['label'])) {
$_bestSuitedAttributes[$_opt] = array(
'attribute_id' => $_attr->getAttributeId(),
'attribute_code' => $_attr->getAttributeCode(),
'attribute_label' => $_attr->getLabel(),
'option_id' => $_item['value_index'],
);
unset($_tmpOptions[$_opt]);
}
}
}
}
/**
* If these two are equal, we found the correct product!
* convert the attributes to the array required later
*/
$optionsToUse = array('super_attribute' => array());
if (count($_bestSuitedAttributes) == count($filters['options'])) {
foreach ($_bestSuitedAttributes as $_attr)
$optionsToUse['super_attribute'][(int) $_attr['attribute_id']] = (int) $_attr['option_id'];
}
if (empty($optionsToUse['super_attribute']))
$this->_fault('product_not_found', "Configurable product found, but options didn't match");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment