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

标签 drupal drupal-7

我已经尝试申请 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/

相关文章:

drupal - 添加一个类到 "body"

mysql - 为每天有 20,000 名访问者的多站点 Drupal 安装扩展 MYSQL 数据库

css - 带有 drupal zen 主题和漂亮菜单的 Firefox 渲染问题

mysql - drush sql-sync 上的访问被拒绝(远程用户和数据库用户不同)

drupal - 自定义 page-xxxx.tpl.php 不起作用

linux - Drupal 迁移问题 block 未显示

drupal - 如何从标签上删除冒号?

php - Drupal 7 : Randomize header background passing file name from template. php 到 css 文件

drupal - 如何避免 entity_load 返回的实体中每个字段中的 'und' 键?

javascript - Drupal 7 #ajax 更改事件阻止同一元素上的其他更改事件处理程序