如何更改价格取决于php中woocommerce上购买的产品和客户?

时间:2019-05-24 09:18:49

标签: php wordpress woocommerce

我想在php中编写一个函数,该函数根据客户购买的商品数量来更改WooCommerce中所有产品的价格。

例如:客户以80欧元购买一件商品。现在,所有其他商品的价格为50欧元,否为80欧元。他现在以50欧元的价格购买了第二个商品,其他所有物品的价格为25欧元。

我尝试过插件,但没有人能如我所愿。我是php的初学者,所以我写了一些php代码段,但我不太了解该怎么做。


function recalculate_price30( $cart_object) {
  if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    foreach ( $cart_object->get_cart() as $hash => $value ) {
            // I want to make discounts only for products in category with ID ***/25 → Exclusivité
            if( !in_array( 25, $value['data']->get_category_ids() )) {

                $newprice = $value['data']->get_regular_price() - 30;

                // in case the product is already on sale and it is much cheeper with its own discount
                if( $value['data']->get_sale_price() > $newprice )
                    $value['data']->set_price( $newprice );
            }
    }
}

function recalculate_price55( $cart_object) {
  if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    foreach ( $cart_object->get_cart() as $hash => $value ) {
            // I want to make discounts only for products in category with ID ***/25 → Exclusivité
            if( !in_array( 25, $value['data']->get_category_ids() )) {

                $newprice = $value['data']->get_regular_price() - 55;

                // in case the product is already on sale and it is much cheeper with its own discount
                if( $value['data']->get_sale_price() > $newprice )
                    $value['data']->set_price( $newprice );
            }
    }
}

function recalculate_price70( $cart_object) {
  if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    foreach ( $cart_object->get_cart() as $hash => $value ) {
            // I want to make discounts only for products in category with ID ***/25 → Exclusivité
            if( !in_array( 25, $value['data']->get_category_ids() )) {

                $newprice = $value['data']->get_regular_price() - 70;

                // in case the product is already on sale and it is much cheeper with its own discount
                if( $value['data']->get_sale_price() > $newprice )
                    $value['data']->set_price( $newprice );
            }
    }
}


function number_product_purchased() {

    global $product, $woocommerce, $woocommerce_loop;

    //Get user
    $current_user = wp_get_current_user();

    //Get user order (Completed + Processing)  + AJOUTER ARTICLES PANIER
    $customer_orders = get_posts( array(
        'numberposts' => -1,
        'meta_key'    => '_customer_user',
        'meta_value'  => $current_user->ID,
        'post_type'   => wc_get_order_types(),
        'post_status' => array_keys( wc_get_is_paid_statuses() ),
    ) );

    //parcourir les commandes pour obtenir le nombre d'articles
    if ( ! $customer_orders ) return;
    $product_number = 0;
    foreach ( $customer_orders as $customer_order ) {
        $order = wc_get_order( $customer_order->ID );
        $items = $order->get_items();
        foreach ( $items as $item ) {
            $product_number = $product_number + 1;
        }
      }
      $cartcount = WC()->cart->get_cart_contents_count();
      $product_number= $product_number + $cartcount;

        //changer affichage des prix
        switch ($product_number){
          case 0:
            break;
          case 1:

            break;
          case 2:

            break;
          default:

            break;

          }
}

add_action('woocommerce_before_calculate_totals', 'number_product_purchased');

我只想使用一次功能,但是怎么用?我可以在切换情况下使用add_action吗?谢谢你们

0 个答案:

没有答案