Web services 优惠券MagentoAPI肥皂

Web services 优惠券MagentoAPI肥皂,web-services,magento,soap,Web Services,Magento,Soap,我在制作以下内容时遇到优惠券API问题: $couponCode = "test"; $resultCartCoupon = $proxy->call($sessionId, "cart_coupon.add", array($shoppingCartId, $couponCode)); 我总是得到:Uncaught SoapFault exception:[1083]优惠券无效如果我在前端尝试优惠券代码,就没有问题。有没有人成功地使用过这个API部件 谢谢。此错误来自Mage\u C

我在制作以下内容时遇到优惠券API问题:

$couponCode = "test";

$resultCartCoupon = $proxy->call($sessionId, "cart_coupon.add", array($shoppingCartId, $couponCode));
我总是得到:Uncaught SoapFault exception:[1083]优惠券无效如果我在前端尝试优惠券代码,就没有问题。有没有人成功地使用过这个API部件


谢谢。

此错误来自
Mage\u Checkout\u Model\u Cart\u优惠券\u Api::\u applyCoupon()

这看起来可能是一个bug,如果($couponCode!=$quote->getCouponCode()){,则应该是
,但我不确定


可能是您尝试应用优惠券的购物车(报价)无效,即没有接收优惠券所需的符合条件的商品。您确定
$shoppingCartId
与Magento的
sales\u flat\u quote
表中的预期报价正确匹配吗?

我注意到此摘录中存在错误:

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

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

在API上调试2-3小时后,我已在末尾解决此错误。检查优惠券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
?>

上面的代码给出了“未捕获的SoapFault异常:[1083]优惠券无效”的错误信息。当我调试核心代码时,我知道magento cart.create API在sales_flat_quote表中插入了错误的店铺id。我手动更改了sales_flat_quote表中的店铺id值,然后再次运行优惠券API,之后效果很好。下面是我的解决方案。创建购物车id时,只需运行下面的更新查询即可进行更改商店id。

您不需要编写直接SQL来解决此问题。只需在API调用中指定store ID参数即可。下面的示例是使用Magento SOAP API 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();
}
在以上所有操作后,应用折扣代码

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();
}
试试看
{
$result=$client->shoppingcartcuponadd($session,$quoteId,'test123',$storeId);
回显“
应用折扣代码:”; var_dump($结果); } 捕获(例外$ex) { echo“
折扣代码失败:”..ex->getMessage(); }
我注意到错误在这段摘录中:
尝试{$quote->getShippingAddress()->setCollectShippingRates(true);$quote->setCouponCode(strlen($couponCode)?$couponCode:“”)->collectTotals()->save();}catch(异常$e){$this->u错误(“无法应用优惠券代码,$e->getMessage());}

特别是nesta linha:
->collectTotals()
Ao retirar este trecho,não da erro,mas não aplicado cupom。
$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();
}