且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何获取Magento中可配置项的URL?

更新时间:2023-11-30 12:29:28

不对Magento进行更改就无法做到这一点.

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:

首先,您需要覆盖块 Mage_Catalog_Block_Product_View_Options_Type_Select . (我假设您已经创建了自己的模块-我还没有,并且需要帮助,请告诉我)

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)

为此,您需要将此条目添加到您的config.xml中:

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>

接着添加类 YourCompany_YourModuleName_Product_View_Options_Type_Select 其中需要延长的 Mage_Catalog_Block_Product_View_Options_Type_Select

在此类中,您现在必须覆盖函数 getValuesHtml().首先,您应该从要扩展的类中复制它.

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

在此功能中,您应该找到此foreach:

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 . ''
        );
    }

在此之后,您添加:

$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.

我不确定$_option->getid()是否是您参数的正确名称.但是,如果您在上面的代码清单中为我注释掉的一行中注释,则可以尝试找出答案. 在您的Magento安装中,打开文件 var/log/system.log ,其中现在应显示您的url参数的名称.

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.

现在,您知道如何命名url参数了,您可以执行所需的操作: url/to/product?option_id = value_id

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:如果您问自己为什么我们创建一个新类而不是直接在Magento核心中更改它:我们这样做是为了防止在更新到新版本的Magento时出现问题.

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.

希望我能帮上忙.