Php 如何在循环外使用变体和添加到购物车按钮

Php 如何在循环外使用变体和添加到购物车按钮,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,我在我的网站上使用woocommerce,但由于我只有一个主产品和两个配件(相关产品),我不需要一个经典的商店页面,也不需要为我的每个产品提供单一的产品页面。我的主要产品有不同的颜色 我想有一个添加到购物车按钮,与颜色变化下拉列表和数量字段在我的一个定期张贴页面。与单个产品页面完全相同,但嵌入到我自己的页面中,没有单个产品页面中我不需要的部分(描述…) 我最终决定使用我创建的两个自定义快捷码来实现这一点:[my_vc_product\u price id=“xxx”]和[my_vc_add2ca

我在我的网站上使用woocommerce,但由于我只有一个主产品和两个配件(相关产品),我不需要一个经典的商店页面,也不需要为我的每个产品提供单一的产品页面。我的主要产品有不同的颜色

我想有一个添加到购物车按钮,与颜色变化下拉列表和数量字段在我的一个定期张贴页面。与单个产品页面完全相同,但嵌入到我自己的页面中,没有单个产品页面中我不需要的部分(描述…)

我最终决定使用我创建的两个自定义快捷码来实现这一点:
[my_vc_product\u price id=“xxx”]
[my_vc_add2cart\u variable\u product id=“xxx”]
。所以我可以把它们放在我想要的地方

但我的问题是,下拉菜单+变体可用性+添加到购物车按钮的行为与此元素在单个产品页面中的行为不同: -当我在下拉菜单中选择颜色时,没有显示he变体的可用性; -在下拉菜单中未选择任何颜色时,“添加到购物车”按钮未禁用(只有选择颜色时,该按钮才应禁用并激活)

使用互联网上的一些代码显示价格很容易:

/**
*添加快捷码以允许在页面中显示产品价格
*/ 
函数my\u vc\u display\u product\u price($args){
$product_id=$args['id'];
$product=wc\U get\U product($product\U id);
echo'

。$product->get_price_html()。

; } 添加快捷码(“我的vc产品价格”、“我的vc显示产品价格”);
为了获得相同的图形结果,我只需在行中添加一些CSS类:“woocommerce”和“product”

显示下拉菜单+数量和添加到购物车按钮的代码与plugins/woocommerce/templates/single product/add to cart/中的variable.php文件几乎相同。唯一真正改变的是您需要获得产品的“变化属性”,而不是属性

$attributes=$product->get_variation_attributes();
$attribute_keys=数组_keys($attributes);
因此,完整的功能代码是:

/**
*添加快捷码以允许显示带有变体属性下拉菜单的“添加到购物车”按钮
*/ 
函数my\u vc\u add\u to\u cart\u button\u variable\u product($args){
全球$产品;
$product_id=$args['id'];
$product=wc\U get\U product($product\U id);
如果($product->is_类型('variable')){
$attributes=$product->get_variation_attributes();
$attribute_keys=数组_keys($attributes);
$available\u variations=array($product->get\u available\u variations());
do_action('woocommerce_之前添加_到_cart_表单');?>

尝试使用以下代码

function add_to_cart_form_shortcode( $atts ) {
        if ( empty( $atts ) ) {
            return '';
        }

        if ( ! isset( $atts['id'] ) && ! isset( $atts['sku'] ) ) {
            return '';
        }

        $args = array(
            'posts_per_page'      => 1,
            'post_type'           => 'product',
            'post_status'         => 'publish',
            'ignore_sticky_posts' => 1,
            'no_found_rows'       => 1,
        );

        if ( isset( $atts['sku'] ) ) {
            $args['meta_query'][] = array(
                'key'     => '_sku',
                'value'   => sanitize_text_field( $atts['sku'] ),
                'compare' => '=',
            );

            $args['post_type'] = array( 'product', 'product_variation' );
        }

        if ( isset( $atts['id'] ) ) {
            $args['p'] = absint( $atts['id'] );
        }

        $single_product = new WP_Query( $args );

        $preselected_id = '0';


        if ( isset( $atts['sku'] ) && $single_product->have_posts() && 'product_variation' === $single_product->post->post_type ) {

            $variation = new WC_Product_Variation( $single_product->post->ID );
            $attributes = $variation->get_attributes();


            $preselected_id = $single_product->post->ID;


            $args = array(
                'posts_per_page'      => 1,
                'post_type'           => 'product',
                'post_status'         => 'publish',
                'ignore_sticky_posts' => 1,
                'no_found_rows'       => 1,
                'p'                   => $single_product->post->post_parent,
            );

            $single_product = new WP_Query( $args );
        ?>
            <script type="text/javascript">
                jQuery( document ).ready( function( $ ) {
                    var $variations_form = $( '[data-product-page-preselected-id="<?php echo esc_attr( $preselected_id ); ?>"]' ).find( 'form.variations_form' );
                    <?php foreach ( $attributes as $attr => $value ) { ?>
                        $variations_form.find( 'select[name="<?php echo esc_attr( $attr ); ?>"]' ).val( '<?php echo esc_js( $value ); ?>' );
                    <?php } ?>
                });
            </script>
        <?php
        }

        $single_product->is_single = true;
        ob_start();
        global $wp_query;

        $previous_wp_query = $wp_query;

        $wp_query          = $single_product;

        wp_enqueue_script( 'wc-single-product' );
        while ( $single_product->have_posts() ) {
            $single_product->the_post()
            ?>
            <div class="single-product" data-product-page-preselected-id="<?php echo esc_attr( $preselected_id ); ?>">
                <?php woocommerce_template_single_add_to_cart(); ?>
            </div>
            <?php
        }

        $wp_query = $previous_wp_query;

        wp_reset_postdata();
        return '<div class="woocommerce">' . ob_get_clean() . '</div>';
}
add_shortcode( 'add_to_cart_form', 'add_to_cart_form_shortcode' );

/*Example Usage [add_to_cart_form id=147]*/
函数添加到购物车表单快捷码($atts){
如果(空($atts)){
返回“”;
}
如果(!isset($atts['id'])和(&!isset($atts['sku'])){
返回“”;
}
$args=数组(
“每页帖子数”=>1,
“post_类型”=>“产品”,
“发布状态”=>“发布”,
“忽略粘性帖子”=>1,
“未找到行”=>1,
);
如果(isset($atts['sku'])){
$args['meta_query'][]=数组(
“密钥”=>“\u sku”,
“value'=>清理文本字段($atts['sku']),
“比较”=>“=”,
);
$args['post_type']=数组('product','product_variation');
}
如果(isset($atts['id'])){
$args['p']=absint($atts['id']);
}
$single_product=新的WP_查询($args);
$preselected_id='0';
如果(isset($atts['sku'])和&$single_product->have_posts()&&&&'product_variation'==$single_product->post->post_type){
$variation=新WC_产品_variation($single_Product->post->ID);
$attributes=$variation->get_attributes();
$preselected\u id=$single\u product->post->id;
$args=数组(
“每页帖子数”=>1,
“post_类型”=>“产品”,
“发布状态”=>“发布”,
“忽略粘性帖子”=>1,
“未找到行”=>1,
'p'=>$single\u product->post->post\u parent,
);
$single_product=新的WP_查询($args);
?>
jQuery(文档).ready(函数($){
变量$variations_form=$(“[数据产品页面预选id=”“]”)。查找('form.variations_form');
$variations_form.find('select[name=”“]).val('');
});

太好了!非常感谢。
现在最难的是:我需要理解你的代码并将其与我的代码进行比较;-)

非常感谢!我们正在为agesCan寻找一个类似的解决方案。请发布一个屏幕截图,说明这是什么样子的?此外,我是否需要将其转换为插件,或者我可以将其作为php添加到页面中(作为“SNIPPY”php代码片段)你可以在php代码段中使用它,因为这是完美的。实际上,我使用的插件改变了单选按钮的selectfields,不知怎么的,它也改变了,所以我很高兴:DHello,我不明白这段代码的结果是什么。我在这页上寻找类似的东西。你认为有可能调整你的代码,使其具有类似的内容吗?这很简单它从您那里获取id并生成html