代码之家  ›  专栏  ›  技术社区  ›  jfar_2020

根据签出中的复选框自定义字段在“项目名称”下添加文本

  •  1
  • jfar_2020  · 技术社区  · 7 年前

    我有一个自定义函数,它检查复选框是否被选中,如果是,它会在价格旁边添加“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;
    }
    

    $isTaxRelefe = get_post_meta($product->id, 'disability_exemption', true);
    
    if ($isTaxRelefe == 'yes') {
       $content .= 'VAT RELIEF AVAILABLE';    
    }
    

    但这并没有起作用,我尝试过各种各样的变化,改变为回声声明等,但没有运气。我肯定我写错了。有人能建议吗?我不太熟悉的是WordPress函数,如果我可以写一个只针对签出页,我不知道它如何决定输出你的文件的位置。一个if-else语句似乎是一个明显的选择,但没有任何运气。

    1 回复  |  直到 7 年前
        1
  •  1
  •   LoicTheAztec    7 年前

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

    也可以使用 WC_Data get_meta() 直接从产品对象。

    下面是带附加钩住函数的重新访问代码,该函数将有条件地显示 “增值税减免可用”
    (不覆盖模板) 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;
    }
    

    enter image description here

    enter image description here