为WooCommerce产品添加自定义按钮以获取特定属性和术语

时间:2017-08-31 08:28:29

标签: php wordpress woocommerce attributes product

对于拥有WooCommerce的网站,我需要在产品具有特定属性+值时向单个产品页面添加按钮。在这种情况下:当产品具有属性pa_aanbod添加按钮,并且当产品具有属性值Verhuur时,添加另一个按钮。

通过制作如下所示的课程,我做到了这一点。这样做的问题是,当我向产品添加另一个随机属性时,按钮组正在被复制,因此对于每个新添加的属性,都会添加一个额外的按钮组。

我的问题是:如何确保按钮组仅添加一次,并且在向产品添加其他属性时不会重复?我认为它与foreach语句有关,但我不确定如何解决这个问题。

if (!class_exists('WcAddFormButtons')) {

class WcAddFormButtons
{

    public function __construct()
    {

        add_action( 'woocommerce_product_meta_end', array( $this, 'single_product_add_formbuttons', ), 50);

    }

    /**
     * Check the terms of product taxonomy pa_aanbod
     * to get the right buttons associated with the forms
     *
     * @since 1.0.0
     */
    public function single_product_add_formbuttons()
    {

        global $product;
        $attributes = $product->get_attributes();
        $product_title = get_the_title( $product->get_id() );

        if ( ! $attributes ) {
            return;
        }

        foreach ( $attributes as $attribute ) :

            // skip variations
            if ($attribute['is_variation']) {
                continue;
            }

            if ($attribute['is_taxonomy']) {

                $terms = wp_get_post_terms($product->get_id(), 'pa_aanbod');

                echo '<div class="button-group">';

                    foreach ($terms as $term) :

                        switch ($term->name) {

                            case 'Verhuur' :
                                echo '<a class="vc_general vc_btn vc_btn-style-normal vc_btn-color-main-brand-color full-width" href="' . site_url() . '/direct-huren/?makkina_machine_name=' . $product_title . '">Direct huren</a>';
                                break;
                        }

                    endforeach;

                    echo '<a class="vc_general vc_btn vc_btn-style-normal vc_btn-color-main-brand-color full-width" href="' . site_url() . '/offerte-aanvragen/?makkina_machine_name=' . $product_title . '">Offerte aanvragen</a>';

                    echo '<a class="ask-demo" href="' . site_url() . '/demo-aanvragen/?makkina_machine_name=' . $product_title . '">Demo aanvragen</a>';

                echo '</div>';
            }

        endforeach;

    }

}
new WcAddFormButtons();
}

1 个答案:

答案 0 :(得分:1)

添加其他属性时,以下是避免重复的正确代码:

if (!class_exists('WcAddFormButtons')) {

class WcAddFormButtons
{

    public function __construct()
    {

        add_action( 'woocommerce_product_meta_end', array( $this, 'single_product_add_formbuttons', ), 50);

    }

    /**
     * Check the terms of product taxonomy pa_aanbod
     * to get the right buttons associated with the forms
     *
     * @since 1.0.0
     */
    public function single_product_add_formbuttons()
    {

        global $product;

        ## -- Set HERE below your product attribute taxonomy and term name -- ##
        $taxonomy = 'pa_aanbod';
        $term_name = 'Verhuur';

        $attributes = $product->get_attributes();
        $product_title = get_the_title( $product->get_id() );

        if ( ! $attributes ) return;

        foreach ( $attributes as $attribute ) :

            $attribute_data = $attribute->get_data();

            if ( $attribute_data['variation'] != 1 && $attribute_data['is_taxonomy'] == 1 && $attribute_data['name'] == $taxonomy ) {

                $terms = wp_get_post_terms( $product->get_id(), $taxonomy );

                echo '<div class="button-group">';

                foreach( $terms as $term ) if( $term->name == $term_name ) {

                    echo '<a class="vc_general vc_btn vc_btn-style-normal vc_btn-color-main-brand-color full-width" href="' . site_url() . '/direct-huren/?makkina_machine_name=' . $product_title . '">Direct huren</a>';
                        break;
                }

                echo '<a class="vc_general vc_btn vc_btn-style-normal vc_btn-color-main-brand-color full-width" href="' . site_url() . '/offerte-aanvragen/?makkina_machine_name=' . $product_title . '">Offerte aanvragen</a>';

                echo '<a class="ask-demo" href="' . site_url() . '/demo-aanvragen/?makkina_machine_name=' . $product_title . '">Demo aanvragen</a>';

                echo '</div>';
            }

        endforeach;

    }

}
new WcAddFormButtons();
}

此代码经过测试并正常运行。