gpt4 book ai didi

drupal - 以编程方式将优惠券代码应用于购物车/订单

转载 作者:行者123 更新时间:2023-12-01 15:57:41 27 4
gpt4 key购买 nike

我已经尝试申请 Drupal 商业优惠券大约 2 天了。我已经负责验证优惠券,但目前在尝试兑换优惠券时遇到了困难。

所以在我的回调函数中,我正在调用:

my_module_coupons_coupon_redeem($coupon);

在兑换函数中我有:

function my_module_coupons_coupon_redeem($coupon) { 
global $user;
$uid = $user->uid;

$order = commerce_cart_order_load($uid);

// Wrap the order for easy access to field data.
$order_wrapper = entity_metadata_wrapper('commerce_order', $order);

// Create the new line item.
$line_item = commerce_coupon_line_item_new($coupon, $order->order_id);
$line_item->commerce_unit_price = array('und' => array(
'0' => array('amount' => 500, 'currency_code' => commerce_default_currency())
));

$line_item_wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);
if (!is_null($line_item_wrapper->commerce_unit_price->value())) {
// Add the base price to the components array.
if (!commerce_price_component_load($line_item_wrapper->commerce_unit_price->value(), 'base_price')) {
$line_item_wrapper->commerce_unit_price->data = commerce_price_component_add(
$line_item_wrapper->commerce_unit_price->value(),
'base_price',
$line_item_wrapper->commerce_unit_price->value(),
TRUE
);
}
}

$line_item_wrapper->commerce_total->data = $line_item_wrapper->commerce_unit_price->data;
//$line_item_wrapper->commerce_product->data = $coupon;

// Save the line item now so we get its ID.
commerce_line_item_save($line_item);

// Add it to the order's line item reference value.
$order_wrapper->commerce_line_items[] = $line_item;

commerce_order_save($order);
}

优惠券 line_item 正在保存在数据库中,但是当我刷新购物车页面时出现以下错误:

EntityMetadataWrapperException: Unknown data property commerce_product. in EntityStructureWrapper->getPropertyInfo()

只是想知道这是否是不使用规则应用优惠券的正确方法,我是否应该首先将其保存为订单项?

最佳答案

查看 sites/all/modules/commerce_coupon/oncludes/commerce_coupon.checkout_pane.inc

global $user;
$uid = $user->uid;
$error = '';

// load the cart
$order = commerce_cart_order_load($uid);

// load the coupon from the coupon code
// see MySQL -> your-schema, table: commerce_coupon, column: code
$coupon = commerce_coupon_redeem_coupon_code($code, $order, $error);

// 302 to the cart
drupal_goto('checkout/' . $order->order_id);

关于drupal - 以编程方式将优惠券代码应用于购物车/订单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18821392/

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