カスタム金額の広告申込情報をカートに追加する必要があります。 コマース商品はprice = 0で保存され、モジュールは価格を計算してカート/オーダーに追加しますが、プログラムで価格を設定する方法はわかりません。Drupal Commerceラインアイテム:価格を変更しますか?
ルールの使用については読んだことがありますが、ルールを使用せずに価格を設定/変更できるようにするには、が必要です。
私はエンティティラッパーを試しましたが、commerce_product_line_item_new()で作成された広告申込情報を変更しようとしましたが、広告申込情報がカートに入るときは常にオリジナルの商品価格(私の場合は0) 。
プログラムで広告申込情報の価格を変更するにはどうすればよいですか?
私のコードは、これまでのようになります。
// 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';
}
奇妙なことに、私は商業/モジュールは/ LINE_ITEM/commerce_line_item.rules.incで見つけcommerce_line_item_unit_price_amount()のコードを適応させるためにしようと試みたが、これをということです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);
?>
まだ失敗すると、line_itemはカートに入るが、参照された製品の元の価格となる。
あなたのラッパーを保存していない...それは(すなわち、 '$ line_item_wrapper->保存の問題かもしれません(); ') – Clive
@Clive私はそれもしようと試みたのだと思いますが、私は別の私が何かをやっていたとき、念のために – Strae
ああ、あなたはまた、' $のorder_wrapper'を保存する必要があります(1つは私を得てみてください与えてみましょう同様の数ヶ月前) – Clive