gpt4 book ai didi

Drupal Commerce 行项目 : alter the price?

转载 作者:行者123 更新时间:2023-12-04 01:28:10 25 4
gpt4 key购买 nike

我必须将一些自定义金额的订单项添加到我的购物车中。
商务产品以价格 = 0 保存,我的模块计算价格并将行项目添加到购物车/订单,但我不明白如何以编程方式设置价格。

我已经阅读了有关使用规则的信息,但我需要我的模块能够设置/更改价格,不调用规则 .

我尝试过使用实体包装器,我尝试更改使用 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';
}

奇怪的是,我尝试修改 commerce/modules/line_item/commerce_line_item.rules.inc 中的 commerce_line_item_unit_price_amount() 代码,但是这个测试:
<?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 以引用产品的原价进入购物车。

任何的想法?

最佳答案

我今天整天都在努力解决这个问题,最终找到了改变订单项价格的正确途径。问题是,即使您成功地将订单项价格更改为自定义值,在下一页刷新购物车时,也会重置订单项以匹配原始产品价格。看看 commerce_cart_order_refresh() 功能了解详情。每次在页面上加载订单/购物车时都会执行此功能,并且无法绕过它。

事实证明,更改行项目价格的正确方法是使用规则或实现 hook_commerce_cart_line_item_refresh() 功能。无论哪种方式,Drupal Commerce 都需要能够在每次加载购物车/订单时应用更改逻辑。

我最终在 Line Item 中创建了一个自定义字段,我在其中存储了我想要的自定义价格值。然后,每当刷新购物车时,我使用定价规则将自定义价格值复制到产品价格值。

以下博客文章对解决这个问题非常有帮助。它向您展示了如何将自定义字段添加到行项目类型以及如何设置定价规则以将自定义金额复制到单价。

http://commerceguys.com/blog/using-custom-line-items-provide-donation-feature-drupal-commerce

关于Drupal Commerce 行项目 : alter the price?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13491983/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com