简体   繁体   English

Magento-在产品创建观察者之前获取商店ID

[英]Magento - Get store id before product create observer

I want to add a few websites to a product based on the current store id in the catalog_product_save_before observer. 我想基于catalog_product_save_before观察器中的当前商店ID向产品添加一些网站。

The observer is working fine, but I can not seem to get the current store id from which the product is created. 观察者工作正常,但我似乎无法获取用于创建产品的当前商店ID。 This is what I tried: 这是我尝试的:

Mage::app()->getStore()->getId();

which returns 0. 返回0。

Magento treats admin panel as a separate store with name admin and id 0. So, it returns the store id for admin, which is not what I want. Magento将管理面板视为名称为admin且ID为0的单独商店。因此,它返回admin的商店ID,这不是我想要的。

So, I tried a few things in here , and it still returns the id as 0. 因此,我在这里尝试了一些操作,它仍然返回id为0。

More Details: 更多细节:

I have logged in as a store admin to enterprise.lmm.dev(virtual host for enterprise website) and am creating a product. 我已以商店管理员身份登录到enterprise.lmm.dev(企业网站的虚拟主机),并正在创建产品。 Here, both the website name and store name is 'enterprise'. 在这里,网站名称和商店名称均为“企业”。

When creating a product, I choose 'enterprise' from the 'Choose Store View' drop-down and fills the product create form. 创建产品时,我从“选择商店视图”下拉列表中选择“企业”,并填写产品创建表单。

I have written an observer function 'before_product_save()', in which this is what I do: 我已经编写了一个观察者函数“ before_product_save()”,这就是我要做的:

echo Mage::app()->getStore()->getStoreId();

But, this is returning 0. 但是,这将返回0。

I want to know if this is the way to go and if I can access the store id for the store 'enterprise'. 我想知道这是否可行,是否可以访问商店“企业”的商店ID。

Thanks. 谢谢。

At this point store_id in the product object has been set to the ID of the store for which you save the current values. 此时,产品对象中的store_id已设置为您要为其存储当前值的商店的ID。

So assuming, $product is your product object, you can use this: 因此,假设$product是您的产品对象,则可以使用以下代码:

$product->getStoreId()

Or if you need the store model: 或者,如果您需要商店模型:

Mage::app()->getStore($product->getStoreId());

尝试:

Mage::app()->getStore()->getStoreId();

如果Mage::app()->getStore()->getStoreId()不起作用,请尝试$this->helper('core')->getStoreId()Mage::helper('core')->getStoreId()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM