在商店级别创建结帐会话

发布于 2024-12-17 14:17:21 字数 835 浏览 1 评论 0原文

我正在尝试在商店级别(而不是站点级别)为登录客户创建不同的结账会话。

我们有一个拥有 2 家商店的网站。当用户将产品添加到购物车时,我们希望仅在该商店的购物车中显示该产品。但目前在商店之间切换会显示之前添加到购物车的其他商店的产品。

经过一些研究,我发现每当用户登录商店时,都会在 sales_flat_quote 表中为登录的客户创建一条记录,这对于该站点中的所有商店来说都是常见的。因此,当用户切换商店时,我需要在 sales_flat_quote 表中创建一条新记录。

我发现 Mage_Sales_Model_Mysql4_Quote 类中的 loadByCustomerId() 函数是在 sales_flat_quote 表 中创建新记录的函数。但我看到的只是一个选择查询:

$read = $this->_getReadAdapter();
$select = $this->_getLoadSelect('customer_id', $customerId, $quote)
                ->where('is_active=1')
                ->order('updated_at desc')
                ->limit(1);
$data = $read->fetchRow($select);

我不明白选择查询如何在 sales_flat_quote 表中创建记录。

有人可以指导当登录的客户切换到同一站点下的其他商店时如何在此表中创建新记录吗?

I'm trying to create different checkout sessions for the logged in customer at a store level, rather than at the site level.

We have a site with 2 stores. When a user adds a product to the cart, we want to show that product in the cart only for that store. But currently switching between stores will show up products of other stores previously added to the cart.

After some research I found that whenever a user logs in to a store, a record is created in the sales_flat_quote table for the logged in customer, which is common for all the stores in that site. So I need to create a new record in the sales_flat_quote table when the user switches stores.

I found that the loadByCustomerId() function in class Mage_Sales_Model_Mysql4_Quote is what creates the new record in the sales_flat_quote table. But all I see is a select query:

$read = $this->_getReadAdapter();
$select = $this->_getLoadSelect('customer_id', $customerId, $quote)
                ->where('is_active=1')
                ->order('updated_at desc')
                ->limit(1);
$data = $read->fetchRow($select);

I don't understand how the select query is creating the record in the sales_flat_quote table.

Could someone guide as to how to create a new record in this table for the logged in customer when he/she switches to an other store under the same site?

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

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

发布评论

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

评论(1

遗忘曾经 2024-12-24 14:17:21

我们的网站有 2 家商店。当用户将产品添加到购物车时,
我们只想在该商店的购物车中显示该产品。但
目前在商店之间切换会显示其他商店的产品
之前已添加到购物车的商店。

这是正确

看起来您想要将 store 定义的购物车分开,它们都连接到 Magento 中相同的 website 定义。

在 Magento 中,同一网站中定义的所有商店始终共享相同的购物车。

这是设计的。

要实现购物车分离,您需要将 Magento 设置更改为每个网站有两个 网站一个 商店 。否则,我猜您会遇到将两家商店分开的无尽问题(就像您现在遇到的问题)。

尽管如此,我还是会尝试回答你的具体问题:

我不明白选择查询如何在
sales_flat_quote 表。

SELECT 查询通常永远不会创建记录;它被认为是读取数据。

看一下 Mage_Checkout_Model_Session::loadCustomerQuote()

$customerQuote = Mage::getModel('sales/quote')
    ->setStoreId(Mage::app()->getStore()->getId())
    ->loadByCustomer(Mage::getSingleton('customer/session')->getCustomerId());

这就是导致调用您正在讨论的 Mage_Sales_Model_Mysql4_Quote::loadByCustomerId() 的原因。它只是加载客户报价(如果有),没有其他。

通过调用quotes save() 方法来创建/更新记录。

例如,Magento EE 1.11.0.0 是这样保存记录的:

if ($customerQuote->getId() && $this->getQuoteId() != $customerQuote->getId()) {
    if ($this->getQuoteId()) {
        $customerQuote->merge($this->getQuote())
            ->collectTotals()
            ->save();   // <- either this 
    }

    $this->setQuoteId($customerQuote->getId());

    if ($this->_quote) {
        $this->_quote->delete();
    }
    $this->_quote = $customerQuote;
} else {
    $this->getQuote()->getBillingAddress();
    $this->getQuote()->getShippingAddress();
    $this->getQuote()->setCustomer(Mage::getSingleton('customer/session')->getCustomer())
        ->setTotalsCollectedFlag(false)
        ->collectTotals()
        ->save();   // <- or that does the save
}

您没有提到您使用的 Magento 版本,因此您的里程可能会有所不同,但原理仍然保持不变。

We have a site with 2 stores. When a user adds a product to the cart,
we want to show that product in the cart only for that store. But
currently switching between stores will show up products of other
stores previously added to the cart.

Which is correct.

It looks like you want to separate carts for store definitions, which both are connected to the very same website definition in Magento.

In Magento, all stores defined within the same website do always share the very same cart.

That's by-design.

To achieve cart separation you would need to change your Magento setup to have two websites and one store per website. Otherwise you'll run into endless issues separating the two stores (like the issue you're having now), I guess.

That said, I'll nevertheless try to answer your concrete question:

I don't understand how the select query is creating the record in the
sales_flat_quote table.

A SELECT query usually never ever will create a record; it's thought to read data.

Have a look at Mage_Checkout_Model_Session::loadCustomerQuote():

$customerQuote = Mage::getModel('sales/quote')
    ->setStoreId(Mage::app()->getStore()->getId())
    ->loadByCustomer(Mage::getSingleton('customer/session')->getCustomerId());

This is what leads to the call of Mage_Sales_Model_Mysql4_Quote::loadByCustomerId() you're talking about. It just loads the customers quote (if any), nothing else.

Creation/update of the record happens by calling the quotes save() method.

Magento EE 1.11.0.0 for example saves the record this way:

if ($customerQuote->getId() && $this->getQuoteId() != $customerQuote->getId()) {
    if ($this->getQuoteId()) {
        $customerQuote->merge($this->getQuote())
            ->collectTotals()
            ->save();   // <- either this 
    }

    $this->setQuoteId($customerQuote->getId());

    if ($this->_quote) {
        $this->_quote->delete();
    }
    $this->_quote = $customerQuote;
} else {
    $this->getQuote()->getBillingAddress();
    $this->getQuote()->getShippingAddress();
    $this->getQuote()->setCustomer(Mage::getSingleton('customer/session')->getCustomer())
        ->setTotalsCollectedFlag(false)
        ->collectTotals()
        ->save();   // <- or that does the save
}

You didn't mention the Magento version you use, so your mileage may vary, but the principle will still remain the same.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文