Muszę dodać do koszyka niektóre elementy zamówienia z niestandardową kwotą. Produkt komercyjny jest zapisywany z ceną = 0, a mój moduł oblicza cenę i dodaje element zamówienia do koszyka/zamówienia, ale nie rozumiem, jak ustawić programowo cenę.Elementy Drupal Commerce Line: zmienić cenę?
Przeczytałem o używaniu reguł, ale potrzebuję mojego modułu, aby móc ustawić/zmienić cenę, bez wywoływania reguł.
Próbowałem z opakowaniem jednostkowym, próbowałem zmienić element zamówienia utworzony za pomocą funkcji commerce_product_line_item_new(), ale nic, gdy element zamówienia trafia do koszyka, zawsze ma oryginalną cenę produktu (w moim przypadku 0) .
Jak programowo zmienić cenę elementu zamówienia?
Mój kod wygląda tak daleko:
// For debugging, this function is called by hook_menu()
function mymodule_test($product_id)
{
global $user;
$user = user_load($user->uid);
$order = commerce_cart_order_load($user->uid);
$order_wrapper = entity_metadata_wrapper('commerce_order', $order);
$product = commerce_product_load($product_id);
$line_item = commerce_product_line_item_new(
$product,
1,
0,
array(
),
'cover'
);
$line_item_wrapper = entity_metadata_wrapper("commerce_line_item", $line_item);
$line_item_wrapper->commerce_unit_price->data = commerce_price_component_add(
$line_item_wrapper->commerce_unit_price->value(),
'base_price',
array(
'amount' => 1234,
'currency_code' => 'EUR',
'data' => array(),
),
TRUE
);
$insert_line_item = commerce_cart_product_add($user->uid, $line_item_wrapper->value(), FALSE);
return 'done';
}
Najdziwniejsze jest to, że ja tryed dostosować kod commerce_line_item_unit_price_amount() znalezione w handlu/modules/LINE_ITEM/commerce_line_item.rules.inc, ale ten test:
<?php
global $user;
$product = commerce_product_load(4); // my commerce product for test
$line_item = commerce_product_line_item_new(
$product,
1,
0,
array(
),
'cover' // I do have this line_items type
);
// manually set amount and component name
$amount = 1234;
$component_name = 'base_price'; // tryed with discount, nothing change
$wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);
$unit_price = commerce_price_wrapper_value($wrapper, 'commerce_unit_price', TRUE);
// Calculate the updated amount and create a price array representing the
// difference between it and the current amount.
$current_amount = $unit_price['amount'];
$updated_amount = commerce_round(COMMERCE_ROUND_HALF_UP, $amount);
$difference = array(
'amount' => $updated_amount - $current_amount,
'currency_code' => $unit_price['currency_code'],
'data' => array(),
);
// Set the amount of the unit price and add the difference as a component.
$wrapper->commerce_unit_price->amount = $updated_amount;
$wrapper->commerce_unit_price->data = commerce_price_component_add(
$wrapper->commerce_unit_price->value(),
$component_name,
$difference,
TRUE
);
$insert_line_item = commerce_cart_product_add($user->uid, $line_item, FALSE);
?>
nadal nie powiodło się, linia line_item dostaje się do koszyka, ale z pierwotną ceną produktu referencyjnego.
Każdy pomysł?
Nie jesteś zapisywania obwolut ... że może być problem (czyli '$ line_item_wrapper-> Zapisz(); ') – Clive
@ Clive Myślę, że też to wypróbowałem, ale pozwól mi spróbować jeszcze raz na wszelki wypadek. – Strae
Och, musisz też zapisać' $ order_wrapper' (ten dostał mnie, gdy robiłem coś podobnie kilka miesięcy wstecz) – Clive