Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.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 如果产品在WooCommerce中缺货,则向链接添加自定义类_Php_Wordpress_Woocommerce_Shortcode_Product - Fatal编程技术网

Php 如果产品在WooCommerce中缺货,则向链接添加自定义类

Php 如果产品在WooCommerce中缺货,则向链接添加自定义类,php,wordpress,woocommerce,shortcode,product,Php,Wordpress,Woocommerce,Shortcode,Product,在WooCommerce中,您知道如何将类添加到 我正在创建一个SVG映射。每个部件/路径将链接到特定的产品。如果链接到的产品缺货,则应添加“oos”类(以便该零件不再可见) 我不反对使用某种短代码,如果这会有所帮助的话。但是短代码需要能够包装每个独特的SVG路径 库存: <svg> <a href="https://example.com/?p=332"> <path></path> </a> <

在WooCommerce中,您知道如何将类添加到

我正在创建一个SVG映射。每个部件/路径将链接到特定的产品。如果链接到的产品缺货,则应添加“oos”类(以便该零件不再可见)

我不反对使用某种短代码,如果这会有所帮助的话。但是短代码需要能够包装每个独特的SVG路径

库存:

<svg>
    <a href="https://example.com/?p=332">
        <path></path>
    </a>
</svg>

缺货:

<svg>
    <a href="https://example.com/?p=332" class="oos">
        <path></path>
    </a>
</svg>


注意,在URL中,如果有助于PHP找到相应的项,我也可以使用产品的ID(而不是使用通常的slug URL)。

最简单的方法应该是为html
构建自定义函数快捷码;
}
添加_短码('svg_产品_链接','custom_短码_svg_产品_链接');
}
代码位于活动子主题(或主题)的function.php文件或任何插件文件中

在WooCommerce 3+上测试并输出所需的html


用法示例(针对您的情况):

注意:我绝对不熟悉SVG文件和html格式,所以我希望它能与
一起使用

/*
*@id(产品id参数)
*/
[svg_product_link id=“332”]
[/svg\u产品\u链接]

由于某种原因,当添加该函数时,它现在给我一个HTTP错误500。似乎是由于
返回=
。删除
=
似乎可以让它正常工作。在我将其标记为正确之前,您能否告诉我
prod\u类别
的作用,以及您为什么选择它?什么是
prod\u类别
?@Garconis也已更正/更新…您应该将其替换为
svg\u产品链接
…抱歉,感谢将那些“添加到购物车”链接变成ajax的提示?所以当点击时,它只是添加它,没有任何重定向?
if( !function_exists('custom_shortcode_svg_product_link') ) {

    function custom_shortcode_svg_product_link( $atts, $content = null ) {

        // Shortcode Attributes
        $atts = shortcode_atts(
            array(
                'id' => '',
            ),
            $atts, 'svg_product_link'
        );

        $product_id = $atts['id'];
        // Get an instance of the WC_Product object
        $product = wc_get_product( $product_id );
        // Display the class "oos" when product is out of stock
        $class = $product->is_in_stock() ? '"' : '" class="oos"';
        // The product link
        $product_link = $product->get_permalink();

        // The output
        return '<a href="'.$product_link.$class.'>'.$content.'</a>';
    }
    add_shortcode( 'svg_product_link', 'custom_shortcode_svg_product_link' );
}
/*
 * @ id (the product ID argument)
 */
<svg>
    [svg_product_link id="332"]
        <path></path>
    [/svg_product_link]
    </a>
</svg>