Last active
August 29, 2016 12:13
-
-
Save enru/d788c8c6f59538ed9f96 to your computer and use it in GitHub Desktop.
gets a Magento parent product url with hash to pre-select configurable product option
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function getProductUrlWithAttributes($product, $storeId) { | |
| $url = $product->setStoreId($storeId)->getProductUrl(); | |
| $urlHash = array(); | |
| $parentIds = Mage::getModel('catalog/product_type_configurable')->getParentIdsByChild($product->getId()); | |
| if(isset($parentIds[0])) { | |
| $parent = Mage::getModel('catalog/product')->load($parentIds[0]); | |
| $url = $parent->setStoreId($storeId)->getProductUrl(); | |
| $attrs = $parent->getTypeInstance(true)->getConfigurableAttributes($parent); | |
| foreach ($attrs as $attribute) { | |
| $productAttribute = $attribute->getProductAttribute(); | |
| $productAttributeId = $productAttribute->getId(); | |
| if($attributeValue = $product->getData($productAttribute->getAttributeCode())) { | |
| $urlHash[$productAttributeId] = $attributeValue; | |
| } | |
| } | |
| } | |
| if(count($urlHash) > 0) { | |
| $url .= '#'.http_build_query($urlHash, null, ','); | |
| } | |
| return substr($url, 0, 2000); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Suppose the attribute id is known and is "my_attr_sizes"? After that I loop through the child products collection to build the URL. I would then have something like $product_url + # + $attr_id + = + $attr_id_of_product_attr_value ==> How could I do this?