php - 在 woocommerce 中以编程方式添加产品

标签 php wordpress woocommerce woocommerce-rest-api

<?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-20kg(由用户输入)和他的尺寸(下拉)之间是超重然后根据用户输入要计算的价格。如果产品插入成功,则将此产品自动添加到他/她的购物车并重定向到购物车页面。

我附上了用于从前端模板插入产品的代码。 我怎样才能做到这一点? 提前致谢。

最佳答案

// 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();

关于php - 在 woocommerce 中以编程方式添加产品,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47281841/

相关文章:

php - 如何删除成员(member)A在聊天中接收和发送的消息而不删除成员(member)B帐户中的消息

php - 将 mysql select 查询与 php 变量连接起来?

css - 媒体查询不适用于 Wordpress

javascript - 使用现有 Hook 的函数无法使用自定义 do_action Hook

php - 在后端订单版中 Hook woocommerce 价格

php - 谷歌分析测量协议(protocol)事件跟踪 : events not logging

php - 从刚刚创建的行获取自动递增的整数?

wordpress - 如何将分页添加到我的自定义页面模板

css - 编辑我主页上最受欢迎元素的显示

javascript - Jquery 切换不隐藏/显示特定元素