1

I'm trying to create a snippet at the checkout of woocommerce to take a dropdown list with the label course1 and add all product titles as selections.

as im new to php in general, im struggling to figure out how I would pull the names of the products in the cart.

any tips would be very much appreciated.

Thanks

1 Answer 1

0

You can get cart items from global $woocommerce object

global $woocommerce;
$items = $woocommerce->cart->get_cart();
echo "<select name='products-list'">;
foreach($items as $item => $values) { 
    $_product = $values['data']->post; 
    $price = get_post_meta($values['product_id'] , '_price', true);
    echo "<option value=".$values['product_id'].">".$_product->post_title."-".$price."</option>";   
        } 
echo "</select>";

Use this PHP snippet where you want to show dropdown list

2
  • Thanks for your reply, but where/how do you feed that into a preexisting dropdown called course1 on the page? I'm trying to add it as a snippet to my functions.php
    – Sam
    Oct 13, 2016 at 18:46
  • Either you should append these options (product names) to existing dropdown or name this generated newly dropdown as course1. Use hooks like woocommerce_after_cart_table to display your dropdown from functions.php. You can refer WordPress codex or woocommerce documentation about using hooks. Oct 14, 2016 at 1:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.