天天看点

Magento设置部分产品的Group Price的值

<?php

define('MAGENTO', realpath(dirname(__FILE__)));
require_once MAGENTO . '/app/Mage.php';
umask(0);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);


/*
需求:设置部分产品的Group Price的值
分析:需要查询该产品的Group Price值是否存在,不存在才设置
Group Price的值 等于  (Price的值 / 1.2); 
*/
$handle = Mage::getSingleton('core/resource')->getConnection('core_write');


//1.遍历所有产品
/*
 * catalog_product_entity_decimal表里 attribute_id字段为75的 value值就是 Price的值
 * */
$sql = "SELECT entity_id,`value` from catalog_product_entity_decimal  WHERE attribute_id=75";
$query = $handle->query($sql);

while ($row = $query->fetch()) {
    $row = new Varien_Object($row);
    $entity_id =  $row->getentity_id();
    $value = $row->getValue();

    //2.判断该产品是否已经有了Group Price(根据entity_id去catalog_product_entity_group_price里查询)
    $handle2 = Mage::getSingleton('core/resource')->getConnection('core_write');
    $sql2 = "SELECT * FROM catalog_product_entity_group_price WHERE entity_id=".$entity_id;
    $query2 = $handle2->query($sql2);
    if(!$query2->fetch()){ //该产品不存在Group Price
        echo $entity_id.'<br>';
        $value = $value / 1.2;
        $sql = "INSERT INTO catalog_product_entity_group_price (`entity_id`,`all_groups`,`customer_group_id`,`VALUE`) VALUES (".$entity_id.",0,1,".$value.")";
        $handle->query($sql);     
    }  
}