Woocommerce自定义产品表的添加到购物车按钮

时间:2019-12-21 10:56:50

标签: php wordpress woocommerce

我有一个带有产品变化形式的自定义表格: 我可以叫“名称”和“价格”,但是我无法添加数量选择器并添加到购物车按钮。 有人知道解决方案吗? 预先谢谢你!

function custom_table_after_single_product(){
    global $product;

   // Only for variable products
 if( ! $product->is_type('variable')) return; 

$available_variations = $product->get_available_variations();

if( count($available_variations) > 0 ){

    $output = '<table>
        <thead>
            <tr>
                <th>'. __( 'Name', 'woocommerce' ) .'</th>
                <th>'. __( 'Regular price', 'woocommerce' ) .'</th>
                <th>'. __( 'Quantity', 'woocommerce' ) .'</th>
                <th>'. __( 'Add to Cart', 'woocommerce' ) .'</th>
            </tr>
        </thead>
        <tbody>';

    foreach( $available_variations as $variation ){
        // Get an instance of the WC_Product_Variation object
        $product_variation = wc_get_product($variation['variation_id']);

        $output .= '
        <tr>
            <td>'. $product_variation->get_name() .'</td>
            <td>'. $product_variation->get_regular_price() .'</td>
            <td> <?php woocommerce_quantity_input(); ?></td>
            <td></td>
        </tr>';
    }
    $output .= '
        </tbody>
    </table>';

    echo $output;
}
}

0 个答案:

没有答案