创建具有两个变体的新产品并将所选产品添加到购物车期望的结果: 表单在变体之间进行选择,并将相应的变体添加到购物车。当前结果:无论选择哪个,都将第二个变体添加到购物车注意:添加variation_id应该非常简单,但由于尚未创建变体 ID,我需要一种方法将表单中的变体术语与新分配的variation_id<form name="new-prod" method="post" action=""><input type="radio" name="attribute_pa_size" value="11oz" id="pa_size_v_11oz"><input type="radio" name="attribute_pa_size" value="15oz" checked="checked" id="pa_size">PHP:$post_size = $_POST['attribute_pa_size'];创建了多个变体:第一个变体$parent_id = $product_ID;$variation = array('post_title' => 'Product #' . $parent_id . ' Variation','post_content' => '','post_status' => 'publish','post_parent' => $parent_id,'post_type' => 'product_variation');$variation_id = wp_insert_post( $variation );update_post_meta( $variation_id, '_regular_price', 21.95 );update_post_meta( $variation_id, '_price', 21.95 );update_post_meta( $variation_id, 'attribute_' . $mythis->size_tax, '15oz' );WC_Product_Variable::sync( $parent_id );第二变体$parent_id = $product_ID;$variation = array('post_title' => 'Product #' . $parent_id . ' Variation','post_content' => '','post_status' => 'publish','post_parent' => $parent_id,'post_type' => 'product_variation');$variation_id = wp_insert_post( $variation );update_post_meta( $variation_id, '_regular_price', 18.95 );update_post_meta( $variation_id, '_price', 18.95 );update_post_meta( $variation_id, 'attribute_' . $mythis->size_tax, '11oz' );WC_Product_Variable::sync( $parent_id );将变体添加到购物车$quantity = 1;$variation = $post_size;$woocommerce->cart->add_to_cart( $product_ID, $quantity, $variation_id, $variation );
1 回答
慕尼黑8549860
TA贡献1818条经验 获得超11个赞
$variation_id替换$elevenvariation_id了第一个变体的所有引用。
然后用以下内容替换了“将变体添加到购物车”块,它从我的初始测试开始就成功了:
$quantity = 1;
$variation = $post_size;
$answer = $_POST['attribute_pa_size'];
if ($answer == "15oz") {
$woocommerce->cart->add_to_cart( $product_ID, $quantity, $variation_id, $variation );
}
else {
$woocommerce->cart->add_to_cart( $product_ID, $quantity, $elevenvariation_id, $variation );
}
- 1 回答
- 0 关注
- 73 浏览
添加回答
举报
0/150
提交
取消