Search code examples
phparraysecho

Echo array terms


I'm looking to echo part of an array, what is the best method of echo'ing part of an array into a data field

I have an array that has the terms => subscription and need to pull that field into my input data-subscription="[echo subscription in here ]"

$quantites_required = false;
$previous_post      = $post;
$count              = 0;
$tax_query_args     = [
    'post_type' => 'product',
    'tax_query' => [
        [
            'taxonomy' => 'product_type',
            'field'    => 'name',
            'terms'    => 'subscription',
        ],
    ],
];
$tax_query = get_posts($tax_query_args);
global $grouped_product;
foreach ($tax_query as $post) {
    global $post;
    $price = get_post_meta(get_the_ID(), '_price', true);
    ?>
<li id="product-<?php the_ID();?>" <?php post_class();?>>
    <div class="prod-name">
        <div class="radio">
            <input type="checkbox" id="product-<?php the_ID();?>" value="1" name="quantity[<?php the_ID();?>]" class="wc-grouped-product-add-to-cart-checkbox js-group-item-sel" <?php echo ($count != 0 ? '' : ' checked="checked"'); ?> data-subscription="[echo terms subrciptions here]" />
            <label for="product-<?php the_ID();?>">
                <?php echo the_title(); ?>
            </label>
        </div>
    </div>
    <div class="prod-price" data-price="<?php echo number_format($price, 2); ?>">
        <p>
            <?php echo wc_price($price); ?>
        </p>
    </div>
    <div class="prod_flag">
        <?php get_product_flags(true); //Hide featured for grouped child items. ?>
    </div>
    <?php
$count = $count + 1;
}
$count = 0;

Solution

  • Replace this line

    <input type="checkbox" id="product-<?php the_ID();?>" value="1" name="quantity[<?php the_ID();?>]" class="wc-grouped-product-add-to-cart-checkbox js-group-item-sel" <?php echo ($count != 0 ? '' : ' checked="checked"'); ?> data-subscription="[echo terms subrciptions here]" />
    

    with

    <input type="checkbox" id="product-<?php the_ID();?>" value="1" name="quantity[<?php the_ID();?>]" class="wc-grouped-product-add-to-cart-checkbox js-group-item-sel" <?php echo ($count != 0 ? '' : ' checked="checked"'); ?> data-subscription="<?php echo ($tax_query_args['tax_query'][0]['terms']); ?>" />