我正在使用这个稍微修改过的脚本(在循环内),它可以帮助我生成两个链接,用于将产品添加到购物车中:
<?php
// Get the ID of the product
$id = $_POST["post_var"];
// Get variations
$args = array(
'post_type' => 'product_variation',
'post_status' => array( 'publish' ),
'numberposts' => 2,
'orderby' => 'menu_order',
'order' => 'asc',
'post_parent' => $product->id
);
$variations = get_posts( $args );
$loop = 0;
if ( $variations ) {
foreach ( $variations as $variation ) {
$formats[$loop]["id"] = absint( $variation->ID );
$formats[$loop]["data"] = get_post_meta( $variation->ID );
$variation_label = "?????????";
$loop++;
}
foreach ( $formats as $type )
$response .= '<a href="' . home_url() . '/checkout?add-to-cart=' . $product_id . '&variation_id=' . $type["id"] . '&quantity=1&attribute_pa_format=' . $variation_label . '">' . $variation_label . '</a>';
} ?>
<?php echo $response; ?>
我唯一想知道的是如何获得变体标签。
每个产品都有称为“格式”的变体,其中包含“电子”和“物理”选项,我需要将这些变量输出到链接中。所以$ response看起来像这样:
<a href="http://example.com/checkout?add-to-cart=93&variation_id=94&quantity=1&attribute_pa_format=Electronic">Electronic</a
<a href="http://example.com/checkout?add-to-cart=93&variation_id=95&quantity=1&attribute_pa_format=Physical">Physical</a>
你可以看到我添加了一个名为$ variation_label的变量作为占位符。
难住了。