Javascript 改变可配置产品的价格

Javascript 改变可配置产品的价格,javascript,magento,product,configurable,Javascript,Magento,Product,Configurable,我正在寻找一种方法,在选择了所有必需的选项后,为可配置产品的价格增加价值。我需要匹配所选简单产品的完整SKU,因此每个选项的价格不合适 我已经用SKU-price对构建了一个JSON,现在正在寻找一个JS事件来干净地完成任务,并在添加到购物车和结帐阶段保留新的价格 提前感谢您提供的任何见解您可以使用observer类收听checkout\u cart\u product\u add\u after,并使用产品的超级模式根据报价项目设置自定义价格 在/app/code/local/{namespa

我正在寻找一种方法,在选择了所有必需的选项后,为可配置产品的价格增加价值。我需要匹配所选简单产品的完整SKU,因此每个选项的价格不合适

我已经用SKU-price对构建了一个JSON,现在正在寻找一个JS事件来干净地完成任务,并在添加到购物车和结帐阶段保留新的价格


提前感谢您提供的任何见解

您可以使用observer类收听checkout\u cart\u product\u add\u after,并使用产品的超级模式根据报价项目设置自定义价格

在/app/code/local/{namespace}/{yourmodule}/etc/config.xml中:

<config>
    ...
    <frontend>
        ...
        <events>
            <checkout_cart_product_add_after>
                <observers>
                    <unique_event_name>
                        <class>{{modulename}}/observer</class>
                        <method>modifyPrice</method>
                    </unique_event_name>
                </observers>
            </checkout_cart_product_add_after>
        </events>
        ...
    </frontend>
    ...
</config>
然后在/app/code/local/{namespace}/{yourmodule}/Model/Observer.php创建一个观察者类

class <namespace>_<modulename>_Model_Observer
{
    public function modifyPrice(Varien_Event_Observer $obs)
    {
        // Get the quote item
        $item = $obs->getQuoteItem();
        // Ensure we have the parent item, if it has one
        $item = ( $item->getParentItem() ? $item->getParentItem() : $item );
        // Load the custom price
        $price = $this->_getPriceByItem($item);
        // Set the custom price
        $item->setCustomPrice($price);
        $item->setOriginalCustomPrice($price);
        // Enable super mode on the product.
        $item->getProduct()->setIsSuperMode(true);
    }

    protected function _getPriceByItem(Mage_Sales_Model_Quote_Item $item)
    {
        $price;

        //use $item and maybe your json object to determine the correct price

        return $price;
    }

}

这将从后端处理价格变化。至于javascript,我很抱歉,但我不太确定

您可以使用observer类来监听checkout\u cart\u product\u add\u after,并使用产品的超级模式来设置报价项的自定义价格

在/app/code/local/{namespace}/{yourmodule}/etc/config.xml中:

<config>
    ...
    <frontend>
        ...
        <events>
            <checkout_cart_product_add_after>
                <observers>
                    <unique_event_name>
                        <class>{{modulename}}/observer</class>
                        <method>modifyPrice</method>
                    </unique_event_name>
                </observers>
            </checkout_cart_product_add_after>
        </events>
        ...
    </frontend>
    ...
</config>
然后在/app/code/local/{namespace}/{yourmodule}/Model/Observer.php创建一个观察者类

class <namespace>_<modulename>_Model_Observer
{
    public function modifyPrice(Varien_Event_Observer $obs)
    {
        // Get the quote item
        $item = $obs->getQuoteItem();
        // Ensure we have the parent item, if it has one
        $item = ( $item->getParentItem() ? $item->getParentItem() : $item );
        // Load the custom price
        $price = $this->_getPriceByItem($item);
        // Set the custom price
        $item->setCustomPrice($price);
        $item->setOriginalCustomPrice($price);
        // Enable super mode on the product.
        $item->getProduct()->setIsSuperMode(true);
    }

    protected function _getPriceByItem(Mage_Sales_Model_Quote_Item $item)
    {
        $price;

        //use $item and maybe your json object to determine the correct price

        return $price;
    }

}

这将从后端处理价格变化。至于javascript,我很抱歉,但我不太确定

在我实现JS部分时接受了你的回答:一个问题是,当匿名客户将产品添加到购物车,然后登录到商店时,更新自定义价格,因此必须重新计算价格,你对此有什么想法吗?你是如何做JS的?我很想看看你的解决方案是什么。至于登录问题,客户登录时是否重置了价格?也许有更好的事件可以听,或者你可以观察登录事件并迭代每个项目。我已经解决了sales_quote_merge_after event中的定价问题,这是匿名客户购物车转换为登录客户的地方。至于JS,我已经构建了一个完整的JSON选项id到管理员值对,从JSONA收集完整的简单SKU和最终价格的附加值,当我设法实现JS部分时,接受了你的答案:但还有一个问题-当匿名客户将产品添加到购物车,然后登录到商店时,更新自定义价格,因此必须重新计算价格,你对此有什么想法吗?你是如何做JS的?我很想看看你的解决方案是什么。至于登录问题,客户登录时是否重置了价格?也许有更好的事件可以听,或者你可以观察登录事件并迭代每个项目。我已经解决了sales_quote_merge_after event中的定价问题,这是匿名客户购物车转换为登录客户的地方。至于JS,我已经构建了一个完整的JSON选项id到管理员值对,收集了完整的简单SKU,并再次从JSON中为最终价格增加了价值