[英]Adding programmatically a custom settings tab to admin product data in WooCommerce
[英]Adding Product Programmatically in woocommerce
<?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', '' );
?>
我能夠在 woo-commerce 中從前端創建產品,但我需要像當用戶創建自己的產品時,然后根據他們的輸入數據更改價格,並以各自的價格以及所有輸入產品的數據保存在數據庫中,假設用戶有大約是狗。 15-20公斤(由用戶輸入)和他的尺寸(下拉菜單)為超重,然后根據用戶輸入的價格計算。 如果產品成功插入,則將此產品自動添加到他/她的購物車並重定向到購物車頁面。
我附上了從前端模板插入產品的代碼。 我怎樣才能做到這一點? 提前致謝。
// Get informations by form
$weight = $_REQUEST['weight']; // your input
$size = $_REQUEST['size']; // your dropdown
//
// Calcul your price here with $weight and $size
// ...
用你的計算調整你的元數據
$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();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.