javascript - 单击一个更新按钮保存所有文本字段

标签 javascript php database magento

我们有 magento 网站。我们为 vendor 提供了编辑多个产品的所有文本字段并通过单击一个“更新”按钮保存所有文本字段的选项。

我们使用以下代码来保存一个文本字段的价格。

价格

public function updateFieldPriceAction(){
    Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);      
    $id= $this->getRequest()->getParam('id');
    $customerid=Mage::getSingleton('customer/session')->getCustomerId();
    $collection_product = Mage::getModel('marketplace/product')->getCollection()->addFieldToFilter('mageproductid',array('eq'=>$id))->addFieldToFilter('userid',array('eq'=>$customerid));
    //Mage::getSingleton('core/session')->setEditProductId($id);

    try{
    $upd_price = $this->getRequest()->getParam('price');
    $product = Mage::getModel('catalog/product')->load($id);        
    //$product->setData('price', $upd_price);
    $product->setPrice($upd_price);

    //$stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($id);
    //$stockItem->setData('manage_stock', 1);
    //$stockItem->setData('qty', $this->getRequest()->getParam('qty'));
    $product->save();

    echo $price = $product->getPrice();
    echo $name = $product->getName();

    $response['message'] = 'Your Product Is Been Sucessfully Updated';
    $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($response)); 
    //Mage::getSingleton('core/session')->addSuccess(Mage::helper('marketplace')->__('Your Product Is Been Sucessfully Updated'));


    //endif;
    }catch(Exception $e){
    echo "Not Saving"; exit;    
    Mage::log($e->getMessage());
    }

  }

我们使用以下代码来保存一个文本字段的数量。

数量

public function updateFieldAction(){
        $id= $this->getRequest()->getParam('id');
        $customerid=Mage::getSingleton('customer/session')->getCustomerId();
        $collection_product = Mage::getModel('marketplace/product')->getCollection()->addFieldToFilter('mageproductid',array('eq'=>$id))->addFieldToFilter('userid',array('eq'=>$customerid));
        //Mage::getSingleton('core/session')->setEditProductId($id);
        $stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($id);
        $stockItem->setData('manage_stock', 1);
        $stockItem->setData('qty', $this->getRequest()->getParam('qty'));

        $stockItem->save();

        $response['message'] = 'Your Product Is Been Sucessfully Updated';
        $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($response)); 
        //Mage::getSingleton('core/session')->addSuccess(Mage::helper('marketplace')->__('Your Product Is Been Sucessfully Updated'));
      }

我们使用以下代码通过单个更新按钮 保存所有价格和数量文本字段。价格正在发挥作用。但数量不起作用。

controller.php

public function massupdatesellerproAction(){
    if($this->getRequest()->isPost()){
        if(!$this->_validateFormKey()){
             $this->_redirect('marketplace/marketplaceaccount/myproductslist/');
        }
        $ids= $this->getRequest()->getParam('product_mass_update');
        $price= $this->getRequest()->getParam('price');
        $qty = $this->getRequest()->getParam('qty');
        foreach ($ids as $key => $value) {
    $product = Mage::getModel('catalog/product')->load($value);
    $product->setPrice($price[$key]);
    $product->setQty($qty[$key]);
    $product->save();
        }
        Mage::getSingleton('core/session')->addSuccess( Mage::helper('marketplace')->__('Products has been sucessfully saved from your account'));
        $this->_redirect('marketplace/marketplaceaccount/myproductslist/');


    }}

enter image description here

最佳答案

setQty 在 magento 中不工作。数量通常节省库存而不是产品。删除

$product->setQty($qty[$key]);

从你的 Controller 。

这是一个部分答案,但希望它能让您朝着正确的方向前进。 您可以尝试通过以下代码在 controller.php 中节省数量

 $product->setStockData(
   array( 
          'is_in_stock' => 1, 
          'qty' => $qty[$key],
          'manage_stock' => 1,
          'use_config_notify_stock_qty' => 1
   )
 );

或者 你可以 试试这个controller.php:

$stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product);
$stockItem->setData('qty', $this->getRequest()->getParam('qty'));
$stockItem->save();
$product->save();

然后像以前一样设置并保存您的更改。 你可以查看this .

这是你的数量更新的全部功能

public function updateFieldAction(){
        $id= $this->getRequest()->getParam('id');
        $customerid=Mage::getSingleton('customer/session')->getCustomerId();
        $collection_product = Mage::getModel('marketplace/product')->getCollection()->addFieldToFilter('mageproductid',array('eq'=>$id))->addFieldToFilter('userid',array('eq'=>$customerid));
        //Mage::getSingleton('core/session')->setEditProductId($id);
        $stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($id);
        $stockItem->setData('manage_stock', 1);
        $stockItem->setData('qty', $this->getRequest()->getParam('qty'));

        $stockItem->save();
        $product = Mage::getModel('catalog/product')->load( 'id' );
        $product->save();

        $response['message'] = 'Your Product Is Been Sucessfully Updated';
        $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($response)); 
        //Mage::getSingleton('core/session')->addSuccess(Mage::helper('marketplace')->__('Your Product Is Been Sucessfully Updated'));
      }

Controller 代码

public function massupdatesellerproAction(){
if($this->getRequest()->isPost()){
    if(!$this->_validateFormKey()){
         $this->_redirect('marketplace/marketplaceaccount/myproductslist/');
    }
    $ids= $this->getRequest()->getParam('product_mass_update');
    $price= $this->getRequest()->getParam('price');
    $qty = $this->getRequest()->getParam('qty');
    foreach ($ids as $key => $value) {
     $product = Mage::getModel('catalog/product')->load($value);
     $product->setPrice($price[$key]);
     $product->setStockData(
       array( 
        'is_in_stock' => 1, 
        'qty' => $qty[$key],
        'manage_stock' => 1,
        'use_config_notify_stock_qty' => 1
       )
    );


     $product->save();
    }
    Mage::getSingleton('core/session')->addSuccess( Mage::helper('marketplace')->__('Products has been sucessfully saved from your account'));
    $this->_redirect('marketplace/marketplaceaccount/myproductslist/');


}}

关于javascript - 单击一个更新按钮保存所有文本字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34534988/

相关文章:

javascript - 如何使元素文本区域自动为子字符串着色?

javascript - jQuery 验证插件多个远程问题。是我还是bug?

php - 连接到 mysql 数据库时出错。

php - 将 PNG 图像保存到 mysql 数据库时出现问题

ios - 在 iOS 应用程序更新中删除核心数据模型

javascript - D3.JS : Multiple root points 中的可折叠图

javascript - css 和 js 无法在 Rails 生产模式下工作

mysql - 多对多关系默认为 "all"

javascript - 从字符串中删除以某个字符开头的单词

php - 在 foreach 中格式化 MySQL 日期