Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php shortcode中的WooCommerce do_操作将页面布局搞乱_Php_Wordpress_Woocommerce_Action_Shortcode - Fatal编程技术网

Php shortcode中的WooCommerce do_操作将页面布局搞乱

Php shortcode中的WooCommerce do_操作将页面布局搞乱,php,wordpress,woocommerce,action,shortcode,Php,Wordpress,Woocommerce,Action,Shortcode,我创建了一个短代码来显示特定产品类别的产品(结合了几个ACF字段)。所有这些都很好,所以为了您自己的利益,请使用下面的代码 短代码如下所示:[yl\u get\u products cat\u id='51'order='ASC'template='1']正如您所看到的,我已经添加了template='1',因此您可以在以后添加一个新的布局,如果您愿意的话 好了,那我有什么问题: 当我想添加add-to-CART按钮(以及我的产品加载项)时,我需要如下内容: do_action('woocomm

我创建了一个短代码来显示特定产品类别的产品(结合了几个ACF字段)。所有这些都很好,所以为了您自己的利益,请使用下面的代码

短代码如下所示:
[yl\u get\u products cat\u id='51'order='ASC'template='1']
正如您所看到的,我已经添加了template='1',因此您可以在以后添加一个新的布局,如果您愿意的话

好了,那我有什么问题: 当我想添加add-to-CART按钮(以及我的产品加载项)时,我需要如下内容:

do_action('woocommerce'.$product->get_type().'u add_to_cart')在我的短代码中

do_action('woocommerce_single_product_summary')(然后删除所有不需要的操作)

结果是,它确实显示了整个添加到购物车的输出以及附加选项等,但它总是显示在页面的顶部(而不是模式),并且任何页面样式都被完全忽略

我相信我以前有过这个,我认为它必须与
echo return=return.=
有关,但我无法正确使用它

所以我的实际问题是:
如何在我的产品循环快捷码中向上述操作添加一个快捷码而不破坏布局?

这是我的密码:

// Get a dish category via shortcode
function yl_get_products_shortcode( $atts ) {

    $atts = shortcode_atts( array(
        'cat_id'    => '',
        'orderby'   => '',
        'order'     => '',
        'template'  => '',
    ), $atts );

    $products = new WP_Query(array(
        'post_type'         => 'product',
        'tax_query'         => array( array(
            'taxonomy'          => 'product_cat',
            'field'             => 'term_id',
            'terms'             => $atts['cat_id'],
            // 'include_children' => false // or true (optional)
        )),
        'orderby'           => $atts['orderby'],
        'order'             => $atts['order'],
        'post_status'       => 'publish',
        'posts_per_page'    => -1
    ));

    if ( $products->have_posts() ) {

        while ( $products->have_posts() ) {

            $products->the_post();

            if ($atts['template'] == '1') {

                global $product;
                $id                 = get_the_ID();
                $product            = wc_get_product( $id );

                $name               = $product->get_title();
                $short_description  = $product->get_short_description();
                $description        = $product->get_description();
                $rating_count       = $product->get_rating_count();
                $rating_average     = number_format($product->get_average_rating() * 20, 0) . '%';
                $review_count       = $product->get_review_count();
                $price              = $product->get_price();
                $image_url          = wp_get_attachment_url( $product->get_image_id() );

                $description_nl     = get_field('description_nl');
                $title_en           = get_field('title_en');
                $description_en     = get_field('description_en');
                $allergens          = get_field('allergens');
                $specifics          = get_field('specifics');

                $output .= '
                <li>
                    <div class="uk-grid-small" uk-grid>';
                        if ($product->is_in_stock()) {
                            $output .= '
                            <div class="uk-width-auto">
                                <a href="#product-' . $id . '" class="uk-button uk-button-small uk-button-primary" uk-toggle>
                                        <span uk-icon="icon: cart"></span>
                                </a>
                            </div>';
                        }
                        if (!empty($allergens)) {
                            $output .= '
                            <div class="uk-width-auto">
                                <a href="#allergens-' . $id . '" uk-toggle>
                                    <span uk-icon="icon: info"></span>
                                </a>
                            </div>';
                        }
                        $output .= '
                        <div class="uk-width-expand">
                            <p class="uk-h6 uk-margin-remove">
                                ' . $name;
                                if (!empty($specifics)) {
                                    foreach ($specifics as $specific) {
                                        $output .= '
                                        <img src="/wp-content/uploads/network/images/food-icons/icon-' . str_replace(' ', '-', $specific) . '.svg" width="16" uk-tooltip="' .  $specific . '">';
                                    }
                                }
                            $output .= '
                            </p>
                            <p class="uk-margin-remove">
                                ' . $description_nl;
                                if ($short_description) {
                                    $output .= '<br />' . $short_description;
                                }
                            $output .= '
                            </p>
                        </div>
                        <div class="uk-width-auto uk-h6">
                            € ' . $price . '
                        </div>
                    </div>';

                    if (!empty($allergens) || !empty($specifics)) {
                        $output .= '
                        <div id="allergens-' . $id . '" uk-modal="center: true">
                            <div class="uk-modal-dialog uk-modal-body">
                                <button class="uk-modal-close-default" type="button" uk-close></button>
                                <h3 class="uk-modal-title uk-margin-small uk-text-truncate">
                                    ' . $name;
                                    if (!empty($specifics)) {
                                        foreach ($specifics as $specific) {
                                            $output .= '
                                            <img src="/wp-content/uploads/network/images/food-icons/icon-' . str_replace(' ', '-', $specific) . '.svg" width="16" uk-tooltip="' . $specific . '">';
                                        }
                                    }
                                $output .= '
                                </h3>
                                <p class="uk-margin-small">' . __( 'Below you will find an overview of the allergens that are in this dish. For more information you can always contact our service staff.', 'yorlinq' ) . '</p>
                                <div uk-alert>
                                    <div class="uk-grid-small uk-child-width-1-2@s" uk-grid>';
                                        foreach ($allergens as $allergen) {
                                            $output .= '
                                            <div>
                                                <div class="uk-grid-small uk-flex-middle" uk-grid>
                                                    <div class="uk-width-1-4">
                                                        <img src="/wp-content/uploads/network/images/food-icons/icon-' . str_replace(' ', '-', $allergen) . '.svg">
                                                    </div>
                                                    <div class="uk-width-3-4">
                                                        ' . $allergen . '
                                                    </div>
                                                </div>
                                            </div>';
                                        }
                                    $output .= '
                                    </div>
                                </div>
                            </div>
                        </div>';
                    }

                    if ($product->is_in_stock()) {
                        $output .= '
                        <div id="product-' . $id . '" uk-modal="center: true">
                            <div class="uk-modal-dialog uk-modal-body">
                                <button class="uk-modal-close-default" type="button" uk-close></button>
                                <h3 class="uk-modal-title uk-margin-small uk-text-truncate">
                                    ' . $name;
                                    if (!empty($specifics)) {
                                        foreach ($specifics as $specific) {
                                            $output .= '
                                            <img src="/wp-content/uploads/network/images/food-icons/icon-' . str_replace(' ', '-', $specific) . '.svg" width="16" uk-tooltip="' . $specific . '">';
                                        }
                                    }
                                $output .= '
                                </h3>
                                ' . $description;
                        
                                // Here is where the action sould appear
                        
                            $output .= '
                            </div>
                        </div>';
                    }
                $output .= '
                </li>';

            }

        };

        return '<ul class="uk-list uk-list-divider">' . $output . '</ul>';

    }

}

add_shortcode('yl_get_products','yl_get_products_shortcode');
//通过快捷码获取菜肴类别
函数yl_get_products_shortcode($atts){
$atts=短码_atts(数组)(
“cat_id”=>”,
'orderby'=>'',
“订单”=>“”,
'模板'=>'',
)(港币),;
$products=新的WP\U查询(数组(
“post_类型”=>“产品”,
“tax_query”=>数组(数组(
“分类法”=>“产品分类”,
'field'=>'term_id',
“术语”=>$atts['cat_id'],
//“包含子项”=>false//或true(可选)
)),
'orderby'=>$atts['orderby'],
“订单”=>$atts[“订单”],
“发布状态”=>“发布”,
“每页帖子”=>-1
));
如果($products->have_posts()){
而($products->have_posts()){
$products->the_post();
如果($atts['template']=='1'){
全球$产品;
$id=get_the_id();
$product=wc\U get\U product($id);
$name=$product->get_title();
$short_description=$product->get_short_description();
$description=$product->get_description();
$rating_count=$product->get_rating_count();
$rating_average=number_格式($product->get_average_rating()*20,0)。“%”;
$review\u count=$product->get\u review\u count();
$price=$product->get_price();
$image\u url=wp\u get\u attachment\u url($product->get\u image\u id());
$description\u nl=获取字段(“description\u nl”);
$title_en=获取字段(“title_en”);
$description_en=get_字段('description_en');
$过敏原=获取字段(“过敏原”);
$specifics=get_字段(“specifics”);
$output.='
  • '; 如果($product->is\ U in\ U stock()){ $output.=' '; } 如果(!空($过敏原)){ $output.=' '; } $output.='

    “.$name; 如果(!空($specifics)){ foreach($specific为$specific){ $output.=' '; } } $output.='

    “.$description\u nl; 如果($short_description){ $output.='
    '.$short\u description; } $output.='

    欧元。$价格 '; 如果(!空($过敏原)| |!空($specifics)){ $output.=' “.$name; 如果(!空($specifics)){ foreach($specific为$specific){ $output.=' '; } } $output.='

    。uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu

    '; foreach($过敏原为$过敏原){ $output.='
    function yl_get_products_shortcode( $atts ) {
    //  ... your logic
    
        add_action( 'woocommerce_' . $product->get_type() . '_add_to_cart', function () {
    //      You could remove any action you want
    //      remove_action( 'xxx', 'xxx', priority );
        }, 1 );
    
        ob_start();
        do_action( 'woocommerce_' . $product->get_type() . '_add_to_cart' );
        $add_to_cart_content = ob_get_clean();
    
        return '<ul class="uk-list uk-list-divider">' . $output . '</ul>' . $add_to_cart_content;
    }