如何保存woocommerce产品元数据,然后通过客户电子邮件发送?

时间:2017-08-21 18:06:42

标签: php wordpress woocommerce hook-woocommerce orders

经过两天的挣扎,我现在在这里分享我的问题。我希望在插入产品时将会话值保存到产品的元字段中。目前我使用的代码不起作用

add_action( 'woocommerce_review_order_after_payment', 'save_product_status_discount', 10, 1 );

function save_product_status_discount($order, $sent_to_admin, $plain_text, $email){
    $order_data = $order->get_data(); 
    $item_data = $item_obj->get_data();
    $my_post_meta1 = get_post_meta($item_data['order_id'], 'status_discount'.$item_data['order_id'], true);
     if(empty ( $my_post_meta1 )){
    update_post_meta($item_data['order_id'],'status_discount'.$item_data['order_id'],$_SESSION['status_discount_price']);

     }

}

请帮帮我,我该怎么办。感谢

1 个答案:

答案 0 :(得分:0)

您可以使用但效果不佳,因为您需要为每个订单项 执行此操作(我认为,正确的方法)在最后)

$('#add_taxo').on('click',function() {
    document.getElementById('okok').innerHTML += '<li class="list-group-item"> <%= taxonomies_select_tag( 'statistic[taxonomy]', Folders::Taxonomy, Folders::Taxonomy::TAXONOMY_KEY, level: 3, path: 'folders/taxonomies', include_blank: true, class: 'required form-control test_taxo')%> </li>';
    alert("lklk"); 
});

代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中。

  

正确的方法是将此&#34;状态折扣&#34;作为商品ID元数据,因为您可以在订单中包含许多商品。

所以最好的选项和工作代码应该只是那个紧凑的功能:

// Save the order meta with field value
add_action( 'woocommerce_checkout_update_order_meta', 'save_product_status_discount', 10, 1 );
function save_product_status_discount( $order_id ) {
    $status_discount = get_post_meta($order_id, 'status_discount'.$order_id, true);
    if(empty ( $status_discount ) && ! empty( $_SESSION['status_discount_price'] ) )
        update_post_meta( $order_id, 'status_discount'.$order_id, $_SESSION['status_discount_price'] );
    }
}

// Display the order meta with field value
add_action( 'woocommerce_review_order_after_payment', 'display_product_status_discount', 10, 4 );
function save_product_status_discount($order, $sent_to_admin, $plain_text, $email){
    $status_discount = get_post_meta($order_id, 'status_discount'.$order_id, true);
    if( ! empty ( $status_discount ) )
        echo '<p>'.__( 'Status discount', 'woocommerce' ) . ': ' . $status_discount . '</p>
}

代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中。

在WooCommerce 3+中测试并且有效。