2014-05-19 2 views
2

Magento를 배우고 있으며 이벤트와 관찰자를 배우기 시작했습니다. 금액의 백분율은 관리 제품 영역에서 설정하여 제품에 추가됩니다. 잘 작동하지만 할인 된 가격은 제품 페이지에만 표시됩니다. 아무도 나에게 Magento를 통해 가격을 어떻게 바꿀 수 있는지 제안 해 줄 수 있습니까? 나는 쇼핑 카트에 가야 가격을 변경 의미, 주문 등 아래 옵저버를 사용하여 모든 페이지의 가격 변경 Magento

내가 사용할 수있는 방법

<?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> 

은 알려 주시기 바랍니다 관찰자

<?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의 코드입니다 Magento 전역의 새로운 할인 가격. 감사합니다

+1

이 문제를 해결하기 위해 내 답변이 도움이 되셨나요? 그것을 고쳤습니까? –

+0

@ dushyant 내가 문제를 해결할 수 있다고 생각하는 경우 –

답변

3
  1. 제품의 '카탈로그 가격 규칙'또는 '특별 가격'기능을 사용하지 않는 이유는 무엇입니까? 이것들은 이런 종류의 일을하는 inbuilt 함수입니다.

  2. 장바구니에 담기 가격 변경을 위해, 당신은 다른 이벤트를 관찰 할 필요가 있습니다. 충돌 과정 :

당신은 장바구니에 추가 이벤트 sales_quote_add_item을 잡는다 관찰자를 구축해야하고의 가격을 변경하려면 제품 페이지에 그랬던 것처럼 다음 관찰자의 PHP-물건을 할 수 있습니다 정확히이 작업은 Promotions 메뉴에서 Catalog Price Rules를 사용하는 것이 좋습니다

$observer->getEvent()->getQuoteItem()->setOriginalCustomPrice([your price]) 
+0

Re : 옵션 1, 가격 수정에 10보다 작은 정밀도가 포함되는 경우 가격에 대한 MySQL 필드 유형이 DECIMAL (12,4)' – pspahn

-1

:이 제품은 카드에 추가됩니다.

그러나 나는 당신이 학습 목적으로이 것을 이해하고 있습니다. 따라서 이벤트가 확실하지 않을 때는 파견 된 이벤트를 기록 할 수 있습니다. 에는 dispatchEvent이라는 메서드가 있습니다. 그럼 그냥 모든 파견 이벤트를 기록 할 수 :

public static function dispatchEvent($name, array $data = array()) 
{ 
    if(strpos($name, 'product') || strpos($name, 'price')) { // optional 
     Mage::log($name, null, 'events.log', true); 
    } 
    Varien_Profiler::start('DISPATCH EVENT:'.$name); 
    $result = self::app()->dispatchEvent($name, $data); 
    Varien_Profiler::stop('DISPATCH EVENT:'.$name); 
    return $result; 
} 

귀하의 로그 파일은 var/log 폴더에 생성됩니다. 물론 완료되면 Mage.php을 원래 버전으로 되 돌리는 것을 잊지 마십시오. 핵심 파일을 변경하는 것은 좋지 않습니다. 여기

7

는 솔루션

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; 
    } 
} 
+0

당신은이 답변으로 문자 그대로 많은 시간을 저축했습니다! – Karl

+0

모든 제품에 대해 특별히 선택한 제품을 위해 할 수 있습니까? – ND17

+0

예. 이 솔루션은 간단한 제품에 적용됩니다.당신은 코드 "위의 내 의견을 볼 수 있습니다 단순한 제품에 대해서만 비율 백분율 할인"나는 간단한 제품으로 설정합니다. –