Ciao a tutti.
sono nuovo nell'utilizzo di Drupal. Per motivi lavorativi sto sviluppando un e-commerce utilizzando drupalcommerce su D7.
In questo e-commerce i prezzi dei prodotti variano in base ad un range di quantità, inoltre per ogni fascia di quantità posso avere 2 prezzi, 1 standard e 1 in caso io carichi un logo.
Utilizzando il modulo Price Table ho creato due field di tipo Price Table.
In visualizzazione tutto corretto, vedo le 2 tabelle con i prezzi corretti.
Per gestire il cambio del prezzo in base alla quantità e al fatto che abbia caricato o meno il logo (che è un campo image nel Line Item) ho creato una rule dandogli come evento "After adding a product to the cart" e come action ho creato un codice php che controlla se è presente o meno un logo caricato e fa il confronto sulla quantità selezionando il prezzo corretto. Facendo poi un dump del line item i valori sono aggiornati correttamente ma nella pagina del carrello ho il prezzo calcolato sul prezzo originale del prodotto e non su quelli definiti nei campi Price Table. Allego sotto il codice php della rule:
<?php
// mi salvo quantità, logo e prezzo
$product_quantity = $commerce_line_item->quantity;
$uploaded_logo = $commerce_line_item->field_logo["und"];
$commerce_unit_price = $commerce_line_item->commerce_unit_price["und"][0];
$prices_to_check = NULL;
if(!empty($uploaded_logo)){ // se ho caricato un logo.....
$prices_to_check = $commerce_product->field_custom_prices["und"]; // uso i prezzi per la personalizzazione....
} else { // altrimenti....
$prices_to_check = $commerce_product->field_prices["und"]; // uso i prezzi standard....
}
// Per ognuno dei prezzi presenti nel prodotto...
foreach($prices_to_check as $price_object){
if($price_object["max_qty"] == -1){ // se la max_qty è -1 => vuol dire che è unlimited e considero solo il min_qty
if($product_quantity >= $price_object["min_qty"]){ // se la quantità è compresa nell'intervallo corrente....
$commerce_line_item->commerce_unit_price["und"][0]["amount"] = $price_object["amount"]; // salvo il prezzo corrente nel line item...
$commerce_line_item->commerce_unit_price["und"][0]["data"]["components"][0]["price"]["amount"] = $price_object["amount"];
$commerce_product->commerce_price["und"][0]["amount"] = $price_object["amount"];
break; // interrompo il ciclo...
}
} else {
if($product_quantity >= $price_object["min_qty"] and $product_quantity <= $price_object["max_qty"]){ // se la quantità è compresa nell'intervallo corrente....
$commerce_line_item->commerce_unit_price["und"][0]["amount"] = $price_object["amount"]; // salvo il prezzo corrente nel line item...
$commerce_line_item->commerce_unit_price["und"][0]["data"]["components"][0]["price"]["amount"] = $price_object["amount"];
$commerce_product->commerce_price["und"][0]["amount"] = $price_object["amount"];
break; // interrompo il ciclo...
}
}
}
$commerce_line_item->commerce_total["und"][0]["amount"] = $commerce_line_item->commerce_unit_price["und"][0]["amount"]*$product_quantity;
$commerce_line_item->commerce_total["und"][0]["data"]["components"][0]["price"]["amount"] = $commerce_line_item->commerce_unit_price["und"][0]["data"]["components"][0]["price"]["amount"]*$product_quantity;
//var_dump($commerce_line_item);
//exit();
commerce_line_item_save($commerce_line_item); // salvo il line item...
?>
Spero di essere riuscito a spiegare la problematica. Grazie a tutti!