在WooCommerce 3

时间:2017-09-27 12:55:45

标签: php wordpress woocommerce cart product

在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 );
        }
    }
}

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

2 个答案:

答案 0 :(得分:2)

您必须创建一个包含所有目标产品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 );
            }
        }
    }
}

答案 1 :(得分:1)

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

您将能够以这种方式使其适用于多个已定义的产品ID (在数组中)

data.GetParam(1)

代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中。

此代码经过测试,适用于WooCommerce 3 +