我正在尝试在购物车中添加产品的自定义变体,但在经过一番努力将产品添加到购物车但没有变化数据之后。
这是我正在使用的代码: 的javascript:
//create request object
cartItemobj.productID = jQuery('#product_id').val();
cartItemobj.qty = qtys;
cartItemobj.customData = jQuery('.wct_frontend_design_right_panel').html();
cartItemobj.designAttr = designData;
cartItemobj.measurementsAttr = measurementsData;
jQuery.ajax ({
type: "POST",
url: jQuery('#admin-ajax').val(),
data: {action:'wct_add_to_cart',wct_design_data:cartItemobj},
success: function( data )
{
if( data == 'cart_updated' )
{
jQuery('.wct_add_to_cart_loader').hide();
window.location.href = jQuery('#wct_cart_url').val();
}
},
error: function(){}
});
此Javascript调用此函数执行添加到购物车操作
function wct_add_to_cart()
{
global $wpdb,$woocommerce;
$final_product_id = '';
$final_custom_variations_id = '';
$uIds = uniqid().rand(0,10);
$guid = get_permalink($_REQUEST['wct_design_data']['productID']);
$post_data=array(
'post_author'=>1,
'post_status'=>'publish',
'post_name'=>'product-'.$_REQUEST['wct_design_data']['productID'].'-variation',
'post_parent'=>$_REQUEST['wct_design_data']['productID'],
'post_title'=>'wct_custom_variation',
'post_type'=>'product_variation',
'guid' => $guid ,
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_date' => date('Y-m-d H:i:s'),
'post_date_gmt' => date('Y-m-d H:i:s'),
'post_modified' => date('Y-m-d H:i:s'),
'post_modified_gmt' => date('Y-m-d H:i:s'),
);
//$post_id = wp_insert_post($post_data, true);
$wpdb->insert( $wpdb->prefix."posts", $post_data );
$post_ids = $wpdb->insert_id;
$final_product_id = $_REQUEST['wct_design_data']['productID'];
$price = $wpdb->get_var("select meta_value from $wpdb->postmeta where meta_key = '_india-1_price' and post_id = $final_product_id");
update_post_meta( $post_ids, '_india-1_price', $price);
update_post_meta( $post_ids, 'attribute_scwct-variation', 'scwct-variation' );
$final_custom_variations_id = $post_ids;
$get_price = get_post_meta($post_ids,'_india-1_price',TRUE);
$arr = array();
$arr['type'] = 'custom';
$arr['design'] = 'custom-data_' . $uIds;
$arr['wct_custom_price'] = $price;
if( isset($_REQUEST['wct_design_data']['measurementsAttr']) && $_REQUEST['wct_design_data']['measurementsAttr']['torso_length'] )
{
$measurement = 'Torso Length: '.$_REQUEST['wct_design_data']['measurementsAttr']['torso_length'].', Sleeves Length: '.$_REQUEST['wct_design_data']['measurementsAttr']['sleeves_length'].', Shoulder Width: '.$_REQUEST['wct_design_data']['measurementsAttr']['shoulder_width'].', Neck: '.$_REQUEST['wct_design_data']['measurementsAttr']['neck'].', Chest Around: '.$_REQUEST['wct_design_data']['measurementsAttr']['chest_around'].', Stomach Area: '.$_REQUEST['wct_design_data']['measurementsAttr']['stomach_area'].', Hips Area: '.$_REQUEST['wct_design_data']['measurementsAttr']['hips_area'].', Bicep Around: '.$_REQUEST['wct_design_data']['measurementsAttr']['bicep_around'];
$arr['Measurements'] = $measurement;
}
if($woocommerce->cart->add_to_cart( $final_product_id, $_REQUEST['wct_design_data']['qty'], $final_custom_variations_id, arr, null ) )
{
echo 'cart_updated';
}
此代码将产品添加到购物车但不刷新页面。手动重新加载页面后更新购物车但没有自定义变体。 请帮忙