dougao9864 2019-01-27 11:53
浏览 45

在WooCommerce结帐流程中获取产品ID [重复]

This question already has an answer here:

At checkout I need to block delivery in certain cities for a specific product (not all products). When user fills in a city that should be blocked based on a list that I defined, the checkout process blocks the order and a custom notice error appear perfectly as desired.

I use this code to block delivery in certain city:

add_action( 'woocommerce_checkout_process', 'shipping_validate_city' );
function shipping_validate_city() {
    if ( in_array( $product_id, array( 3059, 3058) ) ) {
        $disableCityList = array (
            'Rabat',
            'Temara',
            'Sale',
            'Tamessna',
        );
        $billingCity = isset( $_POST['billing_city'] ) ? trim( $_POST['billing_city'] ) : '';
        $billingCity = str_replace(array('-','_'),' ',$billingCity);
        $billingCity = ucwords($billingCity);

        if (in_array($billingCity, $disableCityList))
        {
            wc_add_notice( __('this product not allowed shipping for the city you mentioned') , 'error' );
        }
    }
}

My problem is. How can I do this for specific product only?

</div>
  • 写回答

1条回答 默认 最新

  • dtttlua7165 2019-01-27 13:49
    关注

    I think it work now, i fetch product id from cart items

    foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
        $pdt_id = $cart_item['product_id'];
    }
    

    and i used to check if the product exist in array,

    add_action( 'woocommerce_checkout_process', 'shipping_validate_city' );
    
    function shipping_validate_city() {
    $pdt_id= array();
    foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
        $pdt_id = $cart_item['product_id'];
    }
    if ( in_array( $pdt_id , array( 3059, 3058) ) ) {
        $enableCityList = array (
        'Rabat',
        'Temara',
        'Sale',
        'Tamessna',
        'Tamesna',
        'RABAT',
        'TEMARA',
        'SALE',
        'TAMESSNA',
        'TAMESNA',
        'Salé',
        );
        $billingCity = isset( $_POST['billing_city'] ) ? trim( $_POST['billing_city'] ) : '';
        $billingCity = str_replace(array('-','_'),' ',$billingCity);
        $billingCity = ucwords($billingCity);
    
        if (!in_array($billingCity, $enableCityList))
        {
        wc_add_notice( __('Livraison à ville que vous avez indiquée non couverts pour ce produit ') , 'error' );
        }
    }
    

    }

    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)