【问题标题】:Coupon Magento API Soap优惠券 Magento API 肥皂
【发布时间】:2015-01-29 22:02:09
【问题描述】:

我在制作时遇到了 Coupon API 的问题:

$couponCode = "test";

$resultCartCoupon = $proxy->call($sessionId, "cart_coupon.add", array($shoppingCartId, $couponCode));

我总是得到:未捕获的 SoapFault 异常:[1083] 如果我在前端尝试优惠券代码,优惠券无效,没有问题。有没有人成功使用过这个 API 部分?

谢谢。

【问题讨论】:

    标签: web-services magento soap


    【解决方案1】:

    这个错误来自Mage_Checkout_Model_Cart_Coupon_Api::_applyCoupon()

    if ($couponCode) {
        if (!$couponCode == $quote->getCouponCode()) {
            $this->_fault('coupon_code_is_not_valid');
        }
    }
    

    这看起来可能是一个错误,应该是if ($couponCode != $quote->getCouponCode()) {,但我不确定。

    可能是您尝试应用优惠券的购物车(报价单)无效,即没有接收优惠券所需的合格商品。您确定 $shoppingCartId 正确匹配 Magento 的 sales_flat_quote 表中的预期报价吗?

    【讨论】:

    • 我注意到这个摘录中有错误:
      try { $quote-&gt;getShippingAddress()-&gt;setCollectShippingRates(true); $quote-&gt;setCouponCode(strlen($couponCode) ? $couponCode : '') -&gt;collectTotals() -&gt;save(); } catch (Exception $e) { $this-&gt;_fault("cannot_apply_coupon_code", $e-&gt;getMessage()); } <br> Em especifico nesta linha:-&gt;collectTotals() Ao retirar este trecho, não da erro, mas não aplicado o cupom.
    【解决方案2】:

    我注意到这个摘录中有错误:

    try { 
        $quote->getShippingAddress()->setCollectShippingRates(true); 
        $quote->setCouponCode(strlen($couponCode) ? $couponCode : '') 
        ->collectTotals() 
        ->save(); 
    } catch (Exception $e) { 
        $this->_fault("cannot_apply_coupon_code", $e->getMessage()); 
    } 
    

    在此特定行中:-&gt;collectTotals() 通过删除此拉伸,不是错误,但未应用优惠券。

    【讨论】:

      【解决方案3】:

      在 API 上调试 2-3 小时后,我已经解决了这个错误。检查下面我在 Coupon API 中使用的代码。

      <?php
       $mage_url = 'http://yoursiteurl/api/soap?wsdl';
       $mage_user= "API_User"; //webservice user login
       $mage_api_key = "API_PASSWORD"; //webservice user pass
       $client = new SoapClient($mage_url);
      
       $couponCode = 'couponCode'; // a coupon to be apply
       $shoppingCartId = '35'; // a cart Id which i have put test id
       $sessionId = $client->login($mage_user, $mage_api_key);
       $result = $client->call($sessionId,'cart_coupon.add',array($shoppingCartId,$couponCode));
      
       print_r($result);
      ?>
      
      The above code gives error that "Uncaught SoapFault exception: [1083] Coupon is not valid". When i debugg the core code i came to know that magento cart.create API insert wrong store id in sales_flat_quote table. I have changed the store id value in sales_flat_quote table manually and again run the Coupon API and after that it works perfectly. So here is the my solution. When you create cart id just run the below update query to change the store id.
      
      <?php
       $shoppingCartId = $soap->call( $sessionId, 'cart.create');
      
       $mageFilename = '../app/Mage.php';
       require_once $mageFilename;
       umask(0);
       Mage::app();
      
       $db_write1 = Mage::getSingleton('core/resource')->getConnection('core_write');
       $updateQue  = "update sales_flat_quote set store_id='1' where entity_id ='".$shoppingCartId."'";
       $db_write1->query($updateQue);
      
       // Now run the Coupon API here
      ?>
      

      代码取自这里:http://chandreshrana.blogspot.in/2015/11/uncaught-soapfault-exception-1083.html

      【讨论】:

        【解决方案4】:

        您无需编写直接 SQL 即可解决此问题。 只需在 API 调用中指定商店 ID 参数。下面的示例是使用 Magento SOAP APIs V2 应用折扣代码的演示脚本:

        /* Set Discount Code */
        
        try
        {
            $result = $client->shoppingCartCouponAdd($session, $quoteId, 'test123',$storeId);
            echo "<br>Apply discount code: ";
            var_dump($result);
        }
        catch(Exception $ex)
        {
            echo "<br>Discount code Failed: " . $ex->getMessage();
        }
        

        要应用折扣代码,请执行以下步骤:

        $quoteId = $client->shoppingCartCreate($session,$storeId);
        
        /* Set cart customer */
        $guest = true;
        
        if ($guest) 
        {
            $customerData = array(
                "firstname" => "testFirstname",
                "lastname" => "testLastName",
                "email" => "testEmail@mail.com",
                "mode" => "guest",
                "website_id" => "1"
            );
        } 
        else 
        {
            $customer  = array(
                "customer_id" => '69301',
                "website_id" => "1",
                "group_id" => "1",
                "store_id" => "1",
                "mode" => "customer",
            );
        }
        
        //Set cart customer (assign customer to quote)
        $resultCustomerSet = $client->shoppingCartCustomerSet($session, $quoteId, $customerData,$storeId);
        
        /* Set customer addresses Shipping and Billing */
        
        $addresses = array(
            array(
                "mode" => "shipping",
                "firstname" => "Ahsan",
                "lastname" => "testLastname",
                "company" => "testCompany",
                "street" => "testStreet",
                "city" => "Karachi",
                "region" => "Sindh",
                "postcode" => "7502",
                "country_id" => "PK",
                "telephone" => "0123456789",
                "fax" => "0123456789",
                "is_default_shipping" => 0,
                "is_default_billing" => 0
            ),
            array(
                "mode" => "billing",
                "firstname" => "Ahsan",
                "lastname" => "testLastname",
                "company" => "testCompany",
                "street" => "testStreet",
                "city" => "Karachi",
                "region" => "Sindh",
                "postcode" => "7502",
                "country_id" => "PK",
                "telephone" => "0123456789",
                "fax" => "0123456789",
                "is_default_shipping" => 0,
                "is_default_billing" => 0
            )
        );
        
        //Set cart customer address
        $resultCustomerAddress = $client->shoppingCartCustomerAddresses($session, $quoteId, $addresses,$storeId);   
        
        /* Set payment method */
        $responsePayment = $client->shoppingCartPaymentMethod($session, $quoteId,       array(
            'method' => 'cashondelivery',
        ),$storeId);  
        
        /* Set shipping method */
        $setShipping = $client->shoppingCartShippingMethod($session, $quoteId, 'flatrate_flatrate',$storeId);
        

        以上都应用折扣码,

        try
        {
            $result = $client->shoppingCartCouponAdd($session, $quoteId, 'test123',$storeId);
            echo "<br>Apply discount code: ";
            var_dump($result);
        }
        catch(Exception $ex)
        {
            echo "<br>Discount code Failed: " . $ex->getMessage();
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-12-27
          • 1970-01-01
          • 2013-06-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多