【问题标题】:WooCommerce deposits: Keep only specific shipping methods for depositsWooCommerce 存款:仅保留存款的特定运输方式
【发布时间】:2021-06-12 15:47:02
【问题描述】:

我使用WooCommerce Deposits 插件,如果客户选择支付押金,我想隐藏除“本地取货”运输方式之外的所有其他运输方式。

客户可以在产品页面上选择是支付例如 10% 的定金还是支付订单的全部金额。发货方式 flat_rate 用于发送订单(如果已全额付款),应隐藏,因为客户需要先支付剩余订单金额并在(线下)商店本地取货。

产品页面上的选择:

<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">

我尝试将可用的隐藏运输方式代码 sn-ps 与 _wc_deposit_enabled 元数据结合起来,但我无法弄清楚。帮助将不胜感激。

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

【问题讨论】:

  • 存款是客户可以选择的支付方式吗?客户如何以及在哪里选择使用押金付款?当购物车物品有押金或客户选择使用押金付款时,是否需要隐藏其他运输方式?你在使用插件吗?请尝试澄清您的问题,编辑它。
  • 谢谢,我已经编辑了这个问题。我希望现在很清楚。
  • 是的,清楚并回答了。请对以下答案提供一些反馈。

标签: php wordpress woocommerce plugins shipping-method


【解决方案1】:

以下代码将仅保留本地取货运输方式的存款(对于 WooCommerce 存款)

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

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

【讨论】:

  • 非常感谢您再次帮助我!我希望其他人也可以使用该代码。
猜你喜欢
  • 2016-04-01
  • 2023-03-09
  • 2014-08-06
  • 2021-08-20
  • 2019-02-15
  • 2018-11-09
  • 1970-01-01
  • 2022-01-02
  • 2021-06-07
相关资源
最近更新 更多