最新消息:觉得本站不错的话 记得收藏哦 博客内某些功能仅供测试 讨论群:135931704 快养不起小站了 各位有闲钱就打赏下把 My Email weicots#gmail.com Please replace # with @

Magento 1x 全局价格规则控制 以及快速定位 文件及JS

Magento 资料整理 ajiang-tuzi 4265浏览

ups
自定义 购物车规则 观测者模式

XML 写法

   <!--块 函数-->
        <blocks>
            <weicot>
                <class>Aps_Weicot_Block</class>
            </weicot>
        </blocks>
<.....>
        <events>
            <checkout_cart_product_add_after>
                <observers>
                    <produts_price_update>
                        <class>Aps_Weicot_Model_Observer</class>
                        <method>productsPriceUpdate</method>
                    </produts_price_update>
                </observers>
            </checkout_cart_product_add_after>
        </events>

<.....>

        <models>

购物车页面价格控制

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/4/28 0028
 * Time: 08:52
 */
class Aps_Weicot_Model_Observer{
    public function productsPriceUpdate(Varien_Event_Observer $obs)
    {


       // var_dump($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


       // var_dump($item->getProduct()->getCategoryIds());
       // die('here');

        Mage::log($item->getProduct()->getCategoryIds(),null,"catalog.log");
        if(
            $item->getProduct()->getCategoryIds()[0]<'60'
        ){
            $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="99";

        //use $item to determine your custom price.

        return $price;
    }

}

产品页面价格规则 调用的 JSON 数据

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/4/28 0028
 * app\design\frontend\rwd\default\template\catalog\product\view.phtml
 */


/**
 * Product view template
 *
 * @see Mage_Catalog_Block_Product_View
 * @see Mage_Review_Block_Product_View
 */
?>
<?php $_helper = $this->helper('catalog/output'); ?>
<?php $_product = $this->getProduct(); ?>
<script type="text/javascript">
    var optionsPrice = new Product.OptionsPrice(<?php echo $this->getJsonConfig() ?>);
</script>

通过全局搜索 function getJsonConfig() 可得调用方法

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/4/28 0028
 * app\code\core\Mage\Catalog\Block\Product\View.php
 */

public function getJsonConfig()
    {
        $config = array();
        if (!$this->hasOptions()) {
            return Mage::helper('core')->jsonEncode($config);
        }

        $_request = Mage::getSingleton('tax/calculation')->getDefaultRateRequest();
        /* @var $product Mage_Catalog_Model_Product */
        $product = $this->getProduct();
        $_request->setProductClassId($product->getTaxClassId());
        $defaultTax = Mage::getSingleton('tax/calculation')->getRate($_request);

        $_request = Mage::getSingleton('tax/calculation')->getRateRequest();
        $_request->setProductClassId($product->getTaxClassId());
        $currentTax = Mage::getSingleton('tax/calculation')->getRate($_request);

        $_regularPrice = $product->getPrice();
        $_finalPrice = $product->getFinalPrice();
        if ($product->getTypeId() == Mage_Catalog_Model_Product_Type::TYPE_BUNDLE) {
            $_priceInclTax = Mage::helper('tax')->getPrice($product, $_finalPrice, true,
                null, null, null, null, null, false);
            $_priceExclTax = Mage::helper('tax')->getPrice($product, $_finalPrice, false,
                null, null, null, null, null, false);
        } else {
            $_priceInclTax = Mage::helper('tax')->getPrice($product, $_finalPrice, true);
            $_priceExclTax = Mage::helper('tax')->getPrice($product, $_finalPrice);
        }
        $_tierPrices = array();
        $_tierPricesInclTax = array();
        foreach ($product->getTierPrice() as $tierPrice) {
            $_tierPrices[] = Mage::helper('core')->currency(
                Mage::helper('tax')->getPrice($product, (float)$tierPrice['website_price'], false) - $_priceExclTax
                    , false, false);
            $_tierPricesInclTax[] = Mage::helper('core')->currency(
                Mage::helper('tax')->getPrice($product, (float)$tierPrice['website_price'], true) - $_priceInclTax
                    , false, false);
        }
        $config = array(
            'productId'           => $product->getId(),
            'priceFormat'         => Mage::app()->getLocale()->getJsPriceFormat(),
            'includeTax'          => Mage::helper('tax')->priceIncludesTax() ? 'true' : 'false',
            'showIncludeTax'      => Mage::helper('tax')->displayPriceIncludingTax(),
            'showBothPrices'      => Mage::helper('tax')->displayBothPrices(),
            'productPrice'        => Mage::helper('core')->currency($_finalPrice, false, false),
            'productOldPrice'     => Mage::helper('core')->currency($_regularPrice, false, false),
            'priceInclTax'        => Mage::helper('core')->currency($_priceInclTax, false, false),
            'priceExclTax'        => Mage::helper('core')->currency($_priceExclTax, false, false),
            /**
             * @var skipCalculate
             * @deprecated after 1.5.1.0
             */
            'skipCalculate'       => ($_priceExclTax != $_priceInclTax ? 0 : 1),
            'defaultTax'          => $defaultTax,
            'currentTax'          => $currentTax,
            'idSuffix'            => '_clone',
            'oldPlusDisposition'  => 0,
            'plusDisposition'     => 0,
            'plusDispositionTax'  => 0,
            'oldMinusDisposition' => 0,
            'minusDisposition'    => 0,
            'tierPrices'          => $_tierPrices,
            'tierPricesInclTax'   => $_tierPricesInclTax,
        );

        $responseObject = new Varien_Object();
        Mage::dispatchEvent('catalog_product_view_config', array('response_object' => $responseObject));
        if (is_array($responseObject->getAdditionalOptions())) {
            foreach ($responseObject->getAdditionalOptions() as $option => $value) {
                $config[$option] = $value;
            }
        }

        return Mage::helper('core')->jsonEncode($config);
    }

通过 Mage::helper(‘core’)->jsonEncode($config) 可获的 生成数据

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/4/28 0028
 * 无特价
 */
var optionsPrice = new Product.OptionsPrice({
    "productId": "270",
    "priceFormat": {
        "pattern": "\uffe5\u00a0%s",
        "precision": 2,
        "requiredPrecision": 2,
        "decimalSymbol": ".",
        "groupSymbol": ",",
        "groupLength": 3,
        "integerRequired": 1
    },
    "includeTax": "false",
    "showIncludeTax": false,
    "showBothPrices": false,
    "productPrice": 18.19,
    "productOldPrice": 18.19,
    "priceInclTax": 18.19,
    "priceExclTax": 18.19,
    "skipCalculate": 1,
    "defaultTax": 0,
    "currentTax": 0,
    "idSuffix": "_clone",
    "oldPlusDisposition": 0,
    "plusDisposition": 0,
    "plusDispositionTax": 0,
    "oldMinusDisposition": 0,
    "minusDisposition": 0,
    "tierPrices": [],
    "tierPricesInclTax": []
});

//有特价
var optionsPrice = new Product.OptionsPrice({
    "productId": "2009",
    "priceFormat": {
        "pattern": "\uffe5\u00a0%s",
        "precision": 2,
        "requiredPrecision": 2,
        "decimalSymbol": ".",
        "groupSymbol": ",",
        "groupLength": 3,
        "integerRequired": 1
    },
    "includeTax": "false",
    "showIncludeTax": false,
    "showBothPrices": false,
    "productPrice": 24.7,
    "productOldPrice": 29.7,
    "priceInclTax": 24.7,
    "priceExclTax": 24.7,
    "skipCalculate": 1,
    "defaultTax": 0,
    "currentTax": 0,
    "idSuffix": "_clone",
    "oldPlusDisposition": 0,
    "plusDisposition": 0,
    "plusDispositionTax": 0,
    "oldMinusDisposition": 0,
    "minusDisposition": 0,
    "tierPrices": [],
    "tierPricesInclTax": []
});

通过 Product.OptionsPrice 可追踪到 调用 JS 函数的PHTM

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/4/28 0028
 * app\design\frontend\base\default\template\catalog\product\view\options.phtml
 */

         if(element.type == 'select-multiple' && ('options' in element)) {
                        $A(element.options).each(function(selectOption) {
                            if (('selected' in selectOption) && typeof(configOptions[selectOption.value]) != 'undefined') {
                                if (selectOption.selected) {
                                    curConfig = configOptions[selectOption.value];
                                } else {
                                    curConfig = {price: 0};
                                }
                                optionsPrice.addCustomPrices(optionId + '-' + selectOption.value, curConfig);
                                optionsPrice.reload();
                            }
                        });
                    } else {
                        optionsPrice.addCustomPrices(element.id || optionId, curConfig);
                        optionsPrice.reload();
                    }

通过 搜索 addCustomPrices optionsPrice.reload() 追踪到调用的JS 库

//有特价 自动修改 js 代码
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/4/28 0028
 * js\varien\product.js
 */ 
         if (price > 0 || this.displayZeroPrice) {
                    formattedPrice = this.formatPrice(price);
                } else {
                    formattedPrice = '';
                }

                if ($(pair.value).select('.price')[0]) {
                    $(pair.value).select('.price')[0].innerHTML = formattedPrice;
                    if ($(pair.value+this.duplicateIdSuffix) && $(pair.value+this.duplicateIdSuffix).select('.price')[0]) {
                        $(pair.value+this.duplicateIdSuffix).select('.price')[0].innerHTML = formattedPrice;
                    }
                } else {
                    $(pair.value).innerHTML = formattedPrice+"W";//测试 单位万 后缀
                    if ($(pair.value+this.duplicateIdSuffix)) {
                        $(pair.value+this.duplicateIdSuffix).innerHTML = formattedPrice+"W"; //测试 单位万 后缀
                    }
                }

转载请注明:(●--●) Hello.My Weicot » Magento 1x 全局价格规则控制 以及快速定位 文件及JS

蜀ICP备15020253号-1