gpt4 book ai didi

php - WooCommerce 存款 : Keep only specific shipping methods for deposits

转载 作者:行者123 更新时间:2023-12-04 07:57:20 26 4
gpt4 key购买 nike

我用 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">
我尝试将可用的隐藏运输方式代码片段与 _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;
}

最佳答案

以下代码将仅保留存款的本地取件运输方式(对于 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 文件中。它应该有效。

关于php - WooCommerce 存款 : Keep only specific shipping methods for deposits,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66636944/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com