【问题标题】:woocommerce auto restore stock quanitiy hook when order refundwoocommerce 订单退款时自动恢复库存数量挂钩
【发布时间】:2017-04-07 07:40:33
【问题描述】:

订单退款时我需要 woocommerce 自动恢复库存数量挂钩 请任何人对此提供帮助 我尝试了 2 个钩子

 add_action( 'woocommerce_product_set_stock','action_woocommerce_variation_set_stock', 10, 1 );
add_action( 'woocommerce_variation_set_stock', 'action_woocommerce_variation_set_stock', 10, 1 );

但是当产品库存数量减少时,这些钩子在前端工作

请解决这个问题

【问题讨论】:

    标签: woocommerce hook-woocommerce


    【解决方案1】:

    尝试使用以下动作钩子

    add_action( 'woocommerce_order_status_processing_to_refunded', 'YOUR_ACTION_NAME', 10, 1 );
    add_action( 'woocommerce_order_status_completed_to_refunded', 'YOUR_ACTION_NAME', 10, 1 );
    add_action( 'woocommerce_order_status_on-hold_to_refunded', 'YOUR_ACTION_NAME', 10, 1 );
    add_action( 'woocommerce_order_status_processing_to_refunded', 'YOUR_ACTION_NAME', 10, 1 );
    add_action( 'woocommerce_order_status_completed_to_refunded', 'YOUR_ACTION_NAME', 10, 1 );
    add_action( 'woocommerce_order_status_on-hold_to_refunded', 'YOUR_ACTION_NAME', 10, 1 );
    add_action( 'woocommerce_order_status_failed_to_refunded', 'YOUR_ACTION_NAME', 10, 1 );
    
    function YOUR_ACTION_NAME( $order_id ) {
            $order = new WC_Order( $order_id );
    
            if ( ! get_option('woocommerce_manage_stock') == 'yes' && ! sizeof( $order->get_items() ) > 0 ) {
                return;
            }
    
            foreach ( $order->get_items() as $item ) {
    
                if ( $item['product_id'] > 0 ) {
                    $_product = $order->get_product_from_item( $item );
    
                    if ( $_product && $_product->exists() && $_product->managing_stock() ) {
    
                        $old_stock = $_product->stock;
    
                        $qty = apply_filters( 'woocommerce_order_item_quantity', $item['qty'], $this, $item );
    
                        $new_quantity = $_product->increase_stock( $qty );
    
                        do_action( 'woocommerce_auto_stock_restored', $_product, $item );
    
                        $order->add_order_note( sprintf( __( 'Item #%s stock incremented from %s to %s.', 'woocommerce' ), $item['product_id'], $old_stock, $new_quantity) );
    
                        $order->send_stock_notifications( $_product, $new_quantity, $item['qty'] );
                    }
                }
            }
        } //End of function
    

    YOUR_ACTION_NAME 替换为您自己的函数名称。

    【讨论】:

      【解决方案2】:

      这是我的代码:

      /**
       * WooCommerce - do a custom action on order status change
       */
      function jasom_custom_actions_on_order_status_change( $order_id, $checkout = null ) {
      
          $order = new WC_Order( $order_id );
      
          if (!get_option( 'woocommerce_manage_stock' ) == 'yes' && !sizeof( $order->get_items() ) > 0) {
              return;
          }
      
          if ($order->get_status() === 'refunded') {
      
              $changes = [];
      
              foreach ($order->get_items() as $item) {
      
                  if ($item[ 'product_id' ] > 0) {
      
                      $product = $item->get_product();
      
                      if ($product && $product->exists() && $product->managing_stock()) {
      
                          $qty = $item->get_quantity(); // Get the item quantity
      
                          $new_quantity = $product->increase_stock( $qty );
      
                          do_action( 'woocommerce_auto_stock_restored', $product, $item );
      
                          $changes[] = $product->get_formatted_name() . ' ' . ( $new_quantity - $qty ) . '→' . $new_quantity;
      
                          $order->send_stock_notifications( $product, $new_quantity, $item[ 'qty' ] );
                      }
                  }
              }
      
              if ($changes) {
                  $order->add_order_note( __( 'Stock levels increased:', 'woocommerce' ) . ' ' . implode( ', ', $changes ) );
              }
          }
      
      }
      
      add_action( 'woocommerce_order_status_changed', 'jasom_custom_actions_on_order_status_change' );
      

      【讨论】:

        猜你喜欢
        • 2018-02-03
        • 2018-01-10
        • 2015-10-07
        • 1970-01-01
        • 2021-04-21
        • 1970-01-01
        • 2017-06-10
        • 2020-08-17
        • 2016-07-14
        相关资源
        最近更新 更多