मुझे अपने कार्ट में कस्टम लाइन के साथ कुछ लाइन आइटम जोड़ना होगा। वाणिज्य उत्पाद की कीमत = 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_line_item_unit_price_amount() वाणिज्य/मॉड्यूल/LINE_ITEM/commerce_line_item.rules.inc में पाए जाने वाले कोड अनुकूल करने के लिए tryed है, लेकिन यह है परीक्षण:
<?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 मुझे लगता है कि मैं वह भी tryed किया था, लेकिन मुझे दे एक और सिर्फ मामले में कोशिश – Strae
ओह, तुम भी' $ order_wrapper' (जब मैं कुछ कर रहा था कि मुझे मिल गया सहेजने की आवश्यकता होगी जाने इसी तरह कुछ महीने पहले) – Clive