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

 
Bóng ma xuất hiện ở nghĩa địa giữa ban ngày

Bóng ma xuất hiện ở nghĩa địa giữa ban ngày

Mặc dù giữa ban ngày, nhưng ông John Finch vẫn khẳng định, hồn ma xuất hiện trong bức ảnh chính là nguyên nhân khiến ông cảm thấy lạnh gáy.

Sức mạnh của HTML 5 trong thiết kế web mobile

Sức mạnh của HTML 5 trong thiết kế web mobile

Công nghệ thiết kế website bằng HTML 5 thì ra đời khá lâu rồi. HTML 5 là công nghệ. Theo IBM thì

Xây dựng Drupal Commerce store hầu hết no coding

Xây dựng Drupal Commerce store hầu hết no coding

Hi everyone, here is a case study about the recent personal business that I started, and how I’ve been able to build a complete working eCommerce store with Drupal Commerce

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

 

Diet con trung