Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
6.9k views
in Technique[技术] by (71.8m points)

php - How to get the URL to a configurable item in Magento?

I am using magento and creating a product finder. Is there a way I can link to a configurable item example: mystore.com/bedding-sheets

lets say I want to send a friend a link to the sheet that is 250*250cm i would think the link was maybe: mystore.com/bedding-sheets?attribute496=1 (except this is not the case)

<dl class="last">
    <dt><label>Size<span class="required">?*</span></label></dt>
     <dd class="last">
     <select class="required-entry super-attribute-select" id="attribute496" name="super_attribute[496]">
      <option value="">Choose option...</option>
      <option value="4">200*200cm</option>
      <option value="3">200*230cm</option>
      <option value="2">200*250cm</option>
      <option value="1">250*250cm</option></select>
    </dd>
</dl>

Anyone know if it's possible to create such a link?

EDIT: So i was able to find out how to add an item to my cart using /checkout/cart/add?product=47&qty=1&super_attribute[496]=4

however I want the user to get a preview of the item they will receive, instead of just dumping it into the cart.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It's not possible to do this without making changes to Magento.

Now suppose you wanted to predefine the selected options of a select box in the url:

First you need to overwrite the block Mage_Catalog_Block_Product_View_Options_Type_Select. (I assume that you have created your own module already - I you haven't and need help with that let me know)

To do this you need to add this entry to your config.xml:

<config>
    <global>
        <blocks>
            <catalog>
                <rewrite>
                    <product_view_options_type_select>YourCompany_YourModuleName_Product_View_Options_Type_Select</product_view_options_type_select>
                </rewrite>
            </catalog>
        </blocks>
    </global>
</config>

Next add the class YourCompany_YourModuleName_Product_View_Options_Type_Select which needs to extend Mage_Catalog_Block_Product_View_Options_Type_Select.

In this class you must now overwrite the function getValuesHtml(). To start off you should copy it from the class you're extending.

In this function you should find this foreach:

    foreach ($_option->getValues() as $_value) {
        $priceStr = $this->_formatPrice(array(
            'is_percent' => ($_value->getPriceType() == 'percent') ? true : false,
            'pricing_value' => $_value->getPrice(true)
        ), false);
        $select->addOption(
            $_value->getOptionTypeId(),
            $_value->getTitle() . ' ' . $priceStr . ''
        );
    }

After this you add:

$standardValue = $this->getRequest()->getParam($_option->getid());
//Mage::log("Option Name: "$_option->getid());
$select->setValue($standardValue);

This should do it. Unfortunately I can't test this right now. So let me know if you run into troubles.

I'm not sure if the $_option->getid() is the right name for your parameter. But you could try to find that out if you comment in the one line I commented out for you in the code listing from above. In your Magento installation open the file var/log/system.log where the name of your url parameters should now appear.

Now that you know the how to name the url parameters you can do exactly what you wanted: url/to/product?option_id=value_id

PS: If you ask yourself why we create a new class instead of changing it directly in the Magento core: We do this to prevent problems when updating to a new version of Magento.

I hope I could help.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...