Skip to content

Instantly share code, notes, and snippets.

@enru
Last active August 29, 2016 12:13
Show Gist options
  • Select an option

  • Save enru/d788c8c6f59538ed9f96 to your computer and use it in GitHub Desktop.

Select an option

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
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);
}
@seansan

seansan commented Aug 29, 2016

Copy link
Copy Markdown

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment