Drupal商业线项目:改变价格? [英] Drupal Commerce Line Items: alter the price?

查看:96
本文介绍了Drupal商业线项目:改变价格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我已经阅读了关于使用规则,但是我需要我的模块能够设置/改变价格而不调用规则



我尝试了一个实体包装器,我尝试改变用commerce_product_line_item_new()创建的行项目,但没有,当订单项进入购物车总是有原始的产品价格(在我的情况下,



如何以编程方式更改订单项价格?



我的代码到目前为止看起来像: / p>

  //调试时,此函数由hook_menu()调用
函数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,
数组(
),
' '
);

$ 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',
数组(
'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_line_item_unit_price_amount()的代码,在商业/ modules / line_item / commerce_line_item.rules.inc中找到,但这个测试:

 <?php 
全局$ user;
$ product = commerce_product_load(4); //我的商业产品测试

$ line_item = commerce_product_line_item_new(
$ product,
1,
0,
数组(
) ,
'cover'//我有这个line_items类型
);

//手动设置金额和组件名称
$ amount = 1234;
$ component_name ='base_price'; //尝试打折,没有变化

$ wrapper = entity_metadata_wrapper('commerce_line_item',$ line_item);
$ unit_price = commerce_price_wrapper_value($ wrapper,'commerce_unit_price',TRUE);

//计算更新的金额,并创建一个代表
/它与当前金额之间的差异的价格数组。
$ 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(),
);

//设置单价金额,并将差额作为组件添加。
$ wrapper-> commerce_unit_price-> amount = $ updated_amount;

$ wrapper-> commerce_unit_price-> data = commerce_price_component_add(
$ wrapper-> commerce_unit_price-> value(),
$ component_name,
$差异,
TRUE
);

$ insert_line_item = commerce_cart_product_add($ user-> uid,$ line_item,FALSE);
?>

仍然失败,line_item进入购物车,但引用产品的原始价格。 p>

任何想法?

解决方案

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

  //更改列表和单个产品页面中的价格。 
函数my_module_commerce_product_calculate_sell_price_line_item_alter($ line_item){

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

}

//更改cart&订购。
函数my_module_commerce_cart_line_item_refresh($ line_item,$ order_wrapper){

$ price = 100; // 1美元
$ line_item-> commerce_unit_price [LANGUAGE_NONE] ['0'] ['amount'] = $ price;
//改变base_price组件。
$ line_item-> commerce_unit_price [LANGUAGE_NONE] ['0'] ['data'] ['components'] ['0'] ['price'] ['amount'] = $ price;

}


I have to add to my cart some line items with a custom amount. The commerce product is saved with price = 0, and my module compute the price and add the line item to the cart/order, but i dont understand how to set programmatically the price.

I've read about using Rules, but I need my module to be able to set/alter the price, without invoking rules.

I've tryed with an entity wrapper, i tryed to alter the line item created with commerce_product_line_item_new(), but nothing, when the line item gets into the cart always has the original product price (in my case, 0).

How to alter a line item price programmatically?

My code so far looks like:

// 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';
}

The strange thing, is that I tryed to adapt the code of commerce_line_item_unit_price_amount() found in commerce/modules/line_item/commerce_line_item.rules.inc, but this 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);
?>

still fail, the line_item get into the cart but with the original price of the referenced product.

Any idea?

解决方案

For those people who don't want to use rules and hope to alter the price directly. Here is my solution:

// 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;

}

这篇关于Drupal商业线项目:改变价格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆