在functions.php中添加“显示单一变体”代码时出错

时间:2019-04-29 20:02:04

标签: php wordpress woocommerce product hook-woocommerce

在WooCommerce中,我试图将一些自定义代码添加到活动主题的functions.php文件中,这将使可变产品能够在商店和档案页面上显示属性下拉列表,数量字段以及“添加到购物车”按钮。

这是我的实际代码:

// Display variations dropdowns on shop page for variable products
add_filter( 'woocommerce_loop_add_to_cart_link', 'woo_display_variation_dropdown_on_shop_page' );

function woo_display_variation_dropdown_on_shop_page() {
    global $product;

    if( $product->is_type( 'variable' )) {

        $attribute_keys = array_keys( $product->get_attributes() );
        ?>

        <form class="variations_form cart" method="post" enctype='multipart/form-data' data-product_id="<?php echo absint( $product->id ); ?>" data-product_variations="<?php echo htmlspecialchars( json_encode( $product->get_available_variations() ) ) ?>">
            <?php do_action( 'woocommerce_before_variations_form' ); ?>

            <?php if ( empty( $product->get_available_variations() ) && false !== $product->get_available_variations() ) : ?>
                <p class="stock out-of-stock"><?php _e( 'This product is currently out of stock and unavailable.', 'woocommerce' ); ?></p>
            <?php else : ?>
                <table class="variations" cellspacing="0">
                    <tbody>
                        <?php foreach ( $product->get_attributes() as $attribute_name => $options ) : ?>
                            <tr>
                                <td class="label"><label for="<?php echo sanitize_title( $attribute_name ); ?>"><?php echo wc_attribute_label( $attribute_name ); ?></label></td>
                                <td class="value">
                                    <?php
                                        $selected = isset( $_REQUEST[ 'attribute_' . sanitize_title( $attribute_name ) ] ) ? wc_clean( urldecode( $_REQUEST[ 'attribute_' . sanitize_title( $attribute_name ) ] ) ) : $product->get_variation_default_attribute( $attribute_name );
                                        wc_dropdown_variation_attribute_options( array( 'options' => $options, 'attribute' => $attribute_name, 'product' => $product, 'selected' => $selected ) );
                                        echo end( $attribute_keys ) === $attribute_name ? apply_filters( 'woocommerce_reset_variations_link', '<a class="reset_variations" href="#">' . __( 'Clear', 'woocommerce' ) . '</a>' ) : '';
                                    ?>
                                </td>
                            </tr>
                        <?php endforeach;?>
                    </tbody>
                </table>

                <?php do_action( 'woocommerce_before_add_to_cart_button' ); ?>

                <div class="single_variation_wrap">
                    <?php
                        /**
                         * woocommerce_before_single_variation Hook.
                         */
                        do_action( 'woocommerce_before_single_variation' );

                        /**
                         * woocommerce_single_variation hook. Used to output the cart button and placeholder for variation data.
                         * @since 2.4.0
                         * @hooked woocommerce_single_variation - 10 Empty div for variation data.
                         * @hooked woocommerce_single_variation_add_to_cart_button - 20 Qty and cart button.
                         */
                        do_action( 'woocommerce_single_variation' );

                        /**
                         * woocommerce_after_single_variation Hook.
                         */
                        do_action( 'woocommerce_after_single_variation' );
                    ?>
                </div>

                <?php do_action( 'woocommerce_after_add_to_cart_button' ); ?>
            <?php endif; ?>

            <?php do_action( 'woocommerce_after_variations_form' ); ?>
        </form>

        <?php } else {

        echo sprintf( '<a rel="nofollow" href="%s" data-quantity="%s" data-product_id="%s" data-product_sku="%s" class="%s">%s</a>',
            esc_url( $product->add_to_cart_url() ),
            esc_attr( isset( $quantity ) ? $quantity : 1 ),
            esc_attr( $product->id ),
            esc_attr( $product->get_sku() ),
            esc_attr( isset( $class ) ? $class : 'button' ),
            esc_html( $product->add_to_cart_text() )
        );
    }
} 

但是抛出了以下错误,并使它无法正常工作:

  

由于文件wp-content / plugins / woocommerce-composite-products / includes / wc-cp-template-functions.php文件的第666行出现错误,您的PHP代码更改已回滚。请修复并尝试再次保存。

     

未捕获的错误:在
中的null上调用成员函数get_type()   wp-content/plugins/woocommerce-composite-products/includes/wc-cp-template-functions.php:666
  堆栈跟踪:
  #0 wp-includes / class-wp-hook.php(286):wc_cp_before_add_to_cart_button('')
  #1 wp-includes / class-wp-hook.php(310):WP_Hook->apply_filters('', Array)
  #2 wp-includes / plugin.php(465):WP_Hook->do_action(Array)
  #3 wp-content / themes / electro-child / functions.php(2228):do_action('woocommerce_bef...')
  #4 wp-includes / class-wp-hook.php(288):woo_display_variation_dropdown_on_shop_page('<a href="https:...')
  #5 wp-includes / plugin.php(208):WP_Hook->apply_filters('<a href="https:...', Array)
  #6 wp-content/plugins/woocommerce/templates/loop/add

我认为与我使用的Composite产品插件存在冲突。

我们非常感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

代码中最大的错误是您只是忘记了使用过滤器钩子,在这种情况下,您需要返回输出,而不是回显它。

然后,您将使用此过滤器挂钩中包含的挂钩参数。

您还只需要仅更改可变产品的行为,因为在您使用实际代码的同时,您还使用了WooCommerce Composite Products插件,因此它会报告错误。

>

因此,请尝试尝试类似(未试用)的内容:

// Display variations dropdowns on shop page for variable products
add_filter( 'woocommerce_loop_add_to_cart_link', 'woo_display_variation_dropdown_on_shop_page', 10, 3 );

function woo_display_variation_dropdown_on_shop_page( $add_to_cart_link, $product, $args ) {
    if( $product->is_type('variable') ) {

        $attribute_keys = array_keys( $product->get_attributes() );

        ob_start(); // Start buffering
        ?>

        <form class="variations_form cart" method="post" enctype='multipart/form-data' data-product_id="<?php echo absint( $product->id ); ?>" data-product_variations="<?php echo htmlspecialchars( json_encode( $product->get_available_variations() ) ) ?>">
            <?php do_action( 'woocommerce_before_variations_form' ); ?>

            <?php if ( empty( $product->get_available_variations() ) && false !== $product->get_available_variations() ) : ?>
                <p class="stock out-of-stock"><?php _e( 'This product is currently out of stock and unavailable.', 'woocommerce' ); ?></p>
            <?php else : ?>
                <table class="variations" cellspacing="0">
                    <tbody>
                        <?php foreach ( $product->get_attributes() as $attribute_name => $options ) : ?>
                            <tr>
                                <td class="label"><label for="<?php echo sanitize_title( $attribute_name ); ?>"><?php echo wc_attribute_label( $attribute_name ); ?></label></td>
                                <td class="value">
                                    <?php
                                        $selected = isset( $_REQUEST[ 'attribute_' . sanitize_title( $attribute_name ) ] ) ? wc_clean( urldecode( $_REQUEST[ 'attribute_' . sanitize_title( $attribute_name ) ] ) ) : $product->get_variation_default_attribute( $attribute_name );
                                        wc_dropdown_variation_attribute_options( array( 'options' => $options, 'attribute' => $attribute_name, 'product' => $product, 'selected' => $selected ) );
                                        echo end( $attribute_keys ) === $attribute_name ? apply_filters( 'woocommerce_reset_variations_link', '<a class="reset_variations" href="#">' . __( 'Clear', 'woocommerce' ) . '</a>' ) : '';
                                    ?>
                                </td>
                            </tr>
                        <?php endforeach;?>
                    </tbody>
                </table>

                <?php do_action( 'woocommerce_before_add_to_cart_button' ); ?>

                <div class="single_variation_wrap">
                    <?php
                        /**
                         * woocommerce_before_single_variation Hook.
                         */
                        do_action( 'woocommerce_before_single_variation' );

                        /**
                         * woocommerce_single_variation hook. Used to output the cart button and placeholder for variation data.
                         * @since 2.4.0
                         * @hooked woocommerce_single_variation - 10 Empty div for variation data.
                         * @hooked woocommerce_single_variation_add_to_cart_button - 20 Qty and cart button.
                         */
                        do_action( 'woocommerce_single_variation' );

                        /**
                         * woocommerce_after_single_variation Hook.
                         */
                        do_action( 'woocommerce_after_single_variation' );
                    ?>
                </div>

                <?php do_action( 'woocommerce_after_add_to_cart_button' ); ?>
            <?php endif; ?>

            <?php do_action( 'woocommerce_after_variations_form' ); ?>
        </form> 
        <?php

        $add_to_cart_link = ob_get_clean(); // The buffered content
    }
    return $add_to_cart_link;
}