Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
265 views
in Technique[技术] by (71.8m points)

cart - WooCommerce Hook - woocommerce_after_cart_item_name

I am using a function to add custom meta to products. I have used the following hooks for SHOW on Product Loop woocommerce_after_shop_loop_item and Product Single Page woocommerce_product_meta_end.

So, when applying to get the same results on CART PAGE product/item by using the hook woocommerce_after_cart_item_name it doesn’t work with no results.

Why isn’t the hook woocommerce_after_cart_item_name working if it works with the other previous hooks mentioned?

This the code I am using. I just change the hook to make it to show in Product Loop and Product Single Page, need it to show on cart Products as well. I was just wondering why it doesn't work with cart item hook..

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
}

It is from a plugin. I was just given this code from woocommerce support but i do not know how to complete it. He says to replace with my meta_keys in the code which I will post below here. Can you help me finish this? or tell me where I need to replace. My meta_keys are $season and $car-type but i don't know how to apply with the code provided by 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] );
}
}

I located the ClassName (class FeatureTyreTips) and added that to the code you provided. I entered this in functions.php and it said fatal error when loading the cart page. I also tried placing it in the cart.php with same results...and I tried in the same plugin file where this code came from originally. All 3 locations did not work...only difference was that it did not show fatal error when adding and activating your new code in the plugin file when loading cart page. Any ideas? grazie Vdgeatano

question from:https://stackoverflow.com/questions/65866037/woocommerce-hook-woocommerce-after-cart-item-name

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The "Fatal Error" is most likely due to the fact that "the non-static method cannot be called statically": https://www.php.net/manual/en/language.oop5.static.php

I have now corrected the code.

Considering that the class name that was missing in your code is FeatureTyreTips, the correct code to add some text after the product name is:

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;

}

The code has been tested (where possible) and works.
It needs to be added to your theme's functions.php file or in your plugin.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...