结帐后根据产品供应商拆分订单(WCFM市场)

时间:2019-10-19 15:34:41

标签: wordpress woocommerce

我需要先拆分订单,然后再根据产品供应商的不同来浏览页面 例如,如果订单包含5个来自不同供应商的产品 -X供应商的2个产品 -Z供应商的2个产品 -1个供应商的产品 所以我需要根据原始订单数据生成3个不同的订单,并存储生成的订单而不是原始订单 我使用的是WCFM市场插件,但我发现了一个高级插件可以做到这一点,但需要另一个多供应商插件

这是我在代码中所做的事情,但出现内部服务器错误

add_action( 'woocommerce_checkout_create_order', 'HF_before_checkout_create_order', 20, 2 ); 
function HF_before_checkout_create_order( $order, $data ){
  global $wpdb;
  global $WCFM;
  global $woocommerce;

  $vendors = array();

  foreach( $order->get_items() as $key => $item ) {
    $product_id = $item['product_id'];
    $vendor = $WCFM->wcfm_vendor_support->wcfm_get_vendor_id_from_product( $product_id );
    if( $vendor ) {
      $vendors[] = $vendor;
      // wc_delete_order_item( $key );
    }

    if( count($vendors) ) :

      foreach( $vendors as $vendor_order ) :

          $order_seller = new WC_Order();

          foreach( $data['line_items'] as $line_item ) {
              $args = $line_item['args'];
              $product = wc_get_product( isset($args['variation_id']) && $args['variation_id'] > 0 ? $$args['variation_id'] : $args['product_id'] );
              $product_vendor = $WCFM->wcfm_vendor_support->wcfm_get_vendor_id_from_product( $product->ID );
              if( $product_vendor == $vendor_order ) :
                $order_seller->add_product( $product, $line_item['quantity'], $line_item['args'] );
              endif;
          }

        $order_seller->set_address( $order->get_address( 'billing' ), 'billing' );
        $order_seller->set_address( $order->get_address( 'shipping' ), 'shipping' );

        $order_seller->calculate_taxes();
        $order_seller->calculate_totals();
        $order_seller->save();

      endforeach;

    endif;
  }

  $order->update_status(
    'cancelled',
    __( 'This order has been splitted to separate orders' )
  );
  $history = $order->addStatusHistoryComment('Order marked as cancelled.', false);
  $history->setIsCustomerNotified(false);
  $order->save();

}

0 个答案:

没有答案