wordpress 限制某些产品只运送到特定的邮政编码/邮编在WooCommerce

jdzmm42g  于 5个月前  发布在  WordPress
关注(0)|答案(1)|浏览(95)

在WooCommerce,有没有插件,我可以限制一些产品可以在一些特定的邮政编码/PIN码交付.这意味着,假设我有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的航运或支付网关。

wwtsj6pe

wwtsj6pe1#

您的代码已经过时,不适应,并有一些错误。请尝试以下操作:

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

字符串
代码放在你的子主题的functions.php文件中(或插件中)。测试和工作。

相关问题