要求特定州

时间:2017-02-13 03:58:06

标签: php wordpress woocommerce product variations

在我们的Woocommerce商店,我们有3种可变产品(软饮料),分别为330毫升和500毫升。

330毫升产品有各种变化:

  • 1六包
  • 2六包
  • 3六包
  • 4六包
  • 1个纸箱(24瓶)

500毫升产品有各种变化:

  • 1六包
  • 2六包
  • 3六包
  • 4六包
  • 1个纸箱(20瓶)

我们只卖给:

  • 西澳大利亚州
  • 南澳大利亚
  • 北领地

我们成功地限制了上述3个州的销售,目前我们还没有向维多利亚州,塔斯马尼亚州,新南威尔士州或昆士兰州销售(我们重点关注最西部的3个州)

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

  • 4包600毫升300毫升产品
  • 或300毫升产品的纸箱
  • 或4个六包500毫升产品
  • 或1箱500毫升产品

任何帮助将不胜感激。

由于

1 个答案:

答案 0 :(得分:1)

这是一个隐藏在 woocommerce_add_to_cart_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;
}

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

  

您可以选择在提交错误变体时以及客户未登录时显示自定义消息(在产品添加到购物车提交时)...

此代码经过测试并有效......

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

  • 当前用户状态,
  • 商品中添加商品 ...
  • 的变化slu ..

以下是代码:

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文件中...