gpt4 book ai didi

magento - 使用观察者 Magento 更改所有页面上的产品价格

转载 作者:行者123 更新时间:2023-12-02 15:27:41 25 4
gpt4 key购买 nike

我正在学习 Magento,我已经开始学习事件和观察者。通过在管理产品区域中设置,将一定百分比的金额添加到产品中。它工作正常,但折扣价格仅显示在产品页面上。有人可以建议我如何通过 Magento 更改价格吗?我的意思是更改价格应该进入购物车、订单等。

下面是观察者的代码

<?php
class Xyz_Catalog_Model_Price_Observer
{
public function __construct()
{
}
/**
* Applies the special price percentage discount
* @param Varien_Event_Observer $observer
* @return Xyz_Catalog_Model_Price_Observer
*/
public function apply_discount_percent($observer)
{
$event = $observer->getEvent();
$product = $event->getProduct();
// process percentage discounts only for simple products
if ($product->getSuperProduct() && $product->getSuperProduct()->isConfigurable()) {
} else {
$percentDiscount = $product->getPercentDiscount();

if (is_numeric($percentDiscount)) {
$today = floor(time()/86400)*86400;
$from = floor(strtotime($product->getSpecialFromDate())/86400)*86400;
$to = floor(strtotime($product->getSpecialToDate())/86400)*86400;

if ($product->getSpecialFromDate() && $today < $from) {
} elseif ($product->getSpecialToDate() && $today > $to) {
} else {
$price = $product->getPrice();
$finalPriceNow = $product->getData('final_price');

$specialPrice = $price - $price * $percentDiscount / 100;

// if special price is negative - negate the discount - this may be a mistake in data
if ($specialPrice < 0)
$specialPrice = $finalPriceNow;

if ($specialPrice < $finalPriceNow)
$product->setFinalPrice($specialPrice); // set the product final price
}
}
}
return $this;
}
}

config.xml

<?xml version="1.0"?>
<config>
<global>
<models>
<xyzcatalog>
<class>Xyz_Catalog_Model</class>
</xyzcatalog>
</models>
<events>
<catalog_product_get_final_price>
<observers>
<xyz_catalog_price_observer>
<type>singleton</type>
<class>Xyz_Catalog_Model_Price_Observer</class>
<method>apply_discount_percent</method>
</xyz_catalog_price_observer>
</observers>
</catalog_product_get_final_price>
</events>
</global>
</config>

请告知我如何在整个 Magento 中使用新的折扣价格。谢谢

最佳答案

这是解决方案

config.xml

<?xml version="1.0"?>
<config>
<modules>
<Seta_DiscountPrice>
<version>0.1.0</version> <!-- Version number of your module -->
</Seta_DiscountPrice>
</modules>
<global>
<models>
<setadiscountprice>
<class>Seta_DiscountPrice_Model</class>
</setadiscountprice>
</models>
<events>
<catalog_product_get_final_price>
<observers>
<seta_discountprice_price_observer>
<type>singleton</type>
<class>Seta_DiscountPrice_Model_Price_Observer</class>
<method>apply_10</method>
</seta_discountprice_price_observer>
</observers>
</catalog_product_get_final_price>
<catalog_product_collection_load_after>
<observers>
<seta_discountprice_price_observer>
<type>singleton</type>
<class>Seta_DiscountPrice_Model_Price_Observer</class>
<method>apply_view</method>
</seta_discountprice_price_observer>
</observers>
</catalog_product_collection_load_after>
</events>
</global>
</config>

Observer.php

<?php

class Seta_DiscountPrice_Model_Price_Observer
{
public function __construct()
{
}
/**
* Applies the special price percentage discount
* @param Varien_Event_Observer $observer
* @return Seta_DiscountPrice_Model_Price_Observer
*/
public function apply_10($observer)
{
$event = $observer->getEvent();
$product = $event->getProduct();
// process percentage discounts only for simple products
if ($product->getSuperProduct() && $product->getSuperProduct()->isConfigurable()) {
} else {

$product->setFinalPrice(10);
}
return $this;
}
public function apply_view($observer)
{
$event = $observer->getEvent();
$myCustomPrice = 10;
$products = $observer->getCollection();

foreach( $products as $product )
{
$product->setPrice( $myCustomPrice );
$product->setFinalPrice( $myCustomPrice );
}
return $this;
}
}

关于magento - 使用观察者 Magento 更改所有页面上的产品价格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23730150/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com