Php WooCommerce存款:只保留存款的特定运输方式

Php WooCommerce存款:只保留存款的特定运输方式,php,wordpress,woocommerce,plugins,shipping-method,Php,Wordpress,Woocommerce,Plugins,Shipping Method,我使用插件,并希望隐藏所有其他运输方式,除了“本地皮卡”运输方式,如果客户选择支付押金 客户可以在产品页面上选择是使用10%的押金支付,还是全额支付订单。发货方式固定费率用于发送订单(如果订单已全部支付),并且应隐藏,因为客户需要先在(离线)商店中通过本地提货支付剩余订单金额 产品页面上的选项: <input type="radio" name="wc_deposit_option" value="yes" id="wc

我使用插件,并希望隐藏所有其他运输方式,除了“本地皮卡”运输方式,如果客户选择支付押金

客户可以在产品页面上选择是使用10%的押金支付,还是全额支付订单。发货方式固定费率用于发送订单(如果订单已全部支付),并且应隐藏,因为客户需要先在(离线)商店中通过本地提货支付剩余订单金额

产品页面上的选项:

<input type="radio" name="wc_deposit_option" value="yes" id="wc-option-pay-deposit">
<input type="radio" name="wc_deposit_option" value="no" id="wc-option-pay-full" checked="checked">

以下代码仅保留存款的本地提货装运方式(对于商业存款):


代码进入活动子主题(或活动主题)的functions.php文件。它应该可以工作。

存款是客户可以选择的付款方式吗?客户选择使用押金支付的方式和地点?当购物车项目有押金或客户选择押金付款时,是否需要隐藏其他运输方式?你在使用插件吗?请尝试澄清你的问题,编辑它。谢谢,我已经编辑了这个问题。我希望现在清楚了。是的,清楚了,回答了。感谢您对以下答案的反馈。非常感谢您再次帮助我!我希望其他人也能使用该代码。
/**
     * Are deposits enabled for a specific product
     * @param  int $product_id
     * @return bool
     */
    public static function deposits_enabled( $product_id, $check_variations = true ) {
        $product = wc_get_product( $product_id );

        if ( ! $product || $product->is_type( array( 'grouped', 'external' ) ) ) {
            return false;
        }

        $setting = get_post_meta( $product_id, '_wc_deposit_enabled', true );

        if ( $check_variations && empty( $setting ) ) {
            $children = get_children( array(
                'post_parent' => $product_id,
                'post_type' => 'product_variation',
            ) );

            foreach ( $children as $child ) {
                $child_enabled = get_post_meta( $child->ID, '_wc_deposit_enabled', true );
                if ( $child_enabled ) {
                    $setting = $child_enabled;
                    break;
                }
            }
        }
        
        if ( empty( $setting ) ) {
            $setting = get_option( 'wc_deposits_default_enabled', 'no' );
        }

        if ( 'optional' === $setting || 'forced' === $setting ) {
            if ( 'plan' === self::get_deposit_type( $product_id ) && ! self::has_plans( $product_id ) ) {
                return false;
            }
            return true;
        }
        return false;
    }
add_filter( 'woocommerce_package_rates', 'only_local_pickup_for_deposits', 100, 2 );
function only_local_pickup_for_deposits( $rates, $package ) {
    $has_deposit = false;

    // Loop through cart items for the current shipping package
    foreach( $package['contents'] as $item ) {
        if ( isset($item['is_deposit']) && $item['is_deposit'] ) {
            $has_deposit = true;
            break; // Stop the loop
        }
    }

    // If deposit is enabled for a cart item
    if( $has_deposit ) {
        // Loop through shipping rates
        foreach ( $rates as $rate_key => $rate ) {
            // Remove all shipping methods except "Local pickup" 
            if ( 'local_pickup' !== $rate->method_id ) {
                unset($rates[$rate_key]);
            }
        }
    }
    return $rates;
}