在woocommerce中分组产品父母与Grouped产品儿童

时间:2017-12-16 00:09:05

标签: php wordpress woocommerce

我正在使用PDF打包插件(WooCommerce PDF Invoices)在下订单后创建装箱单。和捆绑产品的Product Bundles插件。

在这些PDF中,我想区分捆绑产品的容器与其子容器。

目前我正在使用Product Bundles:

if(wc_pb_is_bundle_container_cart_item($item) )

它检查项目是否是捆绑产品的容器然后返回true。我需要一个类似的函数,如果item是child或者在bundle中,它将返回true。

这是包装单pdf正文的代码:

<?php
/**
 * PDF packing slip template body.
 *
 * This template can be overridden by copying it to youruploadsfolder/woocommerce-pdf-invoices/templates/packing-slip/simple/yourtemplatename/body.php.
 *
 * HOWEVER, on occasion WooCommerce PDF Invoices will need to update template files and you
 * (the theme developer) will need to copy the new files to your theme to
 * maintain compatibility. We try to do this as little as possible, but it does
 * happen. When this occurs the version of the template file will be bumped and
 * the readme will list any important changes.
 *
 * @author  Bas Elbers
 * @package WooCommerce_PDF_Invoices/Templates
 * @version 0.0.1
 */

$templater                      = WPI()->templater();
$order                          = $templater->order;
$formatted_shipping_address     = $order->get_formatted_shipping_address();
$formatted_billing_address      = $order->get_formatted_billing_address();
$line_items                     = $order->get_items( 'line_item' );
$color                          = $templater->get_option( 'bewpi_color_theme' );
?>

<table>
    <tr class="title">
        <td colspan="3">
            <h2><?php _e( 'Packing Slip', 'woocommerce-pdf-invoices' ); ?></h2>
        </td>
    </tr>
    <tr class="information">
        <td width="50%">
            <?php echo nl2br( $templater->get_option( 'bewpi_company_address' ) ); ?>
        </td>

        <td>
            <?php
            if ( $templater->get_option( 'bewpi_show_ship_to' ) && ! empty( $formatted_shipping_address ) && $formatted_shipping_address !== $formatted_billing_address && ! $templater->has_only_virtual_products( $line_items ) ) {
                printf( '<strong>%s</strong><br />', __( 'Ship to:', 'woocommerce-pdf-invoices' ) );
                echo $formatted_shipping_address;
            }
            ?>
        </td>

        <td>
            <?php echo $formatted_billing_address; ?>
        </td>
    </tr>
</table>
<table>
    <thead>
        <tr class="heading" bgcolor="<?php echo $color; ?>;">
            <th>
                <?php _e( 'Qty', 'woocommerce-pdf-invoices' ); ?>
            </th>

            <th>
                <?php _e( 'Product', 'woocommerce-pdf-invoices' ); ?>
            </th>

            <th>

                <?php _e( 'SKU', 'woocommerce-pdf-invoices' ); ?>
            </th>
        </tr>
    </thead>
    <tbody>
    <?php
    //$parentItem = 0;
    foreach ( $line_items as $item_id => $item ) { 

        $product = BEWPI_WC_Order_Compatibility::get_product( $order, $item );


        if(wc_pb_is_bundle_container_cart_item($item)  ){
        ?>

        <tr class="item">
            <td width="10%">
                <?php  echo $item['qty']; ?>
                <?php // print_r($item); die(); ?>
            </td>

            <td width="65%">
                <?php
                echo $item['name'];

                do_action( 'woocommerce_order_item_meta_start', $item_id, $item, $order );

                $templater->wc_display_item_meta( $item, true );
                $templater->wc_display_item_downloads( $item, true );

                do_action( 'woocommerce_order_item_meta_end', $item_id, $item, $order );
                ?>
            </td>

            <td width="25%">

                <?php echo $product && $product->get_sku() ? $product->get_sku() : '-'; ?>
            </td>
        </tr>

    <?php  }  else   { ?>
        <tr class="item">
            <td width="10%" style="float:right;">
                <?php  echo $item['qty']; ?>
            </td>

            <td width="5%" style="float:right;">
                <?php
                echo '----'.$item['name'];
                //echo 'i m child';

                do_action( 'woocommerce_order_item_meta_start', $item_id, $item, $order );

                $templater->wc_display_item_meta( $item, true );
                $templater->wc_display_item_downloads( $item, true );

                do_action( 'woocommerce_order_item_meta_end', $item_id, $item, $order );
                ?>
            </td>

            <td width="25%" style="float:right;">

                <?php echo $product && $product->get_sku() ? $product->get_sku() : '-'; ?>
            </td>
        </tr>


        <?php }} ?>
    </tbody>
</table>

<table class="notes">
    <tr>
        <td>
            <?php
            // Customer notes.
            if ( $templater->get_option( 'bewpi_show_customer_notes' ) ) {
                // Note added by customer.
                $customer_note = BEWPI_WC_Order_Compatibility::get_customer_note( $order );
                if ( $customer_note ) {
                    printf( '<strong>' . __( 'Note from customer: %s', 'woocommerce-pdf-invoices' ) . '</strong><br />', nl2br( $customer_note ) );
                }

                // Notes added by administrator on 'Edit Order' page.
                foreach ( $order->get_customer_order_notes() as $custom_order_note ) {
                    printf( '<strong>' . __( 'Note to customer: %s', 'woocommerce-pdf-invoices' ) . '</strong><br />', nl2br( $custom_order_note->comment_content ) );
                }
            }
            ?>
        </td>
    </tr>
</table>

仅当产品是任何产品包的一部分时,我才想追加产品名称前面的----。只是想要这个检查,我无法弄清楚我该怎么做

感谢。

1 个答案:

答案 0 :(得分:1)

问题是您使用 Cart Functions 而不是 Order Functions

首先,您需要使用wc_pb_is_bundle_container_order_item代替wc_pb_is_bundle_container_cart_item,因为我们正在检查订单商品

除此之外,wc_pb_is_bundled_order_item是您应该用来确定产品是否是容器项的子项的检查。这很重要,因为它会检查项目是否有父容器,这样当您也按顺序排列非捆绑项目时,您的代码可以扩展。

以下是问题的部分相关代码。

<?php  }  else   { ?>
    <tr class="item">
        <td width="10%" style="float:right;">
            <?php  echo $item['qty']; ?>
        </td>

        <td width="5%" style="float:right;">
            <?php
            // Adding a check to see if current item is a child of a container
            $item_name_padding = wc_pb_is_bundled_order_item( $item ) ? '----': '';
            echo $item_name_padding . $item['name'];
            //echo 'i m child';

            do_action( 'woocommerce_order_item_meta_start', $item_id, $item, $order );

            $templater->wc_display_item_meta( $item, true );
            $templater->wc_display_item_downloads( $item, true );

            do_action( 'woocommerce_order_item_meta_end', $item_id, $item, $order );
            ?>
        </td>

        <td width="25%" style="float:right;">

            <?php echo $product && $product->get_sku() ? $product->get_sku() : '-'; ?>
        </td>
    </tr>


    <?php }} ?>