2016-01-23 140 views
0

我有一个插件,它以编程方式成功地将自定义产品添加到Woocommerce。问题是,当用户第一次导航到产品页面时,没有“添加到购物车”按钮可见。我可以通过编辑产品并手动解决此问题,而无需触摸其他任何东西。我不知道为什么这会起作用,我想以编程方式解决它。Woocommerce'添加到购物车'按钮在程序添加产品后丢失

要想从: enter image description here

  1. 我点击 “编辑”
  2. 我点击 “更新”
  3. 然后我看到:

enter image description here

我如何以编程方式显示“添加到购物车”按钮?

开火“初始化”:

public function createRaffleProduct(){ 
    global $CRG_productName; 
    global $CRG_regularPrice; 
    $post = array(
     'post_author' => $user_id, 
     'post_content' => '', 
     'post_status' => "publish", 
     'post_title' => $CRG_productName, 
     'post_parent' => '', 
     'post_type' => "product", 
    ); 
    //Create post: 
    $post_id = wp_insert_post($post, $wp_error); 
    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', $CRG_regularPrice); 
    update_post_meta($post_id, '_sale_price', "1"); 
} 
+1

乍一看,我想你可能会缺少'_price'元键。仔细查看'save_post'上发生了什么,并确保您使用的是全部相同的元。 – helgatheviking

+0

感谢您提及它,但这不是关于Codeception的问题,所以我删除了标签并解释了为什么您提到了它。 – Naktibalda

+0

Helgathevicking,工作! –

回答

2

编程时创造一个产品,你必须确保所有正在使用的相同的元作为WooCommerce产生的save_post

从您的代码中,您错过了_price元键。如果没有_price,那么产品不是purchasable,并且不会显示添加到购物车按钮。

相关问题