gpt4 book ai didi

drupal-8 - 在 Drupal 8 Commerce 中,如何以编程方式从当前购物车中获取订单 ID 和产品变体 ID?

转载 作者:行者123 更新时间:2023-12-03 19:36:45 25 4
gpt4 key购买 nike

我想得到:

  • 当前用户的订单ID;
  • 当前用户购物车中的所有产品变体 ID;
  • 产品变体的价格;
  • 图片;
  • 标题;

  • 我有以下几点:
    $store_id = 1;
    $order_type = 'default';
    $entity_manager = \Drupal::entityManager();
    $cart_manager = \Drupal::service('commerce_cart.cart_manager');
    $cart_provider = \Drupal::service('commerce_cart.cart_provider');
    $store = $entity_manager->getStorage('commerce_store')->load($store_id);

    $cart = $cart_provider->getCart($order_type, $store);

    if(isset($cart)){
    $itemNum = count($cart->getItems());
    ($itemNum > 0) ? $variables['items_in_cart'] = $itemNum : '';
    }

    $cart .= 'Subtotal: '.$cart->getSubtotalPrice();
    $cart .= '<br>Total price: '.$cart->getTotalPrice();
    $cart .= '<br>Created: '.$cart->getCreatedTime();
    $cart .= '<br>Customer id: '.$cart->getCustomerId();
    $cart .= '<br>Email: '.$cart->getEmail();
    $cart .= '<br>IP address: '.$cart->getIpAddress();
    $cart .= '<br>Item num. in cart: '.$itemNum;

    最佳答案

    您需要加载 Product store, cart and variation entity , 您需要从加载的产品中获取产品变体 ID,然后提取 Order ID for the current user, All product variation ID from the current users cart, Prices for Product variations, Images, Titles .

    $product_variation = $event->getEntity();
    $current_store = \Drupal::service('commerce_store.current_store');
    $store = $current_store->getStore();
    $cart_provider = \Drupal::service('commerce_cart.cart_provider');
    $cart = $cart_provider->getCart("default", $store);

    foreach ($cart->getItems() as $order_item) {
    $productType = $order_item->getPurchasedEntity()->get('type')[0]->get('target_id')->getValue();
    // ksm("Order Item", $order_item);
    // For getting custom and variation field value.
    //$field_custom_value = $product_variation->get('field_custom_value')[0]->get('target_id')->getValue();
    $order_no = $order_item->get('purchased_entity')[0]->get('target_id')->getValue();
    ksm("Order No", $order_no);
    $order_no_id = $order_item->get('order_item_id')[0]->get('value')->getValue();
    ksm("Order No Id", $order_no_id);

    }

    关于drupal-8 - 在 Drupal 8 Commerce 中,如何以编程方式从当前购物车中获取订单 ID 和产品变体 ID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47938545/

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