根据Woocommerce结帐中的复选框自定义字段在项目名称下添加文本

问题描述 投票:1回答:1

我有一个自定义函数,检查是否选中了复选框,如果是,它会在价格旁边添加'with vat relief'。如果未选中,则会在价格旁边添加“inc vat”。这工作正常,我的代码是:

add_filter( 'woocommerce_get_price_html', 'conditional_price_suffix', 20, 2 );
function conditional_price_suffix( $price, $product ) {
   $isTaxRelefe = get_post_meta($product->id, 'disability_exemption', true);

   if ($isTaxRelefe == 'yes')
       $price .= ' ' . __('with vat relief');

    else $price .= ' ' . __('inc vat');

   return $price;
}

我现在需要做的是添加另一个针对结帐页面的功能,该功能表明如果选中该复选框,则在产品标题下方显示一些文字,但我正在努力。我最初的想法是编辑/ checkout / review-order,所以我添加了一个if else语句来输出产品标题旁边的东西。我补充说:

$isTaxRelefe = get_post_meta($product->id, 'disability_exemption', true);

if ($isTaxRelefe == 'yes') {
   $content .= 'VAT RELIEF AVAILABLE';    
}

但这没有任何作用,我尝试过各种变化,改变回声等等,但没有运气。我确定我只是写错了。任何人都可以建议吗?我不是很喜欢WordPress的功能,如果我只能写一个目标结帐页面,我不知道它如何确定输出你的位置。 if else声明似乎是明显的选择,但没有任何运气。

php wordpress woocommerce checkout custom-fields
1个回答
1
投票

您的代码有点过时,您应该使用$product->get_id(),因为您的第一个函数中的Woocommerce 3而不是$product->id函数中的get_post_meta()

您也可以直接使用产品对象中的WC_Data方法get_meta()

以下是您重新访问的代码,其中包含额外的附加功能,该功能将在结帐页面的产品标题下有条件地显示“VAT RELIEF AVAILABLE”文字: (没有覆盖模板review-order.php

add_filter( 'woocommerce_get_price_html', 'conditional_price_suffix', 20, 2 );
function conditional_price_suffix( $price, $product ) {
    if ( $product->get_meta('disability_exemption') === 'yes')
        $price .= ' ' . __('with vat relief');
    else
        $price .= ' ' . __('inc vat');

   return $price;
}

add_filter( 'woocommerce_checkout_cart_item_quantity', 'custom_text_below_checkout_product_title', 20, 3 );
function custom_text_below_checkout_product_title( $quantity_html, $cart_item, $cart_item_key ){
    if ( $cart_item['data']->get_meta('disability_exemption') === 'yes' )
        $quantity_html .= '<br>' . __('VAT RELIEF AVAILABLE');

    return $quantity_html;
}

代码位于活动子主题(活动主题)的function.php文件中。经过测试和工作。

enter image description here

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.