Hướng dẫn tạo 1 Adjustments và các type khá hay của promotion Drupal 8

Hướng dẫn tạo 1 Adjustments và các type khá hay của promotion Drupal 8

Hướng dẫn tạo 1 Adjustments

<?php
public function process(OrderInterface $order)
  {
    foreach ($order->getItems() as $order_item) {
      // SetAdjustment to empty initially.
      $order_item->setAdjustments([]);
      $product_variation = $order_item->getPurchasedEntity();
      $default_product_variation_id = $order_item->getPurchasedEntityId();
      if (!empty($product_variation)) {
        $product_id = $product_variation->get('product_id')
          ->getValue()[0]['target_id'];
        $product = Product::load($product_id);
        $product_type = $product->get('type')->getValue()[0]['target_id'];
        $quantity = $order_item->getQuantity();
        $product_title = $product->getTitle();
        if ($product_type == 'default') {
          $product_price = $order_item->getUnitPrice();
          $product_unit_price = $product_price->getNumber();
          if ($product_unit_price < 20 && $quantity <= 10) {
            $new_adjustment = $product_unit_price;
          }
          elseif ($product_unit_price < 20 && $quantity > 10) {
            $new_adjustment = ($product_unit_price * 10) / $quantity;
          }
          else {
            continue;
          }
          $adjustments = $order_item->getAdjustments();
          // Apply custom adjustment.
          $adjustments[] = new Adjustment([
            'type' => 'custom_adjustment',
            'label' => 'Discounted Price - ' . $product_title,
            'amount' => new Price('-' . $new_adjustment, 'USD'),
          ]);
          $order_item->setAdjustments($adjustments);
        //  $order_item->save();
          $adjustments;
        }
      }
    }
  }

Các type khá hay của promotion Drupal 8

  $promotions = $promotion_storage->loadMultiple($result);
  foreach ($promotions as $promotion) {
    $needs_save = FALSE;
    $needs_disable = FALSE;

    $conditions = $promotion->getConditions();
    $order_item_conditions = array_filter($conditions, function ($condition) {
      /** @var \Drupal\commerce\Plugin\Commerce\Condition\ConditionInterface $condition */
      return $condition->getEntityTypeId() == 'commerce_order_item' && $condition->getPluginId() != 'order_item_quantity';
    });
    $condition_map = [
      'order_item_product' => 'order_product',
      'order_item_product_type' => 'order_product_type',
      'order_item_variation_type' => 'order_variation_type',
    ];
    $condition_items = $promotion->get('conditions')->getValue();

    $known_order_item_offers = [
      'order_item_fixed_amount_off',
      'order_item_percentage_off',
    ];
    $offer = $promotion->getOffer();
    $offer_item = $promotion->get('offer')->first()->getValue();

    // Transfer order item conditions to the offer.
    if ($offer->getEntityTypeId() == 'commerce_order_item') {
      // Modify the offer item directly to be able to upgrade offers that
      // haven't yet been converted to extend OfferItemPromotionOfferBase.
      $offer_item['target_plugin_configuration']['conditions'] = [];
      foreach ($order_item_conditions as $condition) {
        $offer_item['target_plugin_configuration']['conditions'][] = [
          'plugin' => $condition->getPluginId(),
          'configuration' => $condition->getConfiguration(),
        ];
      }
      $needs_save = TRUE;
    }

    // The promotion is using a custom offer, disable it so that it can
    // get updated.
    if (!in_array($offer->getPluginId(), $known_order_item_offers)) {
      $needs_disable = TRUE;
    }

    // Convert known order item conditions to order conditions.
    if ($order_item_conditions) {
      foreach ($condition_items as $condition_item) {
        if (array_key_exists($condition_item['target_plugin_id'], $condition_map)) {
          $condition_item['target_plugin_id'] = $condition_map[$condition_item['target_plugin_id']];
          $needs_save = TRUE;
        }
      }
    }

    // Drop unknown order item conditions.
    $conditions = $promotion->getConditions();
    $order_item_conditions = array_filter($conditions, function ($condition) {
      /** @var \Drupal\commerce\Plugin\Commerce\Condition\ConditionInterface $condition */
      return $condition->getEntityTypeId() == 'commerce_order_item' && $condition->getPluginId() != 'order_item_quantity';
    });
    foreach ($order_item_conditions as $condition) {
      foreach ($condition_items as $index => $condition_item) {
        if ($condition_item['target_plugin_id'] == $condition->getPluginId()) {
          unset($condition_items[$index]);
          $needs_save = TRUE;
          // An unrecognized offer was dropped, but because the offer applies
          // to the order, wasn't transferred there. Disable the promotion
          // to allow the merchant to double check the new configuration.
          if ($offer->getEntityTypeId() == 'commerce_order') {
            $needs_disable = TRUE;
          }
        }
      }
    }

    if ($needs_disable) {
      $disabled[] = $promotion->label();
      $promotion->setEnabled(FALSE);
    }
    if ($needs_save) {
      $promotion->set('offer', $offer_item);
      $promotion->set('conditions', array_values($condition_items));
      $promotion->save();
    }
  }

  $sandbox['current_count'] = 25;
  if ($sandbox['current_count'] >= $sandbox['total_count']) {
    $sandbox['#finished'] = 1;
  }
  else {
    $sandbox['#finished'] = ($sandbox['total_count'] - $sandbox['current_count']) / $sandbox['total_count'];
  }
}

$condition = $promotion->Id()->getConditions();
        
$coupon = $order->get('coupons')->first()->get('entity')->getTarget()->getValue();
Bạn thấy bài viết này như thế nào?: 
Average: 10 (4 votes)
Ảnh của Tommy Tran

Tommy owner Express Magazine

Drupal Developer having 9+ year experience, implementation and having strong knowledge of technical specifications, workflow development. Ability to perform effectively and efficiently in team and individually. Always enthusiastic and interseted to study new technologies

  • Skype ID: tthanhthuy

Bình luận (0)

 

Add Comment

Filtered HTML

  • Các địa chỉ web và email sẽ tự động được chuyển sang dạng liên kết.
  • Các thẻ HTML được chấp nhận: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Tự động ngắt dòng và đoạn văn.

Plain text

  • No HTML tags allowed.
  • Các địa chỉ web và email sẽ tự động được chuyển sang dạng liên kết.
  • Tự động ngắt dòng và đoạn văn.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.

Advertisement

 

jobsora

Dich vu khu trung tphcm

Dich vu diet chuot tphcm

Dich vu diet con trung

Quảng Cáo Bài Viết

 
Một số hàm thao tác với shortcode trong wordpress

Một số hàm thao tác với shortcode trong wordpress

Bài viết thực hiện dựa trên tài liệu Wordpress ở phiên bản 3.5.1. Shortcode được giới thiệu từ phiên bản 2.5, nó sẽ thực hiện một số chức năng nào đó bằng việc tạo một đoạn mã theo cú pháp trong nội dung bài viết

Các công cụ kiểm tra tốc độ load website tốt nhất hiện nay

Các công cụ kiểm tra tốc độ load website tốt nhất hiện nay

Google và các search engine ngày càng quan tâm đến tốc độ load của các website.

Western Digital ngừng sản xuất ổ đĩa cứng dùng giao tiếp PATA từ 29/12

Western Digital ngừng sản xuất ổ đĩa cứng dùng giao tiếp PATA từ 29/12

Western Digital (WD) vừa cho biết rằng họ sẽ dừng sản xuất các ổ cứng sử dụng cổng kết nối PATA(parallel-ATA) thuộc dòng Caviar kể từ ngày 29/12/2013.

Công ty diệt chuột T&C

 

Diet con trung