在Woocommerce与MySQL创建优惠券

时间:2019-01-30 15:34:28

标签: mysql sql wordpress woocommerce

我有两个不同的站点。 Site1是wordpress网站。站点2是其他网站。我正在试图做的是建立在从站点2站点1的数据库中获得优惠券。我已成功连接到站点1的数据库,我能够使MySQL查询。现在,如何仅使用mysql从site2创建woocommerce优惠券。 (站点2是内置在node.js中)

我想做这样的事情(当然这是行不通的,但是这样的事情)

INSERT INTO wp58_posts(post_title, post_status, comment_status,ping_status, post_password, post_name, post_type )values('testcoupon', 'publish', 'closed', 'closed', 'testcoupon', 'shop_coupon');

1 个答案:

答案 0 :(得分:0)

所有魔法-Create a coupon programatically

$coupon_code = 'UNIQUECODE'; // Code
$amount = '10'; // Amount
$discount_type = 'fixed_cart'; // Type: fixed_cart, percent, fixed_product, percent_product
$wp_prefix = 'wp_'; 
$post_author = 1;  // post author ID

$sql = "INSERT INTO `{$wp_prefix}posts` 
(`post_title`, `post_content`, `post_status`, `post_author`, `post_type`) 
  VALUES 
({$coupon_code}, '', 'publish', {$post_author}, 'shop_coupon')";

$coupon_id = $last_inser_id;

$sql = "INSERT INTO `{$wp_prefix}postmeta` 
(`post_id`, `meta_key`, `meta_value`) 
  VALUES 
({$coupon_id}, 'discount_type', {$discount_type})";

$sql = "INSERT INTO `{$wp_prefix}postmeta` 
(`post_id`, `meta_key`, `meta_value`) 
  VALUES 
({$coupon_id}, 'coupon_amount', {$amount})";

$sql = "INSERT INTO `{$wp_prefix}postmeta` 
(`post_id`, `meta_key`, `meta_value`) 
  VALUES 
({$coupon_id}, 'individual_use', 'no')";

$sql = "INSERT INTO `{$wp_prefix}postmeta` 
(`post_id`, `meta_key`, `meta_value`) 
  VALUES 
({$coupon_id}, 'product_ids', '')";

$sql = "INSERT INTO `{$wp_prefix}postmeta` 
(`post_id`, `meta_key`, `meta_value`) 
  VALUES 
({$coupon_id}, 'exclude_product_ids', '')";

$sql = "INSERT INTO `{$wp_prefix}postmeta` 
(`post_id`, `meta_key`, `meta_value`) 
  VALUES 
({$coupon_id}, 'usage_limit', '')";

$sql = "INSERT INTO `{$wp_prefix}postmeta` 
(`post_id`, `meta_key`, `meta_value`) 
  VALUES 
({$coupon_id}, 'expiry_date', '')";

$sql = "INSERT INTO `{$wp_prefix}postmeta` 
(`post_id`, `meta_key`, `meta_value`) 
  VALUES 
({$coupon_id}, 'apply_before_tax', 'yes')";

$sql = "INSERT INTO `{$wp_prefix}postmeta` 
(`post_id`, `meta_key`, `meta_value`) 
  VALUES 
({$coupon_id}, 'free_shipping', 'no')";