代码之家  ›  专栏  ›  技术社区  ›  Alex

在WooCommerce的后端创建订单时,设置客户“免征增值税”

  •  0
  • Alex  · 技术社区  · 8 年前

    我想在woocommerce中添加一个操作,当在后端创建订单时,为作为客户的特殊用户删除订单中的税款。

    此代码适用于网站上的正常订单流程,但不适用于后端

    add_action( 'woocommerce_checkout_update_order_review', 'remove_tax_from_user' );
    add_action( 'woocommerce_before_cart_contents', 'remove_tax_from_user' );
    
    function remove_tax_from_user( $post_data ) {
      global $woocommerce;
      $username = $woocommerce->customer->get_username();
      $user = get_user_by('login',$username);
      if($user)
      { 
        if( get_field('steuer_befreit', "user_{$user->ID}") ):
        $woocommerce->customer->set_is_vat_exempt( true );
        endif;
      }
    }
    
    1 回复  |  直到 8 年前
        1
  •  1
  •   LoicTheAztec    8 年前

    在后端,客户需要在单击“添加订单”之前“免征增值税”。

    你可以试着用钩子 save_post_shop_order 在订单数据保存到后端之前触发,方式如下:

    add_action( 'save_post_shop_order', 'backend_remove_tax_from_user', 50, 3 );
    function backend_remove_tax_from_user( $post_id, $post, $update ) {
    
        if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
            return $post_id; // Exit if it's an autosave
    
        if ( $post->post_status != 'publish' )
            return $post_id; // Exit if not 'publish' post status
    
        if ( ! current_user_can( 'edit_order', $post_id ) )
            return $post_id; // Exit if user is not allowed
    
        if( ! isset($_POST['customer_user']) ) return $post_id; // Exit
    
        if( $_POST['customer_user'] > 0 ){
            $customer_id = intval($_POST['customer_user']);
            if( get_field('steuer_befreit', "user_{$customer_id}") ){
                $wc_customer = new WC_Customer( $customer_id );
                $wc_customer->set_is_vat_exempt( true );
            }
        }
    }
    

    代码进入功能。活动子主题(或主题)的php文件。

    但这行不通 你将无法用任何现有的钩子来实现这一点。唯一的方法是让第一个客户免除增值税,然后您可以为该客户添加订单。

    推荐文章