首页 > 解决方案 > WooCommerce 挂钩 - woocommerce_after_cart_item_name

问题描述

我正在使用一个函数将自定义元添加到产品中。woocommerce_after_shop_loop_item我在 Product Loop和 Product Single Page上使用了以下钩子woocommerce_product_meta_end

因此,当使用钩子申请在 CART PAGE 产品/项目上获得相同的结果时, woocommerce_after_cart_item_name它不会在没有结果的情况下工作。

woocommerce_after_cart_item_name如果它与前面提到的其他钩子一起工作,为什么钩子不起作用?

这是我正在使用的代码。我只是更改了钩子以使其显示在产品循环和产品单页中,也需要它显示在购物车产品上。我只是想知道为什么它不适用于购物车项目挂钩..

public function woocommerce_after_cart_item_name() 
{
    global $product;

    if ( !PluginOptions::value('shop_season') && !PluginOptions::value('shop_car_type') )
        return;

    $product_id = $product->get_id();

    $season = get_post_meta( $product_id, 'season', true );
    $car_type = get_post_meta( $product_id, 'car_type', true );

    $tips_seasons   = $this->ui_tips_season();
    $tips_car_types = $this->ui_tips_car_types();

    ?>
    <div class="tyre-details tyre-tips">
        <?php if ( PluginOptions::value('shop_season') && $season ): ?>
            <?php echo $tips_seasons[ strtolower($season) ]; ?>
        <?php endif ?>
        <?php if ( PluginOptions::value('shop_car_type') && $car_type ): ?>
            <?php echo $tips_car_types[ strtolower($car_type) ]; ?>
        <?php endif ?>
    </div>
    <?php
}

它来自一个插件。我刚刚从 woocommerce 支持中获得了此代码,但我不知道如何完成它。他说用我将在下面发布的代码中的 meta_keys 替换。你能帮我完成这个吗?或者告诉我需要更换的地方。我的 meta_keys 是 $season 和 $car-type 但我不知道如何使用 woocommerce 提供的代码。

   // Display custom cart item meta data (in cart and checkout)
add_filter( 'woocommerce_get_item_data',     'display_cart_item_custom_meta_data', 10, 2 );
function display_cart_item_custom_meta_data( $item_data, $cart_item ) {
    $meta_key = 'PR CODE';
    if ( isset($cart_item['add_size']) && isset($cart_item['add_size']        [$meta_key]) ) {
    $item_data[] = array(
        'key'       => $meta_key,
        'value'     => $cart_item['add_size'][$meta_key],
    );
}
return $item_data;
}

// Save cart item custom meta as order item meta data and display it      everywhere on orders and email notifications.
add_action( 'woocommerce_checkout_create_order_line_item',     'save_cart_item_custom_meta_as_order_item_meta', 10, 4 );
function save_cart_item_custom_meta_as_order_item_meta( $item, $cart_item_key, $values, $order ) {
$meta_key = 'PR CODE';
if ( isset($values['add_size']) && isset($values['add_size'][$meta_key]) ) {
    $item->update_meta_data( $meta_key, $values['add_size'][$meta_key] );
}
}

我找到了ClassName(类 FeatureTyreTips)并将其添加到您提供的代码中。我在functions.php中输入了这个,加载购物车页面时出现致命错误。我还尝试将它放在 cart.php 中,结果相同......我尝试在此代码最初来自的同一个插件文件中。所有 3 个位置都不起作用......唯一的区别是它在加载购物车页面时在插件文件中添加和激活新代码时没有显示致命错误。有任何想法吗?格拉齐·维吉塔诺

标签: woocommercecarthook-woocommerce

解决方案


“致命错误”很可能是由于“不能静态调用非静态方法”这一事实:PHP - 静态方法

我现在已经更正了代码。

考虑到您的代码中缺少的类名称是FeatureTyreTips,在产品名称后添加一些文本的正确代码是:

add_action( 'woocommerce_after_cart_item_name', 'add_custom_text_after_cart_item_name', 10, 2 );
function add_custom_text_after_cart_item_name( $cart_item, $cart_item_key ) {
    
    // create an instance of the "PluginOptions" class
    $PluginOptions = new PluginOptions();
    if ( ! $PluginOptions->value( 'shop_season' ) && ! $PluginOptions->value( 'shop_car_type' ) ) {
        return;
    }

    $product = $cart_item['data'];

    $season = get_post_meta( $product->get_id(), 'season', true );
    $car_type = get_post_meta( $product->get_id(), 'car_type', true );

    // create an instance of the "FeatureTyreTips" class
    $FeatureTyreTips = new FeatureTyreTips();
    $tips_seasons   = $FeatureTyreTips->ui_tips_season();
    $tips_car_types = $FeatureTyreTips->ui_tips_car_types();

    if ( ! $PluginOptions->value('shop_season') || ! $season ) {
        $season = '';
    }

    if ( ! $PluginOptions->value('shop_car_type') || ! $car_type ) {
        $car_type = '';
    }

    $html = '<div class="tyre-details tyre-tips">' . trim( $season . ' ' . $car_type ) . '</div>';
    echo $html;

}

该代码已经过测试(如果可能)并且可以工作。
它需要添加到主题的 functions.php 文件或插件中。


推荐阅读