我在我的 WordPress 网站上使用此代码来在选择特定产品变体时隐藏支付网关。然而,一旦我插入代码,WordPress 就会从后端显示一个严重错误。请帮助我如何在我的网站上使用此代码?
产品属性:需要个性化
变量:是|不
我想要实现的是,如果有人选择“是”,那么结帐页面上的 COD 选项将被禁用。或者,如果他们选择“否”,则只有鳕鱼选项可用。
产品页面网址:https://savvyandgroovy.com/product/prashant/ https://savvyandgroovy.com/product/prashant/
这是我目前正在使用的:
add_filter(‘woocommerce_available_payment_gateways’, ‘conditional_payment_gateways’, 10, 1);
function conditional_payment_gateways($available_gateways)
{
$in_cart=false;
foreach(WC()->cart->get_cart_contents() as $key=>$values)
{
// See if there is an attribute called ‘pa_size’ in the cart
// Replace with whatever attribute you want
if(array_key_exists(‘personalization-required’, (array)$values[‘data’]->get_attributes()))
{
foreach($values[‘data’]->get_attributes() as $attribute=>$variation) ;
// Replace ‘small’ with your value.
if($variation==‘Yes’) $in_cart=true; //edited
}
}
if($in_cart)
unset($available_gateways[‘cod’]); // unset ‘cod’
return $available_gateways;
}
您的代码工作正常,但由于您使用的引号而出现错误。我已更新如下:
add_filter('woocommerce_available_payment_gateways', 'conditional_payment_gateways', 10, 1);
function conditional_payment_gateways( $available_gateways ) {
$in_cart = false;
if ( !empty(WC()->cart) ) {
foreach ( WC()->cart->get_cart_contents() as $key => $values ) {
// See if there is an attribute called ‘pa_size’ in the cart
// Replace with whatever attribute you want
//echo '<pre>';
//print_r($values['data']->get_attributes());
//echo '</pre>';
if (array_key_exists('personalization-required', (array) $values['data']->get_attributes() ) ) {
foreach ($values['data']->get_attributes() as $attribute => $variation);
// Replace ‘small’ with your value.
// if ($variation == 'No') $in_cart = true; //edited
if ($variation == 'Yes') {
$in_cart = true; //edited
// unset($available_gateways['cod']); // unset ‘cod’
}
}
}
if ( $in_cart )
unset($available_gateways['cod']); // unset ‘cod’
}
return $available_gateways;
}
我想要实现的是,如果有人选择“否”变体,则结帐页面上的鳕鱼选项将被禁用。或者,如果他们选择“是”,则只有鳕鱼选项可用。
这不会在您的代码中反向工作,因为您已经编写了if ($variation == ‘Yes’) $in_cart = true; //edited
所以你也需要更新它。所以我也更新了上面的代码。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)