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.
Image CAPTCHA
Enter the characters shown in the image.

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

 
Phần 3 - Custom Image Search với Solr, Filefield Sources và Ctools

Phần 3 - Custom Image Search với Solr, Filefield Sources và Ctools

In our quest to build a custom image search functionality (see parts I and II), we are at the last two steps. 

mạng quảng cáo Yandex

Giới thiệu về Yandex và mạng quảng cáo Yandex

Bài viết này mình sẽ tập hợp các thông tin về Yandex – một trong những công ty công nghệ hàng đầu tại Liên bang Nga, điểm qua một số dịch vụ mà Yandex đang cung cấp.

Broad Match Modifier: từ khóa mở rộng bổ sung

Tìm hiểu về Broad Match Modifier

Chúng tôi đã có một bài viết giới thiệu 4 dạng từ khóa trong quảng cáo google adwords: từ khóa mở rộng, từ khóa cụm từ, từ khóa chính xác và từ khóa phủ định. Hôm nay, chúng tôi tiếp tục giới thiệu thêm một dạng từ khóa mới: từ khóa mở rộng bổ sung – Broad Match Modifier, từ khóa này có biên độ nằm giữa từ khóa mở rộng và từ khóa cụm từ.

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

 

Diet con trung