2017-11-14 7 views
1
<?php 
/** 
* Template name: Create Product 
* @package storefront 
*/ 

$post_id = wp_insert_post(array(
'post_title' => 'Adams Product', 
'post_content' => 'Here is content of the post, so this is our great new products description', 
'post_status' => 'publish', 
'post_type' => "product", 
)); 
wp_set_object_terms($post_id, 'simple', 'product_type'); 
update_post_meta($post_id, '_visibility', 'visible'); 
update_post_meta($post_id, '_stock_status', 'instock'); 
update_post_meta($post_id, 'total_sales', '0'); 
update_post_meta($post_id, '_downloadable', 'no'); 
update_post_meta($post_id, '_virtual', 'yes'); 
update_post_meta($post_id, '_regular_price', ''); 
update_post_meta($post_id, '_sale_price', ''); 
update_post_meta($post_id, '_purchase_note', ''); 
update_post_meta($post_id, '_featured', 'no'); 
update_post_meta($post_id, '_weight', ''); 
update_post_meta($post_id, '_length', ''); 
update_post_meta($post_id, '_width', ''); 
update_post_meta($post_id, '_height', ''); 
update_post_meta($post_id, '_sku', ''); 
update_post_meta($post_id, '_product_attributes', array()); 
update_post_meta($post_id, '_sale_price_dates_from', ''); 
update_post_meta($post_id, '_sale_price_dates_to', ''); 
update_post_meta($post_id, '_price', ''); 
update_post_meta($post_id, '_sold_individually', ''); 
update_post_meta($post_id, '_manage_stock', 'no'); 
update_post_meta($post_id, '_backorders', 'no'); 
update_post_meta($post_id, '_stock', ''); 
?> 

나는 woocommerce 프런트 엔드에서 제품을 만들 수 있어요하지만 난 사용 후 가격이 자신의 입력 데이터에 따라 변화하고 함께 데이터베이스에 저장하기 자신의 제품을 만들 때처럼 필요 제품에 대한 모든 입력 데이터뿐만 아니라 각 가격을 사용자가 약 개가 있다고 가정합니다. 15-20kg (사용자 입력)과 그의 크기 (드롭 다운) 사이의 초과 중량은 다음 사용자 입력에 따라 계산됩니다. 제품이 성공적으로 삽입되면이 제품을 자동으로 장바구니에 추가하고 장바구니 페이지로 리디렉션하십시오.추가 제품 프로그래밍 woocommerce에서

프런트 엔드에 템플릿의 제품을 삽입하기위한 코드를 첨부하고 있습니다. 어떻게 할 수 있습니까? 미리 감사드립니다. 와

+0

당신이 가격을 계산하는 공식을 지정할 수 있습니다 calcul? –

답변

0
// Get informations by form 
$weight = $_REQUEST['weight']; // your input 
$size = $_REQUEST['size']; // your dropdown 
// 
// Calcul your price here with $weight and $size 
// ... 

Adapte 당신의 METAS 당신의

$metas = array(
    '_visibility' => 'visible', 
    '_stock_status' => 'instock', 
    'total_sales' => '0', 
    '_downloadable' => 'no', 
    '_virtual' => 'yes', 
    '_regular_price' => '', 
    '_sale_price' => '', 
    '_purchase_note' => '', 
    '_featured' => 'no', 
    '_weight' => '', 
    '_length' => '', 
    '_width' => '', 
    '_height' => '', 
    '_sku' => '', 
    '_product_attributes' => array(), 
    '_sale_price_dates_from' => '', 
    '_sale_price_dates_to' => '', 
    '_price' => '', 
    '_sold_individually' => '', 
    '_manage_stock' => 'no', 
    '_backorders' => 'no', 
    '_stock' => '' 
); 
foreach ($metas as $key => $value) { 
    update_post_meta($post_id, $key, $value); 
} 

// Add product to cart 
WC()->cart->add_to_cart($post_id); 

// redirect 
wp_redirect(get_permalink($post_id)); 
exit(); 
관련 문제