Jump to content

Resuelto - Código para enviar pedido generado en Tienda de PS a otra PS por Webservice


gusman126

Recommended Posts

Hola a todos, ya que el foro en la zona de ingles esta este post, he querido ponerlo en la zona de español para tenerlo todos nosotros

Aqui el original :

 

 

Este es el código que uso al Validar un pedido en una tienda Ps y que lo envié por Webservice a otro PS Principal

Añado una comprobación de stock en tienda principal, para enviar o no el pedido

La API y URL utilizo un modulo para configurar y por eso usa valores de configuración

public function hookActionValidateOrder($params)
    {
        /* Place your code here. */
		
		require_once(dirname(__FILE__).'/lib/PSWebServiceLibrary.php');
		$api =Configuration::get('MODULO_API');
		$url = Configuration::get('MODULO_URL');
		
		$id_lang = Configuration::get('PS_LANG_DEFAULT');
		
				
		$id_order = $params['order']->id;
		$id_original = $params['order']->id;
		$order = new Order($id_order);
			
		$error = false;
		//productos
		
		$products = $params['order']->getProducts();
		foreach ($products AS $producto){
			$id_pro_order = $producto['product_id'];
			
			$id_product = $id_pro_order;
			
			$haystock = self::checkstock($id_product);
			if(!$haystock){
				$error = true;
			}
			if($haystock < $producto['product_quantity']){
				$error = true;
			}
			
			$id_tienda = $id_pro_order;
			$id_product_attribute = $producto["product_attribute_id"];
			
			$price = Product::getPriceStatic($id_tienda, $usetax = true, $id_product_attribute, $decimals = 2);
			$cantidad_pro_order = $producto['product_quantity'];
			$pros[] = array(
				'id_product' => $id_product,
				'price' => $price,
				'quantity' => $cantidad_pro_order,
				'id_product_attribute' => $id_product_attribute,
			);
			
		}
		if($error){
			return ;
		}
		
		
		$id_currency = $order->id_currency;
		
		
		$hoy = date_create(date('Y-m-d '));
		$date = date_format($hoy, 'Y-m-d H:i:s');

		//cliente
		$cliente = new Customer($order->id_customer);
		$webService = new PrestaShopWebservice($url, $api, false);
		$xml = $webService->get( array( 'url' => $url .'api/customers?schema=blank' ) );
		$xml->customer->passwd      = $cliente->passwd;
		$xml->customer->lastname    = $cliente->lastname;
		$xml->customer->firstname   = $cliente->firstname;
		$xml->customer->email       = $cliente->email;
		$xml->customer->newsletter  = $cliente->newsletter;
		$xml->customer->ip_registration_newsletter  = $cliente->ip_registration_newsletter;
		$xml->customer->is_guest  = $cliente->is_guest;
		$xml->customer->newsletter_date_add = $date;
		$xml->customer->last_passwd_gen     = $date;
		$xml->customer->date_add            = $date;
		$xml->customer->date_upd            = $date;
		$opt = array( 'resource' => 'customers' );
		$opt['postXml'] = $xml->asXML();
		$xml = $webService->add( $opt );
		$id_customer = $xml->customer->id;

		$datoscliente_array = array(
			'firstname' => $cliente->firstname,
			'lastname' => $cliente->lastname,
			'email' => $cliente->email,
			'id_shop_group' => $cliente->id_shop_group,
			'newsletter' => $cliente->newsletter,
			'passwd' => $cliente->passwd,
			'ip_registration_newsletter' => $cliente->ip_registration_newsletter,
			'is_guest' => $cliente->is_guest,
			
			
			
		);
		
		//direccion envio
		$addressDelivery = new Address((int)$order->id_address_delivery);
		$webService = new PrestaShopWebservice($url, $api, false);
		$xml = $webService->get( array( 'url' => $url .'api/addresses?schema=blank' ) );
		$xml->address->id_customer  = $id_customer;
		$xml->address->id_country   = 6;
		$xml->address->alias        = 'Dirección ';
		$xml->address->lastname     = $addressDelivery->lastname;
		$xml->address->firstname    = $addressDelivery->firstname;
		$xml->address->city         = $addressDelivery->city;
		$xml->address->address1     = $addressDelivery->address1;
		$xml->address->phone_mobile = $addressDelivery->phone_mobile;
		$xml->address->phone = $addressDelivery->phone;
		$xml->address->postcode     = $addressDelivery->postcode;
		$xml->address->date_add     = $date;
		$xml->address->date_upd     = $date;
		$xml->address->dni     = $addressDelivery->dni;
		$opt = array( 'resource' => 'addresses' );
		$opt['postXml'] = $xml->asXML();
		$xml = $webService->add( $opt );
		$id_address = $xml->address->id; 
		
		
		
		
		
		//carrito
		$webService = new PrestaShopWebservice($url, $api, false);
		$xml = $webService->get( array( 'url' => $url .'api/carts?schema=blank' ) );
		$xml->cart->id_currency         = $id_currency;
		$xml->cart->id_lang             = $id_lang;
		$lin = 0;
		foreach($pros as $pro){
			$xml->cart->associations->cart_rows->cart_row[$lin]->id_product            = $pro['id_product'];
			$xml->cart->associations->cart_rows->cart_row[$lin]->id_product_attribute  = $pro['id_product_attribute'];
			$xml->cart->associations->cart_rows->cart_row[$lin]->id_address_delivery   = $id_address;
			$xml->cart->associations->cart_rows->cart_row[$lin]->quantity              = $pro['quantity'];
			
			$lin++;	
		}
		$xml->cart->id_address_delivery = $id_address;
		$xml->cart->id_address_invoice  = $id_address;
		$xml->cart->id_customer         = $id_customer;
		
		$xml->cart->carrier             = 4;
		
		$xml->cart->date_add            = $date;
		$xml->cart->date_upd            = $date;
		$opt = array( 'resource' => 'carts' );
		$opt['postXml'] = $xml->asXML();
		$xml = $webService->add( $opt );
		$id_cart = $xml->cart->id;   
		
		
		//pedido ws
		 
		$total_products = $order->total_products;
		$total_products_wt = $order->total_products_wt;
		$total_paid = $order->total_paid;
		$total_paid_real = $order->total_paid_real;
		$id_status = $order->current_state;
		$total_discounts = $order->total_discounts;
		$total_discounts_tax_incl = $order->total_discounts_tax_incl;
		$total_discounts_tax_excl = $order->total_discounts_tax_excl;
		$total_paid_tax_incl = $order->total_paid_tax_incl;
		$total_paid_tax_excl = $order->total_paid_tax_excl;	
		$total_shipping = $order->total_shipping;	
		$total_shipping_tax_incl = $order->total_shipping_tax_incl;	
		$total_shipping_tax_excl = $order->total_shipping_tax_excl;
		
		$tax =  $total_products_wt - $total_products ;
		$msga = CustomerMessage::getMessagesByOrderId($id_order, $private = true);
		$totalenvio = $params['order']->total_shipping;
		$descuentos = $order->getCartRules();
		$pedido = $params['order']->reference;
		$cenvio = round(($order->total_shipping /1.21),2);
		
		
		$webService = new PrestaShopWebservice($url, $api, false);
		$xml = $webService->get(array('url' => $url .'api/orders/?schema=blank'));
		$xml->order->id_address_delivery    = $id_address; // Customer address
		$xml->order->id_address_invoice     = $id_address;        
		$xml->order->id_cart                = $id_cart; 
		$xml->order->id_currency            = $id_currency;
		$xml->order->id_lang                = $id_lang;
		$xml->order->id_customer            = $id_customer; 
		$xml->order->module                 = 'ps_wirepayment';
		$xml->order->payment                = Configuration::get('PS_SHOP_NAME');        
        $xml->order->total_paid             = $total_paid;
        $xml->order->total_paid_real        = $total_paid_real;
        $xml->order->total_products         = $total_products;
        $xml->order->total_products_wt      = $total_products_wt;
		$xml->order->id_carrier            = 4; 
		$xml->order->invoice_number			= 0;

        $xml->order->conversion_rate        = 1;
		$xml->order->valid                      = 1; 
        $xml->order->current_state              = $id_status;        
        $xml->order->total_discounts            = $total_discounts;
        $xml->order->total_discounts_tax_incl   = $total_discounts_tax_incl;
        $xml->order->total_discounts_tax_excl   = $total_discounts_tax_excl;
        $xml->order->total_paid_tax_incl        = $total_paid_tax_incl;
        $xml->order->total_paid_tax_excl        = $total_paid_tax_excl;
        $xml->order->total_shipping             = $total_shipping;
        $xml->order->total_shipping_tax_incl    = $total_shipping_tax_incl;
        $xml->order->total_shipping_tax_excl    = $total_shipping_tax_excl;
		
		$lin = 0;
		foreach($pros as $pro){
			$xml->order->associations->order_rows->order_row[$lin]->product_id             = $pro['id_product'];
			$xml->order->associations->order_rows->order_row[$lin]->product_attribute_id   = $pro['id_product_attribute'];
			$xml->order->associations->order_rows->order_row[$lin]->product_quantity       = $pro['quantity'];
			$xml->order->associations->order_rows->order_row[0]->product_price          = $pro['price'];
			$lin++;
		}
		$opt = array( 'resource' => 'orders' );
        $opt['postXml'] = $xml->asXML();
        $xml = $webService->add( $opt );
        $id_order = $xml->order->id; 
		$idtienda = $xml->order->id;
		
		
}
public function checkstock($id_product){
		require_once(dirname(__FILE__).'/lib/PSWebServiceLibrary.php');
		$api =Configuration::get('MODULO_API');
		$url = Configuration::get('MODULO_URL');
		$id_lang = Configuration::get('PS_LANG_DEFAULT');
		$webService = new PrestaShopWebservice($url, $api, false);
		try {
			$opt = array(
				'resource' => 'products',
				'id' => $id_product,
				
			);
			$xmlprs = $webService->get($opt);
		
		}catch(Exception $e){
			echo "Error en conexión 4!";
			return 0;
		
		}
		return $xmlprs->product->quantity;
		
		
		
	}             

 

Edited by gusman126 (see edit history)
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...