Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 要求客户在特定状态下的最小变化量_Php_Wordpress_Woocommerce_Product_Variations - Fatal编程技术网

Php 要求客户在特定状态下的最小变化量

Php 要求客户在特定状态下的最小变化量,php,wordpress,woocommerce,product,variations,Php,Wordpress,Woocommerce,Product,Variations,在我们的Woocommerce商店,我们有两种不同的产品(一种软饮料),分别为330毫升和500毫升 330毫升的产品有以下变化: 一个六包 2个六包 三个六包 四六包 1箱(24瓶) 500毫升的产品有以下变化: 一个六包 2个六包 三个六包 四六包 1箱(20瓶) 我们只销售给: 西澳大利亚州 南澳大利亚 北领地 我们已经成功地将销售限制在上述3个州,目前我们没有向维多利亚州、塔斯马尼亚州、新南威尔士州或昆士兰州销售(我们的重点是最西部的3个州) 我们如何要求南澳大利亚(SA)的

在我们的Woocommerce商店,我们有两种不同的产品(一种软饮料),分别为330毫升和500毫升

330毫升的产品有以下变化:

  • 一个六包
  • 2个六包
  • 三个六包
  • 四六包
  • 1箱(24瓶)
500毫升的产品有以下变化:

  • 一个六包
  • 2个六包
  • 三个六包
  • 四六包
  • 1箱(20瓶)
我们只销售给:

  • 西澳大利亚州
  • 南澳大利亚
  • 北领地
我们已经成功地将销售限制在上述3个州,目前我们没有向维多利亚州、塔斯马尼亚州、新南威尔士州或昆士兰州销售(我们的重点是最西部的3个州)

我们如何要求南澳大利亚(SA)的客户至少购买:

  • 4六包300毫升产品
  • 或者一盒300毫升的产品
  • 或4 6包500毫升的产品
  • 或一盒500毫升的产品
任何帮助都将不胜感激


谢谢

这里有一个自定义函数,挂接在
woocommerce\u add\u to\u cart\u validation
过滤器钩子中,它将完全满足您的期望

但是您需要按照要求在函数内的代码开头正确设置数组中的数据。如果您的条件不匹配,产品将不会添加到购物车,并且(可选)将显示自定义消息

这是代码:

 add_filter( 'woocommerce_add_to_cart_validation', 'addtocart_postcode_validation', 10, 5 );
 function addtocart_postcode_validation( $passed, $product_id, $quantity, $variation_id, $variations ) {

    // ==> Define HERE your array of targetted variations SLUGs values (NOT the Names)
    $variation_slugs = array('4-six-packs', '1-carton-(20-bottles)', '1-carton-(24 bottles)' );

    // ==> Define HERE your array of restricted states (coma separated)
    $restricted_states = array('South Australia');

    if ( is_user_logged_in() ){

        // Getting current user ID
        $user_id = get_current_user_id();

        // Getting current user address state
        $user_state = get_user_meta($user_id, 'billing_state', true);

        // targeting users from South Australia
        if( in_array( $user_state, $restricted_states ) ){
            foreach( $variations as $variation ){
                // if the variation value is corresponding to a value of our arrays => true (OK)
                if( in_array( $variation, $variation_slugs ) ){
                    $passed = true; // OK
                } else {
                    $passed = false; // NOT OK
                    // Stops the loop
                    break;
                }
            }
        } else {
            $passed = true; // OK for other states
        }

        // (optionally) Displaying an alert message only for targetted state and if is not corresponding to targetted variations slugs
        if( !$passed )
            wc_add_notice( __( 'Sorry, you are not allowed to add this product with the chosen packaging, please chose another allowed packaging.', 'woocommerce' ), 'error' );


    } else { // for non logged user — NOT OK
        wc_add_notice( __( 'To add this product to cart, you need to be logged in.', 'woocommerce' ), 'error' );
        $passed = false;
    }

    return $passed;
}
add_action('woocommerce_before_cart_table','output_cart_raw_data');
function output_cart_raw_data(){
    if ( !WC()->cart->is_empty() && is_user_logged_in() ){
        $user_id = get_current_user_id();
        $user_state = get_user_meta($user_id, 'billing_state', true);

        // Displaying the current user 'billing_state':
        echo '<br><div style="border:solid 2px #333; padding: 10px;"><p><b>User State:</b> <span style="font-style:italic; color: green;">'.$user_state.'</span></p>';

        // Iterating through each cart items
        $count = 1;
        foreach(WC()->cart->get_cart() as $cart_item){
            if($cart_item['variation_id'] > 0){
                echo "<p><b>Cart Item variable $count</b><br>";
                foreach( $cart_item['variation'] as $var_key => $var_value ){
                    echo '<span>Attribute — Key: <span style="font-style:italic; color: green;">'.$var_key.'</span> => Value: <span style="font-style:italic; color: green;">'.$var_value.'</span></span><br>';
                }
                echo '</p>'; $count++;
            }
        }
        echo '</div>';
    }
}
代码位于活动子主题(或主题)的function.php文件或任何插件文件中

或者,当提交了错误的变体并且客户未登录时(在产品添加到购物车提交时),您可以显示自定义消息

这段代码经过测试,可以正常工作


如果您不确定必须在数组中设置的数据,您可以在之前使用这个小功能,它将在购物车页面上输出

  • 当前用户状态
  • 添加到购物车中的物品的变化量
这是代码:

 add_filter( 'woocommerce_add_to_cart_validation', 'addtocart_postcode_validation', 10, 5 );
 function addtocart_postcode_validation( $passed, $product_id, $quantity, $variation_id, $variations ) {

    // ==> Define HERE your array of targetted variations SLUGs values (NOT the Names)
    $variation_slugs = array('4-six-packs', '1-carton-(20-bottles)', '1-carton-(24 bottles)' );

    // ==> Define HERE your array of restricted states (coma separated)
    $restricted_states = array('South Australia');

    if ( is_user_logged_in() ){

        // Getting current user ID
        $user_id = get_current_user_id();

        // Getting current user address state
        $user_state = get_user_meta($user_id, 'billing_state', true);

        // targeting users from South Australia
        if( in_array( $user_state, $restricted_states ) ){
            foreach( $variations as $variation ){
                // if the variation value is corresponding to a value of our arrays => true (OK)
                if( in_array( $variation, $variation_slugs ) ){
                    $passed = true; // OK
                } else {
                    $passed = false; // NOT OK
                    // Stops the loop
                    break;
                }
            }
        } else {
            $passed = true; // OK for other states
        }

        // (optionally) Displaying an alert message only for targetted state and if is not corresponding to targetted variations slugs
        if( !$passed )
            wc_add_notice( __( 'Sorry, you are not allowed to add this product with the chosen packaging, please chose another allowed packaging.', 'woocommerce' ), 'error' );


    } else { // for non logged user — NOT OK
        wc_add_notice( __( 'To add this product to cart, you need to be logged in.', 'woocommerce' ), 'error' );
        $passed = false;
    }

    return $passed;
}
add_action('woocommerce_before_cart_table','output_cart_raw_data');
function output_cart_raw_data(){
    if ( !WC()->cart->is_empty() && is_user_logged_in() ){
        $user_id = get_current_user_id();
        $user_state = get_user_meta($user_id, 'billing_state', true);

        // Displaying the current user 'billing_state':
        echo '<br><div style="border:solid 2px #333; padding: 10px;"><p><b>User State:</b> <span style="font-style:italic; color: green;">'.$user_state.'</span></p>';

        // Iterating through each cart items
        $count = 1;
        foreach(WC()->cart->get_cart() as $cart_item){
            if($cart_item['variation_id'] > 0){
                echo "<p><b>Cart Item variable $count</b><br>";
                foreach( $cart_item['variation'] as $var_key => $var_value ){
                    echo '<span>Attribute — Key: <span style="font-style:italic; color: green;">'.$var_key.'</span> => Value: <span style="font-style:italic; color: green;">'.$var_value.'</span></span><br>';
                }
                echo '</p>'; $count++;
            }
        }
        echo '</div>';
    }
}
add_action('woocommerce_在_cart_table'之前,'output_cart_raw_data');
函数输出\购物车\原始\数据(){
如果(!WC()->cart->is_empty()&is_user_logged_in()){
$user\u id=get\u current\u user\u id();
$user\u state=get\u user\u meta($user\u id,'billing\u state',true);
//显示当前用户的“账单状态”:
回显'
用户状态:'.$User\u State.'

'; //遍历每个购物车项目 $count=1; foreach(WC()->cart->get_cart()作为$cart_项目){ 如果($cart_item['variation_id']>0){ echo“购物车项目变量$count
”; foreach($cart_item['variation']作为$var_key=>$var_value){ echo“Attribute-Key:”.$var_Key.=>Value:“.$var_Value.
”; } 回显“

”;$count++; } } 回声'; } }
此代码位于活动子主题(或主题)的function.php文件中