Magento 批量价格变化

发布于 2024-10-19 12:02:08 字数 1769 浏览 4 评论 0原文

我想做的是,对某个类别的产品进行批量价格更改。我们从供应商处收到建议零售价,但有时这些价格对我们不起作用。因此,我们需要计算产品的成本价,例如,在产品中添加 20%,这样……很简单,成本 = 成本 + 0.2*成本。现在我需要对所选类别中的所有产品执行此操作,所以这是迄今为止我所拥有的...

$category = Mage::getModel('catalog/category')->load(189); 

// load products from category id '189'
$products   = Mage::getModel('catalog/product')
                ->getCollection()  
                ->addCategoryFilter($category)  
                ->addAttributeToSelect('id'); 

foreach($products as $product) { 
    // get the current cost of the product
    $cost = $db->fetchRow("SELECT value FROM `m_catalog_product_entity_decimal` WHERE entity_id='" . $product->getId() . "' AND attribute_id='68'");
    $cost = $cost['value']; 
    $newCost = $cost + $cost*$percentage; 

    // update the product with a new cost
    $db->query("UPDATE `m_catalog_product_entity_decimal` SET value='$newCost' WHERE entity_id='" . $product->getId() . "' AND attribute_id='64'"); 
}

现在,我需要使用原始 SQL,因为我的 php 服务器无法处理所有 magento 产品加载和保存(Magento 1.4的产品模型存在内存泄漏)。这就是为什么我只是从产品中选择“id”,以获取最少的数据。我还知道执行所有这些 SQL 查询是浪费资源,这就是我来这里的原因。如果我的每个类别只有 10 个产品,我会使用产品模型来更新成本并保存产品,但有时每个类别一次有多达 500 个或更多产品。

我希望将其压缩为一个 SQL 查询,并摆脱 foreach 循环和产品集合。成本属性 ID 是 68,价格属性 ID 是 64。对此的任何帮助将不胜感激。

编辑
Magento 在其数据库中使用 EAV 模型。因此,对于我需要访问的属性,即“成本”和“价格”,它们都位于 m_catalog_product_entity_decimal

因此,产品价格属性在表中看起来像这样

value_id    entity_type_id  attribute_id    store_id    entity_id   value
6401             4              64             0            2184      399.9500

value_id 只是行唯一值,entity_type_id 4 表示这是产品属性。 attribute_id 与实际属性相关联。在本例中,64 是“价格”的 attribute_id。 store_id 无关紧要。 Entity_id是实际的商品id,value是商品的实际价格。

What I'm trying to do is, a bulk price change on products in a certain category. We receive suggested retail prices from our vendor, but sometimes these don't work for us. So we need to take the cost price of the product, and for example, add 20% to the product so.. easy enough cost = cost + 0.2*cost. Now I need to do this on all the products in the selected category, so here is what I have thus far...

$category = Mage::getModel('catalog/category')->load(189); 

// load products from category id '189'
$products   = Mage::getModel('catalog/product')
                ->getCollection()  
                ->addCategoryFilter($category)  
                ->addAttributeToSelect('id'); 

foreach($products as $product) { 
    // get the current cost of the product
    $cost = $db->fetchRow("SELECT value FROM `m_catalog_product_entity_decimal` WHERE entity_id='" . $product->getId() . "' AND attribute_id='68'");
    $cost = $cost['value']; 
    $newCost = $cost + $cost*$percentage; 

    // update the product with a new cost
    $db->query("UPDATE `m_catalog_product_entity_decimal` SET value='$newCost' WHERE entity_id='" . $product->getId() . "' AND attribute_id='64'"); 
}

Now, I need to use raw SQL because my php server can't handle all of the magento product loading and saving (Magento 1.4 has a memory leak in the product model). This is why I'm simply selecting the "id" from the product, to get the very least amount of data. I also understand that doing all of these SQL queries is a waste of resources, and thats why I'm here. If each of my categories only had say, 10 products, I would use the product model to update the cost and save the products, but I have sometimes up to 500 or more products in a each category as one time.

I'm hoping to condense this to one SQL query, and get rid of the foreach loop and the product collection. The cost attribute id is 68, and the price attribute id is 64. Any help on this would be much appreciated.

EDIT
Magento uses an EAV model for their database. So for the attributes I need to access, which are "cost" and "price", they are both located in m_catalog_product_entity_decimal

So a products price attribute would look like this in the table

value_id    entity_type_id  attribute_id    store_id    entity_id   value
6401             4              64             0            2184      399.9500

The value_id is just the rows unique value, entity_type_id 4 means this is a product attribute. The attribute_id is associated with the actual attribute. In this case, 64 is the attribute_id for "price". Store_id is irrelevant. Entity_id is the actual product id, and the value is the actual price of the item.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

青春有你 2024-10-26 12:02:08

老实说,B00MER 的想法是正确的,但我以前也曾在紧要关头做过类似的事情。另外,我想写一点 SQL。如果您只想将某个类别中所有产品的成本乘以 1.2,您可以执行以下操作:

update catalog_product_entity_decimal
  set value = value*1.2
  where attribute_id = 75 and
        entity_id IN (select product_id from catalog_category_product
                    where category_id = X);

毫无疑问,您需要重新索引所有内容并检查结果以确保确定。显然,将 X 替换为您的目标类别,并且您似乎使用了表前缀 m_,因此也将其附加(我将其保留给其他来到这里的搜索者)。

希望有帮助!

谢谢,

Honestly, B00MER has the right idea, but I've also had to do similar things in a pinch before. Plus, I felt like writing a little SQL. If you just want to multiply costs by 1.2 for all products in a category, you can do something like this:

update catalog_product_entity_decimal
  set value = value*1.2
  where attribute_id = 75 and
        entity_id IN (select product_id from catalog_category_product
                    where category_id = X);

You'll doubtless need to reindex everything and check your results to make sure. Obviously, replace X with your target category, and it appears that you use a table prefix of m_, so append that too (I'm leaving it off for other searchers who come here).

Hope that helps!

Thanks,
Joe

千柳 2024-10-26 12:02:08

放弃 Magento 的 ORM(尽管它非常消耗资源)并不是一个好主意。 这是 1.4 CE 内存泄漏的补丁,我会在“Magento”中进行修复方式并避免所需数据丢失或部分的麻烦。您也可以考虑 Unigry 的 Data RapidFlow,虽然价格昂贵,但如果您喜欢的话,每一分钱都值得。必须经常管理大量产品/类别数据。

Stepping away from Magento's ORM (as resource hungry as it can be) isn't really a good idea. Here's a patch for 1.4 CE memory leak and I would do it the "Magento" way and save the headaches of missing or partial data needed. You may also consider Unigry's Data RapidFlow as well, hefty price tag but well worth every penny if your having to manage a lot of product/category data often.

萌酱 2024-10-26 12:02:08

考虑一下 magmi ,它可以做这样的事情以及更多的事情,并且使用直接 SQL。
它也适用于多商店设置。

Consider magmi , it can do such things and many more and uses direct SQL.
it also works for multi store setups.

吻泪 2024-10-26 12:02:08

但是您可以使用 magento 数据对象来更新
例子:

$product = Mage::getModel('catalog/product')->load($id);
$product->setData('price',$value);
$product->save();

however you can use magento data object to update
example:

$product = Mage::getModel('catalog/product')->load($id);
$product->setData('price',$value);
$product->save();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文