【问题标题】:Get product id on checkout process in WooCommerce [duplicate]在 WooCommerce 中的结帐过程中获取产品 ID [重复]
【发布时间】:2019-06-20 14:52:16
【问题描述】:

结帐时,我需要阻止特定产品(不是所有产品)在某些城市的配送。当用户根据我定义的列表填写应阻止的城市时,结帐过程会阻止订单,并且自定义通知错误会根据需要完美显示。

我使用此代码阻止某些城市的送货:

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

我的问题是。如何仅针对特定产品执行此操作?

【问题讨论】:

    标签: php wordpress woocommerce hook-woocommerce


    【解决方案1】:

    我认为它现在可以工作了,我从购物车中获取产品 ID

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

    我曾经检查产品是否存在于数组中,

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

    }

    【讨论】:

      猜你喜欢
      • 2020-12-09
      • 1970-01-01
      • 2018-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-11
      • 1970-01-01
      相关资源
      最近更新 更多