以编程方式将产品添加到magento购物车中,仅在第一次不起作用

以编程方式将产品添加到magento购物车中,仅在第一次不起作用,magento,Magento,我正在为注册创建自定义功能,并将产品添加到客户购物车中 如果用户使用“我的功能”注册,他/她添加的第一个产品将不会添加到购物车,除非他在之后添加了另一个产品,一切正常,并且第一个产品也出现在购物车中 如果用户使用magento注册表单注册,则使用“我的”功能将产品添加到购物车中 注册代码 public function signupAction() { $email = $this->getRequest()->getPost('email'); $password

我正在为注册创建自定义功能,并将产品添加到客户购物车中

如果用户使用“我的功能”注册,他/她添加的第一个产品将不会添加到购物车,除非他在之后添加了另一个产品,一切正常,并且第一个产品也出现在购物车中

如果用户使用magento注册表单注册,则使用“我的”功能将产品添加到购物车中

注册代码

public function signupAction() {
    $email = $this->getRequest()->getPost('email');
    $password = $this->getRequest()->getPost('password');
    $firstName = $this->getRequest()->getPost('firstName');
    $LastName = $this->getRequest()->getPost('LastName');

    $session = Mage::getSingleton('customer/session');
    $session->setEscapeMessages(true);
    $websiteId = Mage::app()->getWebsite()->getId();
    $store = Mage::app()->getStore();
    $customer = Mage::getModel("customer/customer");
    $customer->setWebsiteId($websiteId)
            ->setStore($store)
            ->setFirstname($firstName)
            ->setLastname($LastName)
            ->setEmail($email)
            ->setPassword($password);
    try {
        $customer->cleanPasswordsValidationData();
        $customer->save();
        $this->_dispatchRegisterSuccess($customer);
        $this->_successProcessRegistration($customer);
    } catch (Mage_Core_Exception $e) {

    } catch (Exception $e) {

    }
}
public function addAction() {
    $form_key = Mage::getSingleton('core/session')->getFormKey();
    $json = $this->getRequest()->getPost('json');
    $jsonObj = json_decode($json);
    $cart = $this->_getCart();
    $cart->init();
    $response = array();
    try {
        foreach ($jsonObj as $data) {
            $param = ['form_key' => $form_key,
 'qty' => $data->qty, 'product' => $data->productId];
            $product = $this->_initProduct($param['product']);
            if ($data->type == 'simple') {
                $cart->addProduct($product, $param);
            }
        }
        $cart->save();
        $this->_getSession()->setCartWasUpdated(true)

        /**
         * @todo remove wishlist observer processAddToCart
         */
        Mage::dispatchEvent('checkout_cart_add_product_complete', 
array('product' => $product,
 'request' => $this->getRequest(), 
'response' => $this->getResponse()));

        if (!$cart->getQuote()->getHasError()) {
            $response['status'] = 'SUCCESS';
        } else {
            $response['status'] = 'Error';
        }
    } catch (Mage_Core_Exception $e) {
        $msg = "";
        if ($this->_getSession()->getUseNotice(true)) {
            $msg = $e->getMessage();
        } else {
            $messages = array_unique(explode("\n", $e->getMessage()));
            foreach ($messages as $message) {
                $msg .= $message . '<br/>';
            }
        }

        $response['status'] = 'ERROR';
        $response['message'] = $msg;
    } catch (Exception $e) {
        $response['status'] = 'ERROR';
        $response['message'] = $this->__('Cannot add items.');
        Mage::logException($e);
    }
    $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($response));
    return;
}
添加到购物车代码

public function signupAction() {
    $email = $this->getRequest()->getPost('email');
    $password = $this->getRequest()->getPost('password');
    $firstName = $this->getRequest()->getPost('firstName');
    $LastName = $this->getRequest()->getPost('LastName');

    $session = Mage::getSingleton('customer/session');
    $session->setEscapeMessages(true);
    $websiteId = Mage::app()->getWebsite()->getId();
    $store = Mage::app()->getStore();
    $customer = Mage::getModel("customer/customer");
    $customer->setWebsiteId($websiteId)
            ->setStore($store)
            ->setFirstname($firstName)
            ->setLastname($LastName)
            ->setEmail($email)
            ->setPassword($password);
    try {
        $customer->cleanPasswordsValidationData();
        $customer->save();
        $this->_dispatchRegisterSuccess($customer);
        $this->_successProcessRegistration($customer);
    } catch (Mage_Core_Exception $e) {

    } catch (Exception $e) {

    }
}
public function addAction() {
    $form_key = Mage::getSingleton('core/session')->getFormKey();
    $json = $this->getRequest()->getPost('json');
    $jsonObj = json_decode($json);
    $cart = $this->_getCart();
    $cart->init();
    $response = array();
    try {
        foreach ($jsonObj as $data) {
            $param = ['form_key' => $form_key,
 'qty' => $data->qty, 'product' => $data->productId];
            $product = $this->_initProduct($param['product']);
            if ($data->type == 'simple') {
                $cart->addProduct($product, $param);
            }
        }
        $cart->save();
        $this->_getSession()->setCartWasUpdated(true)

        /**
         * @todo remove wishlist observer processAddToCart
         */
        Mage::dispatchEvent('checkout_cart_add_product_complete', 
array('product' => $product,
 'request' => $this->getRequest(), 
'response' => $this->getResponse()));

        if (!$cart->getQuote()->getHasError()) {
            $response['status'] = 'SUCCESS';
        } else {
            $response['status'] = 'Error';
        }
    } catch (Mage_Core_Exception $e) {
        $msg = "";
        if ($this->_getSession()->getUseNotice(true)) {
            $msg = $e->getMessage();
        } else {
            $messages = array_unique(explode("\n", $e->getMessage()));
            foreach ($messages as $message) {
                $msg .= $message . '<br/>';
            }
        }

        $response['status'] = 'ERROR';
        $response['message'] = $msg;
    } catch (Exception $e) {
        $response['status'] = 'ERROR';
        $response['message'] = $this->__('Cannot add items.');
        Mage::logException($e);
    }
    $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($response));
    return;
}
公共函数addAction(){
$form_key=Mage::getSingleton('core/session')->getFormKey();
$json=$this->getRequest()->getPost('json');
$jsonObj=json_decode($json);
$cart=$this->_getCart();
$cart->init();
$response=array();
试一试{
foreach($jsonObj作为$data){
$param=['form\u key'=>$form\u key,
“数量”=>$data->数量,“产品”=>$data->productId];
$product=$this->_initProduct($param['product']);
如果($data->type=='simple'){
$cart->addProduct($product,$param);
}
}
$cart->save();
$this->_getSession()->setCartWasUpdated(true)
/**
*@todo删除愿望列表观察者进程AddToCart
*/
Mage::dispatchEvent('checkout\u cart\u add\u product\u complete',
数组('product'=>$product,
'request'=>this->getRequest(),
'response'=>this->getResponse());
如果(!$cart->getQuote()->getHasError()){
$response['status']='SUCCESS';
}否则{
$response['status']='Error';
}
}捕获(法师核心例外$e){
$msg=”“;
如果($this->_getSession()->getUseNotice(true)){
$msg=$e->getMessage();
}否则{
$messages=array_unique(分解(“\n”,$e->getMessage());
foreach($messages作为$message){
$msg.=$message.“
”; } } $response['status']='ERROR'; $response['message']=$msg; }捕获(例外$e){ $response['status']='ERROR'; $response['message']=$this->_('无法添加项目'); Mage::logException($e); } $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($response)); 返回; }
刷新页面是解决方案,因为magento使用表单键验证数据

所以,当登录一个客户时,会话将根据此进行更改,并且它将正常工作


如果您还有更多问题,请告诉我。

注册客户后刷新页面?@JayramPrajapati不,不是@JayramPrajapati,刷新页面是解决此问题的方法谢谢!!但是你能告诉我为什么创建新客户后需要刷新页面吗!请写下来作为答案。