在WooCommerce中,没有插件可以限制某些产品以特定的邮政编码/个人识别码交付.这意味着,假设我的store 里有100种产品.其中,10种产品将只需1个邮政编码即可送达.其余产品将运往全国各地.

我对编码很陌生.请指导我们如何在functions.php中做到这一点.

add_filter( 'woocommerce_available_payment_gateways' , 'hide_payment_gateway', 20, 1);
function hide_payment_gateway( $gateways ){
    //change whatever amount you want
    if( WC()->checkout->zipcode ≠ 713102 ){
        // then unset the 'cod' key (cod is the unique id of COD Gateway)
        unset( $gateways['cod'] );
        add_action( 'woocommerce_review_order_before_payment', 'COD_exceed_amount_before_paying_notice' );
    }
    return $gateways;
}

function COD_exceed_amount_before_paying_notice() {
    wc_print_notice( __( 'delivery not available', 'woocommerce' ), 'notice' );
}

我在这段代码中发现了一些错误.我需要隐藏某些产品的某些产品ID的运输或支付网关.

推荐答案

你的代码过时了,没有改编,而且有一些错误.请try 以下操作:

// Trigger "update checkout" on postcode change
add_action( 'woocommerce_checkout_init', 'postcode_field_update_checkout_js' );
function postcode_field_update_checkout_js() {
    wc_enqueue_js( "
        // On postcode change
        $('form.woocommerce-checkout').on('change', '#billing_postcode, #shipping_postcode', function() {
            $(document.body).trigger('update_checkout')
        });
    ");
}

add_action( 'woocommerce_calculate_totals', 'check_cart_items_for_customer_postcode' );
function check_cart_items_for_customer_postcode( $cart ) {
    $targeted_postcodes = array('713102'); // The allowed postcodes
    $targeted_product_ids = array( 15, 16, 17, 18, 25); // The product Ids for that postcode
    $product_names = []; // Initializing

    // Get customer postcode
    $postcode = WC()->customer->get_shipping_postcode();
    $postcode = empty($postcode) ? WC()->customer->get_billing_postcode() : $postcode;
    $postcode_match = in_array( $postcode, $targeted_postcodes );

    // Exit if postcode is not yet defined or if it match
    if ( empty($postcode) || in_array( $postcode, $targeted_postcodes ) ) return; 

    // Loop through cart items to collect targeted products
    foreach($cart->get_cart() as $item ) {
        if( in_array( $item['product_id'], $targeted_product_ids ) ) { 
            $product_names[] = $item['data']->get_name(); // Add the product name
        }
    }

    if( count($product_names) > 0 ) {
        if ( is_cart() ) {
            remove_action( 'woocommerce_proceed_to_checkout', 'woocommerce_button_proceed_to_checkout', 20 );
        } elseif ( is_checkout() && ! is_wc_endpoint_url() ) {
            add_filter('woocommerce_order_button_html', 'disabled_place_order_button_html');
        }
        wc_clear_notices(); // Clear other existing notices
        // Avoid checkout displaying an error notice
        wc_add_notice( sprintf( __('The products %s can not be shipped to %s postcode.', 'woocommerce'), 
            implode(', ', $product_names), $postcode ), 'error' );
    }
}

function disabled_place_order_button_html() {
    $order_button_text = __('Place order', 'woocommerce');
    echo '<button type="button" class="button alt disabled" id="place_order_disabled">' . $order_button_text . '</button>';
}

代码位于您的子主题的unctions.php文件中(或在插件中).经过测试,效果良好.

Php相关问答推荐

通过激活模板在数据库中创建表

我需要多个SELECT语句的结果(使用由php ECHO创建的表中显示的Limit(X)

在特定日期之间使用特定的元密钥销售产品

WooCommerce/WordPress:简单的产品属性显示

如何显示每个分类的当前术语的帖子中包含的前20个标签,不包括标签分类

创建一个新的可变产品及其属性以用于WooCommerce中的变体

WooCommerce HPOS管理订单列表中带有订单元数据的定制列

通过访问单个产品页面更新WooCommerce产品元数据

目标类[Set_Locale]不存在.拉威尔

根据WooCommerce中的客户计费国家/地区更改产品价格

Chillerlan/php-iOS名称字段的QR vCard错误

如何在laravel中获得两个时间戳之间的小时和分钟差异

WooCommerce:如果订单总数为零,则隐藏本地取货运输选项

如何更新 Laravel Eloquent 列强制转换为集合

如何显示所有数组数据而不仅仅是最后的结果

无法读取某些 WordPress 主机上的 cookie

安装 Herd 并返回 Valet 后为 502

后退按钮不起作用 laravel ajax crud

如何通过工厂为点列制作一个假坐标?

如何使用在产品快速编辑菜单前端的自定义框中 Select 的值更新 woocommerce 产品属性值?