php - 如果产品具有特定的运输类别,则向 WooCommerce 购物车项目添加自定义文本

标签 php wordpress woocommerce cart hook-woocommerce

当产品具有特定的运输类别时,我尝试在结帐/购物车页面上的产品标题下方显示特定文本。我怎么做?

我很难在产品循环内添加文本。

最佳答案

以下内容将在购物车和结帐页面中的购物车商品名称下显示定义的运输类别的自定义文本:

// Display a custom text under cart item name in cart page
add_filter( 'woocommerce_cart_item_name', 'custom_text_cart_item_name', 10, 3 );
function custom_text_cart_item_name( $item_name, $cart_item, $cart_item_key ) {
    // Here below define your shipping class slug
    $shipping_class = 'extra';

    if( is_cart() && $cart_item['data']->get_shipping_class() === $shipping_class ) {
        $item_name .= '<br /><div class="item-shipping-class">' . __("This is my custom text", "woocommerce") . '</div>';
    }
    return $item_name;
}

// Display a custom text under cart item name in checkout
add_filter( 'woocommerce_checkout_cart_item_quantity', 'custom_checkout_text_cart_item_name', 10, 3 );
function custom_checkout_text_cart_item_name( $item_qty, $cart_item, $cart_item_key ) {
    // Here below define your shipping class slug
    $shipping_class = 'extra';

    if( $cart_item['data']->get_shipping_class() === $shipping_class ) {
        $item_qty .= '<br /><div class="item-shipping-class">' . __("This is my custom text", "woocommerce") . '</div>';
    }
    return $item_qty;
}

代码位于事件子主题(或事件主题)的functions.php 文件中。经过测试并有效。

关于php - 如果产品具有特定的运输类别,则向 WooCommerce 购物车项目添加自定义文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57525672/

相关文章:

php - Laravel PHP fatal error : Class 'Table' not found with migrate command

mysql - fatal error - 但它有效 - 不会激活

php - Wordpress 不会在所有页面上显示导航菜单

php - 尝试从数据库中选择 WooCommerce 值时出现 mysqli_fetch_assoc 错误

php - 当购物车中只有特定产品时,删除 WooCommerce 结账条款和条件

php - CakePHP:无法从自定义包加载类

javascript - 检测页面中任何文本区域的更改,并将此特定输入中更改的数据添加到其他表中

css - Wordpress header 未针对移动设备正确缩放

php - 如何在 Woocommerce 中向自定义感谢网址添加变量

php - Laravel 5 - 配置子文件夹