Drupal Commerce Line项目:改变价格?

Str*_*rae 11 drupal drupal-7 drupal-commerce

我必须在购物车中添加一些自定义金额的订单项.商品保存,价格= 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';
}
Run Code Online (Sandbox Code Playgroud)

奇怪的是,我尝试调整在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);
?>
Run Code Online (Sandbox Code Playgroud)

仍然失败,line_item进入购物车,但与参考产品的原始价格.

任何的想法?

Tim*_*Yao 19

对于那些不想使用规则并希望直接改变价格的人.这是我的解决方案:

// Alter the price in list and single product page.
function my_module_commerce_product_calculate_sell_price_line_item_alter($line_item){

    $price = 100; //1 dollar
    $line_item->commerce_unit_price[LANGUAGE_NONE]['0']['amount'] = $price;

}

// Alter the price in cart & order.
function my_module_commerce_cart_line_item_refresh($line_item, $order_wrapper){

    $price = 100; //1 dollar
    $line_item->commerce_unit_price[LANGUAGE_NONE]['0']['amount'] = $price;
    // Alter the base_price component.
    $line_item->commerce_unit_price[LANGUAGE_NONE]['0']['data']['components']['0']['price']['amount'] = $price;

}
Run Code Online (Sandbox Code Playgroud)


小智 6

如果您要忽略以前保存到订单项的任何值,并重新计算新金额的总额,那么您要查找的函数是commerce_line_item_rebase_unit_price.

设置新的金额值,然后在那里运行您的订单项,保存订单项和订单:

$line_item_wrapper->commerce_unit_price->amount = 13;

commerce_line_item_rebase_unit_price($line_item_wrapper->value());

commerce_line_item_save($line_item_wrapper->value());
Run Code Online (Sandbox Code Playgroud)

  • 在我看来,这是最好的答案.我确信在某些情况下Drupal Commerce的复杂定价系统非常有用,但在用例中我发现我只是想**设置价格**,我不想要实现另一个钩子(从而使我的代码更难理解). (2认同)

Nat*_*eck 5

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

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

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

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

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