Support » Plugin: WooCommerce » woocommerce_after_cart_item_name

  • Resolved Td3

    (@pneupress)


    Hi,

    I am using a funtion to add custom meta to products. i have used the foilowing hooks for SHOW on Product Loop(woocommerce_after_shop_loop_item) and Product Signle Page(woocommerce_product_meta_end)

    So when applying to get the same results on CART PAGE…by using the hook(woocommerce_after_cart_item_name) it doesn’t work nor show ….?

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

    The page I need help with: [log in to see the link]

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter Td3

    (@pneupress)

    i see you are respoding to other posts here….what determines the priority…it doesnt seem to be by when the ticket was submited here…?

    Thank you for response in advance 🙂

    Hi @pneupress

    To display and save custom meta data added to cart in cart, checkout and orders:
    WC()->cart->add_to_cart( $product_id ,1, 0,array(), array(‘add_size’ => array(‘PR CODE’=>’1.0’) );

    You will use the following code:

    // 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] );
        }
    }

    Please note: You need to change Meta key with your keys.

    Thanks
    Ahir

    Thread Starter Td3

    (@pneupress)

    Hi, very much appreciate this response. It has been 2 weeks I am trying to get this done. I am not a developer. Only very basic understanding, but understand a little none the less. I have provided the function i was trying to get to work with the ‘after_cart_item_name’ hook.

    Questions:
    What is the meta_key you are referring too in the code below that I would need to replace. I think it is $car_type and $season as per identified by WPAllimport program and the code provided below by me here.

    What I don’t understand correctly is where I need to replace those 2 keys in the code provided by you above. My confusion is:

    1. Do I replace the actual word “meta key”? everywhere in your code…or just PR CODE …and if i have 2 meta_keys to add in the code how do i do that?

    2. The ‘add_size’ is confusing me… was that part of your example to demonstrate usage of the code or does it stay? please help me finalize.

    3. Can this code be placed in functions.php file or in the cart.php file under the hook (do_action…after_cart_item_name)

    I provided the code so you can help me complete this. I know with your code it will work but my knowledge ends around this point, need to learn more…but i would really like this to be active on my site sooner than later!

    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
    }
    Plugin Support Ena P (a11n)

    (@drwpcom)

    Hi @pneupress. It looks like no one else has been able to help here. I recommend the following places for more development-oriented questions:

Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.