gpt4 book ai didi

php - 如果特定产品在 WooCommerce 购物车中,则强制使用 "Ship to different address"

转载 作者:行者123 更新时间:2023-12-04 07:58:44 25 4
gpt4 key购买 nike

关闭。这个问题需要debugging details .它目前不接受答案。












想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。

6 个月前关闭。




Improve this question




当特定产品在结帐时,我正在尝试添加一个剪辑以激活默认情况下打开的“运送到其他地址”。
我找到了 Enable 'Ship to a different address' check box for specific products in Woocommerce相关的答案线程运行良好!
但是,不能取消选中“运送到其他地址”复选框。如果特定产品在结帐中,则需要提供不同的送货地址。
目标:将这些特定产品的“运送到其他地址”复选框灰显。
我刚换了in the code $products_ids = array(10800, 11907);满足我的需求。
我已经尝试解开该函数,但整个代码不起作用。因此,我正在寻找使该复选框变灰但保持上面代码完全正常工作的最佳方法。

最佳答案

如果我理解,您想保留代码功能,禁用复选框。
最好的办法是先隐藏复选框。然后避免显示或隐藏收货地址通过单击复选框标签,jQuery 将在选中时将其删除。
php 代码(和内联 CSS):

// Hide the checkbox (inline CSS) - Only on checkout page
add_filter( 'wp_head', 'shipping_address_checkbox_ccs' );
function shipping_address_checkbox_ccs() {
if( is_checkout() && ! is_wc_endpoint_url() ) {
?><style>body.checkout-hide-checkbox #ship-to-different-address-checkbox { display:none; } body.checkout-hide-checkbox #ship-to-different-address label { cursor: default; }</style><?php
}
}

// Conditional function
function mandatory_shipping_address(){
$products_ids = array(10800, 11907);
$found = $others_found = false;

foreach ( WC()->cart->get_cart() as $cart_item ){
if ( in_array( $cart_item['data']->get_id(), $products_ids ) ){
$found = true;
} else {
$others_found = true;
}
}
return $found && ! $others_found ? true : false;
}

// Conditionally Enable checkbox (checked)
add_filter( 'woocommerce_ship_to_different_address_checked', 'filter_cart_needs_shipping_address');
function filter_cart_needs_shipping_address( $checked ) {
return mandatory_shipping_address() ? true : $checked;
}

// Conditionally Add a body class - Only on checkout page
add_filter( 'body_class', 'shipping_address_checkbox_body_class' );
function shipping_address_checkbox_body_class( $classes ) {
if( is_checkout() && ! is_wc_endpoint_url() && mandatory_shipping_address() ) {
$classes[] = 'checkout-hide-checkbox';
}
return $classes;
}
jQuery 代码:
// Remove the checkbox when is checked - Only on checkout page
add_action( 'template_redirect', 'cart_needs_shipping_address_js');
function cart_needs_shipping_address_js() {
if( is_checkout() && ! is_wc_endpoint_url() ) :

wc_enqueue_js( "jQuery( function($){
var a = '#ship-to-different-address-checkbox';
if( $(a).is(':checked') ) {
$(a).remove(); // Remove checkbox
}
});");
endif;
}
代码位于事件子主题(或事件主题)的 functions.php 文件中。测试和工作。
所以检查复选框时,您将获得(适用于您的定义产品):
enter image description here

关于php - 如果特定产品在 WooCommerce 购物车中,则强制使用 "Ship to different address",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66573320/

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