强制将特定的 WooCommerce 产品或产品变体单独出售

问题描述 投票:0回答:1

我有下面的代码,请注意强制特定的 WooCommerce 产品以单独的订单出售和用户 7uc1f3r。

我希望将另外两个产品添加到此代码中。第一个产品很简单,其他两个是可变的。这些产品具有重量变化,我只希望其中一种重量变化来触发脚本并防止将其他产品添加到购物车中。因此,代码将循环遍历产品 ID 数组,如下所示:

function filter_woocommerce_add_product_to_cart_array_validation( $passed, $product_id, $quantity, $variation_id, $variations ) {
    
    // Product id to bought alone 
    $product_id_alone_array = array("1562", "1619");
    // Product id of the variation to be bought alone 
    $variation_id_alone_array = array("1319", "1476");

    // Set variable
    $flag = false;

    // cycle through the array 'product_id_alone_array'
    foreach ($product_id_alone_array as $product_id_alone) {
        // If cart is NOT empty when a product is added
       if ( ! WC()->cart->is_empty() ) {
            // Generate a unique ID for the cart item
            $product_cart_id = WC()->cart->generate_cart_id( $product_id_alone );

            // Check if product is in the cart
            $in_cart = WC()->cart->find_product_in_cart( $product_cart_id );

            // If product is already in cart & product ID added is not equal to product ID alone
            if ( $in_cart && ( $product_id != $product_id_alone ) ) {
                $flag = true;
            // Product ID alone is NOT in cart & product ID added is equal to product ID alone
            } elseif( ! $in_cart && ( $product_id == $product_id_alone ) ) {
                $flag = true;
            }
        }

        global $product;
        // If the WC_product Object is not defined globally
        if ( ! is_a( $product, 'WC_Product' ) ) {
            $product = wc_get_product( $product_id );
        }
        $product_name = $product->get_name();

        // True
        if ( $flag ) {
            // Set error message
            wc_add_notice( sprintf(
                __( 'Product <b>%s</b> must be bought separately due to our shipping arrangements.', 'woocommerce' ),
                $product_name,
            ), 'error' );

            // Passed = false
            $passed = false;
        }

        return $passed;
    }

    // cycle through the array 'variation_id_alone_array'
    foreach ($variation_id_alone_array as $variation_id_alone) {
        
        //$variations = $variation_id_alone->get_available_variations();
        //$variations_id = wp_list_pluck( $variations, 'variation_id' );
        //print_r('Variations: '.$variations_id, TRUE);
        
        $_product = new WC_Product_Variation( $variation_id_alone );
        $variation_data = $_product->get_variation_attributes();
        //$variation_detail = woocommerce_get_formatted_variation( $variation_data, true );  // this will give all variation detail in one line
        $variation_detail = woocommerce_get_formatted_variation( $variation_data, false);  // this will give all variation detail one by one
        return $variation_detail; // $variation_detail will return string containing variation detail which can be used to print on website
        return $variation_data; // $variation_data will return only the data which can be used to store variation data
        
        // If cart is NOT empty when a variation is added
       if ( ! WC()->cart->is_empty() ) {
            // Generate a unique ID for the cart item
            $variation_cart_id = WC()->cart->generate_cart_id( $variation_id_alone );

            // Check if variation is in the cart
            $in_cart = WC()->cart->find_product_in_cart( $variation_cart_id );

            // If variation is already in cart & variation ID added is not equal to variation ID alone
            if ( $in_cart && ( $variation_id != $variation_id_alone ) ) {
                $flag = true;
            // Variation ID alone is NOT in cart & variation ID added is equal to variation ID alone
            } elseif( ! $in_cart && ( $variation_id == $variation_id_alone ) ) {
                $flag = true;
            }
        }

        global $product;
        // If the WC_product Object is not defined globally
        if ( ! is_a( $product, 'WC_Product' ) ) {
            $product = wc_get_product( $variation_id );
        }
        $product_name = $product->get_name();

        // True
        if ( $flag ) {
            // Set error message
            wc_add_notice( sprintf(
                __( 'Product <b>%s</b> must be bought separately due to our shipping arrangements.', 'woocommerce' ),
                $product_name,
            ), 'error' );

            // Passed = false
            $passed = false;
        }

        return $passed;
    }
}
add_filter( 'woocommerce_add_to_cart_validation_', 'filter_woocommerce_add_product_to_cart_array_validation', 10, 5 );
php wordpress validation woocommerce product-variations
1个回答
0
投票

存在一些错误、复杂性,代码确实可以简化和优化。

由于您的目标产品需要单独出售,购物车应始终为空,因此无需检查购物车中是否有这些产品。
无需将定义的产品 ID 和产品变体 IDS 分开。

尝试以下验证功能:

add_filter( 'woocommerce_add_to_cart_validation', 'filter_products_to_be_sold_separately', 10, 4 );
function filter_products_to_be_sold_separately( $passed, $product_id, $quantity, $variation_id ) {
    if ( WC()->cart->is_empty() ) {
        return $passed; // Exit if cart is empty
    }
    // HERE, define the targeted products OR product variations IDs to be sold separately
    $targeted_ids = array(1562, 1619, 1319, 1476);

    if ( ! array_intersect($targeted_ids, [$product_id, $variation_id]) ) {
        return $passed; // Exit if it's not a targeted product
    }
    // Get the product or product variation object
    $product = wc_get_product( $variation_id ?: $product_id ); 

    // Displays an error message
    wc_add_notice( sprintf(
        __( 'Product <b>%s</b> must be bought separately due to our shipping arrangements.', 'woocommerce' ),
        $product->get_formatted_name(),
    ), 'error' );

    return false;
}

代码位于子主题的functions.php 文件中(或插件中)。应该可以。

© www.soinside.com 2019 - 2024. All rights reserved.