【问题标题】:Multiplying Woocommerce product price and displaying the result with short code乘以 Woocommerce 产品价格并用短代码显示结果
【发布时间】:2022-01-20 19:26:45
【问题描述】:

我正在尝试创建一些简码,以便显示我的产品价格乘以 0.2 后的价格,但我对 php 了解不多

简码脚本似乎基本上是

function wpc_elementor_shortcode( $atts ) {
}
add_shortcode( 'my_elementor_php_output', 'wpc_elementor_shortcode');

我要添加的实际内容在括号之间

内容似乎在这段代码的轮盘中

$product_id = 99;
$product = wc_get_product( $product_id );
echo $product->get_price_html();

我被告知只会显示产品的价格。我想知道的是如何获取任何数字“get_price_htnml();产生并将其乘以 .2 并让我的简码脚本显示该结果。

这不是正确的代码,但我会想到一些影响

$sum = get_price_html() * .2
echo $sum

如果 php 更简单,我会怎么做。

我也不确定是否将产品 ID 设置为 99,我得到代码的帖子说我需要创建一个产品对象只是为了使用 get_price_html 代码,但短代码需要响应单它被添加到的产品,它不能一直调用它所在的页面上的产品 id 99 的价格

对不起,这是一团糟,我主要编写 css 代码,但需要添加一点 php 以使我的生活更轻松

【问题讨论】:

    标签: php wordpress woocommerce shortcode custom-wordpress-pages


    【解决方案1】:

    您必须使用 2 个功能,第一个更新产品价格,第二个创建产品简码。

    1) 像这样更新产品价格:

       /**
        * @description    Alter Product Pricing For WooCommerce Product
        * @compatible    WooCommerce 4.1
        */
    
        add_filter( 'woocommerce_get_price_html', 'woo_alter_price_display', 9999, 2 );
    
       function woo_alter_price_display( $price_html, $product ) {
        
         // ONLY ON FRONTEND
         if ( is_admin() ) return $price_html;
    
         // ONLY IF PRICE NOT NULL
         if ( '' === $product->get_price() ) return $price_html;
    
         // IF CUSTOMER LOGGED IN
         // if ( wc_current_user_has_role( 'customer' ) ) {
             $price = $product->get_price();
             $updated_price = $price * .02;
             $orig_price = wc_get_price_to_display( $product );
             $price_html = wc_price($updated_price);
         // }
        
          return $price_html;
    
       }
    

    参考网址:https://stackoverflow.com/a/69521331/16560548

    2。创建短产品简码。

        function woo_product_details($atts = array()){
        if(isset($atts['product_id']) && !empty($atts['product_id'])){
            $html = '';
            $product_id                = $atts['product_id'];
            $product                   = wc_get_product($product_id);
    
            $product_name              = $product->get_name();
            $product_slug              = $product->get_slug();
            //$product_sku               = $product->get_sku();
            //$product_description       = $product->get_description();
            $product_short_description = $product->get_short_description(); 
    
            $price = $product->get_price();
            $extra_price = .2;
            $updated_price = $price * $extra_price;
            $price_html = wc_price($updated_price);
            //$product_price             = $product->get_price();
            $product_formated_price    = $product->get_price_html();
            //$product_regular_price     = $product->get_regular_price();
            //$product_sale_price        = $product->get_sale_price();  
            $product_image= wp_get_attachment_image_src( get_post_thumbnail_id($product_id), 'single-post-thumbnail' );
            
            $html .= '<div class="card">
                <img src="'.$product_image[0].'" alt="'.$product_name.'" data-id="'.$product_id.'" style="width:100%">
                <h1>'.$product_name.'</h1>
                <p class="price">'.$product_formated_price.'</p>
                <p>'.$product_short_description.'</p>
                <p><a href="'.site_url().'?add-to-cart='.$product_id.'&quantity=1">Add to Cart</a></p>
            </div>';
            return $html;
        }
    }
    add_shortcode('product_detail', 'woo_product_details');
    

    如何使用简码

    a) 您可以像这样直接在您的 PHP 页面上调用:

    <?php echo do_shortcode('[product_detail product_id = '1002']'); ?>
    

    b) 您可以像这样从管理面板调用您的帖子和页面:

    [product_detail product_id = '1002']
    

    其中 1002 是产品 ID

    参考网址:https://stackoverflow.com/a/69008400/16560548

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-30
      • 2017-11-24
      • 2021-05-31
      • 1970-01-01
      • 1970-01-01
      • 2018-02-08
      相关资源
      最近更新 更多