Jump to content

Problem with Cart_rules when creating new order with Webservice


LaurentA83

Recommended Posts

Hello, 

I have an issue when creating a new order with Webservice API.

I have the following script : 

<html><head><title>WEB SERVICE - CREATE ORDER</title></head><body>
<?php

define('DEBUG', true);
define('PS_SHOP_PATH', 'my_domain');
define('PS_WS_AUTH_KEY', 'my_key');
require_once('PSWebServiceLibrary.php');
$webService = new PrestaShopWebservice(PS_SHOP_PATH, PS_WS_AUTH_KEY, DEBUG);
include_once("../../db_connect_dev.php");

// VARIABLES
$id_customer = $_POST['customer_id'];
$id_carrier = $_POST['carrier_id'];
$id_address = $_POST['address_id'];
$currency = $_POST['currency'];
$payment = $_POST['payment'];
$module = $_POST['module'];
$id_lang = $_POST['lan'];
$id_product = json_decode($_POST['product_id']);
$id_product_attribute = json_decode($_POST['product_attribute']);
$quantity = json_decode($_POST['quantity']);
$total_paid_real = $_POST['price'];
$total_paid = $total_paid_real;
$total_paid_tax_incl = $total_paid_real;
$total_paid_tax_excl = round($total_paid_real / 1.2, 2);
$total_products = $total_paid_tax_excl;
$total_products_wt = $total_paid_real;
$id_lang = 2;
$id_address_delivery = $id_address;
$id_address_invoice = $id_address;
$carrier_tax_rate = 20.000;
$nb_product = count($id_product);
$conversion_rate = 1.000000;
$id_shop = 1;
$id_shop_group = 1;
$id_currency = 1;
$current_state = $_POST['current_state'];
$reduction_value = $_POST['reduction_value'];


// CREATE CART RULE

// Vérifiez si $reduction_value est supérieur à 0 ou non vide
if (!empty($reduction_value) && $reduction_value > 0) {

// Création d'une nouvelle règle de panier
$cart_rule_xml = $webService->get(array('url' => PS_SHOP_PATH.'/api/cart_rules?schema=blank'));

// Configuration de la règle de panier
$cart_rule_xml->cart_rule->id_customer = $id_customer;
$cart_rule_xml->cart_rule->date_from = date('Y-m-d');
$cart_rule_xml->cart_rule->date_to = date('Y-m-d', strtotime('+1 day'));

// Configuration du nom de la règle pour plusieurs langues
$cart_rule_xml->cart_rule->name->language[0]->attributes()->id = 1; // ID de la langue 1
$cart_rule_xml->cart_rule->name->language[0] = "Remise app boutique test"; //. $reduction_value
$cart_rule_xml->cart_rule->name->language[1]->attributes()->id = 2; // ID de la langue 2
$cart_rule_xml->cart_rule->name->language[1] = "Discount app store test"; //. $reduction_value

$cart_rule_xml->cart_rule->active = 1;
$cart_rule_xml->cart_rule->quantity = 1;
$cart_rule_xml->cart_rule->quantity_per_user = 1;
$cart_rule_xml->cart_rule->priority = 1;
$cart_rule_xml->cart_rule->reduction_amount = $reduction_value;
$cart_rule_xml->cart_rule->reduction_tax = 1;

// Envoi l'objet de règle de panier pour création
$cart_rule_opt = array('resource' => 'cart_rules');
$cart_rule_opt['postXml'] = $cart_rule_xml->asXML();
$created_cart_rule = $webService->add($cart_rule_opt);

// Récupération de l'ID de la règle de panier créée
$id_cart_rule = $created_cart_rule->cart_rule->id;

} else {
    // Si la réduction n'est pas définie ou est égale à 0, définit l'ID de la règle de panier à null ou à une valeur par défaut
    $id_cart_rule = null; // Ou une valeur par défaut si nécessaire
}

// CREATE CART

$xml = $webService->get(array('url' => PS_SHOP_PATH.'/api/carts?schema=blank'));
$xml->cart->id_shop_group = $id_shop_group;
$xml->cart->id_shop = $id_shop;
$xml->cart->id_carrier = $id_carrier;
$xml->cart->id_lang = $id_lang;
$xml->cart->id_address_delivery = $id_address_delivery;
$xml->cart->id_address_invoice =  $id_address_invoice;



// SET DELIVERY OPTION
$xml->cart->delivery_option = json_encode([$id_address_delivery => "$id_carrier,"]);

$xml->cart->id_currency = $id_currency;
$xml->cart->id_customer = $id_customer;
$xml->cart->id_guest = 0;
$xml->cart->recyclable = 0;
$xml->cart->gift = 0;
$xml->cart->mobile_theme = 0;
$xml->cart->allow_separated_package = 0;
$xml->cart->id_cart_rule = $id_cart_rule;

for ($i = 0; $i < $nb_product; $i++) {
	$xml->cart->associations->cart_rows->cart_row[$i]->id_product = $id_product[$i];
	$xml->cart->associations->cart_rows->cart_row[$i]->id_address_delivery = $id_address_delivery;
	$xml->cart->associations->cart_rows->cart_row[$i]->id_product_attribute = $id_product_attribute[$i];
	$xml->cart->associations->cart_rows->cart_row[$i]->quantity = $quantity[$i];
}
$opt = array('resource' => 'carts');
$opt['postXml'] = $xml->asXML();
$xml = $webService->add($opt);
$id_cart = $xml->cart->id; // CART ID

// // CREATE CART RULE IF DISCOUNT
// if($cart_rule != 0) {
// 	$sql2 = mysqli_connect($host, $username, $password, $db_name) or die(mysqli_error($sql2));
//     $req2 = "INSERT INTO ps_cart_cart_rule (id_cart, id_cart_rule) VALUES ('$id_cart', '$cart_rule_id')";
// 	$res2 = mysqli_query($sql2, $req2) or die(mysqli_error($sql2));
// }

// CREATE ORDER
$psXML_order = <<<XML
<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
<order>
  <id_carrier>$id_carrier</id_carrier>
  <id_lang>$id_lang</id_lang>
  <id_customer>$id_customer</id_customer>
  <id_cart>$id_cart</id_cart>
  <id_currency>$id_currency</id_currency>
  <id_address_delivery>$id_address_delivery</id_address_delivery>
  <id_address_invoice>$id_address_invoice</id_address_invoice>
  <payment>$payment</payment>
  <module>$module</module>
  <current_state>$current_state</current_state>
  <conversion_rate>$conversion_rate</conversion_rate>
  <total_paid>$total_paid</total_paid>
  <total_paid_tax_incl>$total_paid_tax_incl</total_paid_tax_incl>
  <total_paid_tax_excl>$total_paid_tax_excl</total_paid_tax_excl>
  <total_paid_real>$total_paid_real</total_paid_real>
  <total_products>$total_products</total_products>
  <total_products_wt>$total_products_wt</total_products_wt>
</order>
</prestashop>
XML;
$xml_order = new SimpleXMLElement($psXML_order);
$opt_order = array( 'resource' => 'orders' );
$opt_order['postXml'] = $xml_order->asXML();
$xml_order = $webService->add( $opt_order );
$id_order = $xml_order->order->id;  // ORDER ID

// CHANGE ORDER STATE
//$webService = new PrestaShopWebservice(PS_SHOP_PATH, PS_WS_AUTH_KEY, DEBUG);
//$xml = $webService->get(array('url' => PS_SHOP_PATH.'api/order_histories/?schema=blank'));
//$xml->order_history->id_order = $id_order;
//$xml->order_history->id_order_state = 12;
//$opt = array( 'resource' => 'order_histories' );
//$opt['postXml'] = $xml->asXML();
//$xmloc = $webService->add( $opt );

?>
</body></html>

 

With this code I can successfully create a new cart_rule, a new cart and a new order that all appear in my prestashop backoffice

The problem I have is that even though the discount is correctly added to the order (name and amount are ok and displayed in the order) the order appears with a payment error status ans de cart_rule quantity remains at 1.

The cart_cart_rule and order_cart_rule tables are correctly changed with new entries for that order as well.

The cart_rule is valid for the day so if I enter a new order with the same customer just after the first one, the discount is again added to the order and this time the status is ok (no error payment) and the quantity of the cart_rule is successfully changed to 0.

I tried changing the code by separating it in 2 different files running one after the other from my frontend app. I have the same issue.

I don't want to have 2 buttons on my front end one creating the cart_rule and the other creating order. it might work but it is not what I want for my UX.

Thanks in advance for your help !




 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...