admin管理员组文章数量:1277397
I have a wordpress plugin which performs a count of purchased products in an order which are flagged with my custom metadata value. The custom metadata value is in the form of a checkbox within the product page. I'm using WooCommerce/WooCommerce subscriptions.
If a customer purchases 5 products, a count is done and then stored in a variable. Only products that have the custom metadata value checkbox are counted.
The count does not occur, as I believe my custom metadata value checkbox is not being detected when using get_meta();
within the loop.
Code below shows the metadata checkbox
/* Adding Extra field to product to Enable Traccar integration */
// Display Fields
add_action('woocommerce_product_options_general_product_data', 'wootraccar_product_custom_fields_add');
function wootraccar_product_custom_fields_add(){
global $post;
echo '<div class="wootraccar_product_custom_field">';
// Custom Product Checkbox Field
woocommerce_wp_checkbox( array(
'id' => '_wootraccar_integration',
'desc' => __('Check this box if Traccar integration is required for this product', 'woocommerce'),
'label' => __('Traccar Integration Required', 'woocommerce'),
'desc_tip' => 'true'
));
echo '</div>';
}
// Save Fields
add_action('woocommerce_process_product_meta', 'wootraccar_product_custom_fields_save');
function wootraccar_product_custom_fields_save($post_id){
// Custom Product Text Field
$wootraccar_integration = isset( $_POST['_wootraccar_integration'] ) ? 'yes' : 'no';
update_post_meta($post_id, '_wootraccar_integration', esc_attr( $wootraccar_integration ));
}
the for each loop
$order = wc_get_order( $order_id );
$email = $order->get_billing_email();
$name = $order->get_billing_first_name();
$devicecount =0;
foreach ( $order->get_items() as $item ) {
if( $item->get_meta( '_wootraccar_integration', true )=='yes')
{
$devicecount = $devicecount + $item->get_quantity();
}
}
I have a wordpress plugin which performs a count of purchased products in an order which are flagged with my custom metadata value. The custom metadata value is in the form of a checkbox within the product page. I'm using WooCommerce/WooCommerce subscriptions.
If a customer purchases 5 products, a count is done and then stored in a variable. Only products that have the custom metadata value checkbox are counted.
The count does not occur, as I believe my custom metadata value checkbox is not being detected when using get_meta();
within the loop.
Code below shows the metadata checkbox
/* Adding Extra field to product to Enable Traccar integration */
// Display Fields
add_action('woocommerce_product_options_general_product_data', 'wootraccar_product_custom_fields_add');
function wootraccar_product_custom_fields_add(){
global $post;
echo '<div class="wootraccar_product_custom_field">';
// Custom Product Checkbox Field
woocommerce_wp_checkbox( array(
'id' => '_wootraccar_integration',
'desc' => __('Check this box if Traccar integration is required for this product', 'woocommerce'),
'label' => __('Traccar Integration Required', 'woocommerce'),
'desc_tip' => 'true'
));
echo '</div>';
}
// Save Fields
add_action('woocommerce_process_product_meta', 'wootraccar_product_custom_fields_save');
function wootraccar_product_custom_fields_save($post_id){
// Custom Product Text Field
$wootraccar_integration = isset( $_POST['_wootraccar_integration'] ) ? 'yes' : 'no';
update_post_meta($post_id, '_wootraccar_integration', esc_attr( $wootraccar_integration ));
}
the for each loop
$order = wc_get_order( $order_id );
$email = $order->get_billing_email();
$name = $order->get_billing_first_name();
$devicecount =0;
foreach ( $order->get_items() as $item ) {
if( $item->get_meta( '_wootraccar_integration', true )=='yes')
{
$devicecount = $devicecount + $item->get_quantity();
}
}
Share
Improve this question
asked Oct 15, 2021 at 5:25
arguadarguad
52 bronze badges
1 Answer
Reset to default 0{
$userid="";
$integration_status="";
// Get an instance of the WC_Order object
$order = wc_get_order( $order_id );
$email = $order->get_billing_email();
$name = $order->get_billing_first_name();
$items = $order->get_items();
$devicecount =0;
foreach ( $items as $item ) {
$product_name = $item->get_name();
$product_id = $item->get_product_id();
$product_variation_id = $item->get_variation_id();
$productToBeIntegrated=get_post_meta($product_id,'_wootraccar_integration', true);
//$integration_status.=$productToBeIntegrated;
if ($productToBeIntegrated=="yes") {
$devicecount = $devicecount + $item->get_quantity();
$integration_status.=$devicecount; }
}
本文标签: plugin developmentCount products with custom metadata field in an order
版权声明:本文标题:plugin development - Count products with custom metadata field in an order 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741265109a2368312.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论