【问题标题】:Conditionally set cart item prices for specific products in WooCommerce 3在 WooCommerce 3 中为特定产品有条件地设置购物车商品价格
【发布时间】:2018-03-08 23:27:00
【问题描述】:

在 WooCommerce 中,我使用了这个答案中的一些代码:
Set WooCommerce cart item price to zero if the product has already been bought

它适用于一种产品,但我想让它适用于多种产品

所以下面的代码,对于特定产品,如果客户已经购买,则将购物车项目价格更改为 $0.00,否则将价格设置为 $100(对于第一次购买)

    add_action( 'woocommerce_before_calculate_totals', 'conditionally_change_cart_items_price', 10, 1 );
function conditionally_change_cart_items_price( $cart_object ) {

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;


    $targeted_product_id = 1107;

    // Set Here your custom price (1st purshase)
    $custom_price = 100; // First purshase for product ID 1092

    // Detecting if customer has already bought The targeted product (1092)
    if( is_user_logged_in() ){
        $customer      = wp_get_current_user();
        $customer_id   = $customer->ID; // customer ID
        $customer_email = $customer->email; // customer email

        if( wc_customer_bought_product( $customer_email, $customer_id, $targeted_product_id) )
            $custom_price = 0; // Set to 0 for other purchases (product ID 1092)
    }

    foreach ( $cart_object->get_cart() as $cart_item ) {
        // When targeted product is in cart we change the price
        if ( $cart_item['product_id'] == $targeted_product_id ) {
            // Woocommerce 3+ compatibility
            if ( version_compare( WC_VERSION, '3.0', '<' ) )
                $cart_item['data']->price = $custom_price;
            else
                $cart_item['data']->set_price( $custom_price );
        }
    }
}

我怎样才能使它适用于多种定义的产品(而不是实际的一种)?

【问题讨论】:

    标签: php wordpress woocommerce cart product


    【解决方案1】:

    您必须创建一个包含所有目标产品 ID 的数组,然后您应该检查客户是否已经在 foreach 循环中购买了该产品:

    add_action( 'woocommerce_before_calculate_totals', 'conditionally_change_cart_items_price', 10, 1 );
    
    function conditionally_change_cart_items_price( $cart_object ) {
        if ( is_admin() && ! defined( 'DOING_AJAX' ) )
            return;
    
        // add as many ids as you need
        $targeted_product_ids = array( 1107, 1108, 1109 );
    
        // Set Here your custom price (1st purshase)
        $custom_price = 100; // First purshase for target product
    
    
        if ( is_user_logged_in() ) {
            $customer       = wp_get_current_user();
            $customer_id    = $customer->ID; // customer ID
            $customer_email = $customer->email; // customer email
    
            foreach ( $cart_object->get_cart() as $cart_item ) {
                // When targeted product is in cart we change the price
                if ( in_array( $cart_item['product_id'], $targeted_product_ids ) ) {
    
                    // Detecting if customer has already bought The targeted product
                    if ( wc_customer_bought_product( $customer_email, $customer_id, $targeted_product_id ) )
                        // Set to 0 for other purchases
                        $custom_price = 0;
    
                    // Woocommerce 3+ compatibility
                    if ( version_compare( WC_VERSION, '3.0', '<' ) )
                        $cart_item['data']->price = $custom_price;
                    else
                        $cart_item['data']->set_price( $custom_price );
                }
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      基于:Set Woocommerce cart item price to zero if the product has already been bought

      您可以通过这种方式使其适用于多个已定义的产品 ID(在一个数组中)

      // For WooCommerce version 3 and above (only)
      add_action( 'woocommerce_before_calculate_totals', 'conditionally_change_cart_items_prices', 10, 1 );
      function conditionally_change_cart_items_prices( $cart ) {
          if ( is_admin() && ! defined( 'DOING_AJAX' ) ) 
              return;
      
          if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
              return;
      
          // Set HERE the Products to check in this array
          $targeted_product_ids = array( 37, 50, 89, 124, 327 );
          $products_bought = array();
      
          // Set Here your custom price (1st purshase)
          $custom_price1 = 100; // First purshase
          // Set Here your custom price (more purshases than first)
          $custom_price2 = 0; // Next purshases
      
          // Detecting if customer has already bought our targeted products
          if( is_user_logged_in() ){
              $customer      = wp_get_current_user();
              $customer_id   = $customer->ID; // customer ID
              $customer_email = $customer->email; // customer email
      
              // Checking each product in the array
              foreach( $targeted_product_ids as $product_id ){
                  if( wc_customer_bought_product( $customer_email, $customer_id, $product_id) ){
                      // We set all (targeted) purchased products in an array
                      $products_purchased[] = $product_id;
                  }
              }
          }
      
          // Checking cart items and changing item prices if needed
          foreach ( $cart->get_cart() as $cart_item ) {
              // When a targeted products already purchased is in cart we set price 2
              if ( in_array( $cart_item['product_id'], $products_purchased ) ) {
                  $cart_item['data']->set_price( $custom_price2 );
              } 
              // When a targeted products is in cart and has not been purchased we set price 1
              elseif ( in_array( $cart_item['product_id'], $targeted_product_ids ) ) {
                  $cart_item['data']->set_price( $custom_price1 );
              }
          }
      }
      

      代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件中。

      此代码经过测试,可在 WooCommerce 3+ 中运行

      【讨论】:

      • 如果我从特定类别中选择 5 件产品,购物车中的所有产品都会收取 100 美元的费用。我想将第一个订单的价格限制在 100 美元,而不是 500 美元,然后下一个订单的 0.00 美元。
      • @MujtabaK 1) 您没有要求产品类别,而是产品 ID 数组... 2) 无法检查产品类别... 3) 您的问题不够清楚因为你可以有许多不同数量的产品,所以你必须在一些明确的例子中解释所有案例的价格。
      猜你喜欢
      • 2018-11-07
      • 2021-07-22
      • 2017-12-10
      • 1970-01-01
      • 2018-07-31
      • 2018-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多