我尝试开发一个 woocommerce 插件。我需要更改产品价格和产品供应情况
我正在使用 woocommerce_before_single_product 挂钩,但我不明白该挂钩如何将 id 产品传递给我的函数。
这是我的代码
add_action('woocommerce_before_single_product', 'call_ws_for_as400', 10 , 1);
function call_ws_for_as400() {
$product = wc_get_product( $context['post'] -> ID );
$id = $product->id;
error_log($product);
error_log($id);
}
Run Code Online (Sandbox Code Playgroud)
产品和id始终为空,为什么?
我有一个网站运行旧版本的 Woo v2.5.5,使用旧版 v3 作为 API。我能够使用 woocommerce_api_order_response 操作将数据添加到订单中。
就像是:
add_action( 'woocommerce_api_order_response', 'add_testing_api_function', 10, 1 );
function add_testing_api_function( $order_data ) {
$order_data['foo'] = "testing";
return $order_data;
}
Run Code Online (Sandbox Code Playgroud)
这在旧的 API 链接上工作得很好:
https://example.com/wc-api/v3/orders?consumer_key=KEY&consumer_secret=SECRET
但是,我需要更新到 Woo v3.3+,并且 REST API 的服务器启动方式为:
https://example.com/wp-json/wc/v2/orders?consumer_key=KEY&consumer_secret=SECRET
我的自定义数据不再出现,并且该挂钩似乎不起作用。还有其他我可以使用的吗?
我试图为functions.php 编写一个代码片段,当同时选择角色“订阅者”和付款方式“信用卡”时,该代码片段会对购物车总额应用2% 的折扣。到目前为止我的进展
function discount_when_role_and_payment(/* magic */) {
global $woocommerce;
if ( /* credit-card selected */ && current_user_can('subscriber') ) {
/* get woocommerce cart totals, apply 2% discount and return*/
}
return /*cart totals after discount*/;
}
add_filter( '/* magic */', 'discount_when_role_and_payment' );
Run Code Online (Sandbox Code Playgroud)
有人可以帮忙完成这个吗?或者至少指向正确的方向?
我想在用户创建并保存产品数据后通过肥皂客户端将 woocommerce 产品数据发送到邮政公司的网络服务。所以我需要一个在用户创建产品后触发的钩子。我搜索了很多并找到了一些 wordpress 和 woocommerce 钩子,但它们都没有完成我的工作。发送日期后,Web 服务返回一个应设置为产品 SKU 的 ID。听到的是我的函数代码:(参数来自 save_post_product 挂钩)
function productPublished ($ID , $post , $update){
$product = wc_get_product( $post->ID);
$user = get_option ('myUsername');
$pass = get_option ('myPassword');
$name = $product->get_name();
$price = $product->get_regular_price();
$weight = $product->get_weight() * 1000;
$count = $product->get_stock_quantity();
$description = $product->get_short_description();
$result = $client -> AddStuff ([
'username' => $user,
'password' => $pass,
'name' => $name,
'price' => $price,
'weight' => $weight,
'count' => $count,
'description' => $description,
'percentDiscount' => 0
]); …Run Code Online (Sandbox Code Playgroud) 我正在为一家小型在线商店使用 Woocommerce,并使用 Greatives 的 Movedo Wordpress 主题。
我试图将产品价格移至单个产品页面中简短描述的下方。我将代码插入到functions.php我的子主题文件中:
remove_action('woocommerce_single_product_summary',
'woocommerce_template_single_price', 4 );
add_action('woocommerce_single_product_summary',
'woocommerce_template_single_price', 29 );
Run Code Online (Sandbox Code Playgroud)
但它最终会显示价格两次:单一产品页面。
我做错了什么?任何帮助表示赞赏。
当在此 WooCommerce 商店中订购特定产品时,两个元值将添加到订单中。
存储元值的两个字段位于wp_woocommerce_order_itemmeta
元键是:
quantity
assemblycost
Run Code Online (Sandbox Code Playgroud)
我想在下新订单时以编程方式创建一个新的自定义字段,并将该新字段的值设置为等于quanity * assemblycost已订购产品的元键组装成本。
经过一些研究,我发现这woocommerce_checkout_update_order_meta是一个在订单保存到数据库并且元数据已更新后执行的钩子。所以这似乎是我应该使用的钩子。
function add_item_meta( $order_id ) {
//global $woocommerce;
update_post_meta( $order_id, '_has_event', 'yes' );
}
Run Code Online (Sandbox Code Playgroud)
我尝试在functions.php中添加以下代码:
add_action('woocommerce_checkout_update_order_meta',function( $order_id, $posted ) {
$assemblycost = wc_get_order_item_meta($order_id, 'assemblycost');
$quantity = wc_get_order_item_meta($order_id, 'quantity');
$calculatedValue = $quantity * $assemblycost;
wc_update_order_item_meta( $order_id, 'calculated_field', $calculatedValue );
} , 10, 2);
Run Code Online (Sandbox Code Playgroud)
这确实创建了新的元字段,但它将该值设置为 0。
如何更改上面的代码,以便计算字段的值是 的乘积quantity * assemblycost?
当产品具有特定的运输类别时,我尝试在结帐/购物车页面上的产品标题下方显示特定文本。我怎么做?
我很难在产品循环内添加文本。
我有这个函数,当页面加载时它会被触发。它检查帖子类型是否为“产品”,在这种情况下我需要更改产品价格并保存页面。
\n\n我工作得很好,但我需要知道如何设置新产品的价格。
\n\nfunction changeAndSave( $array ) { \n\n $post_type = get_post_type();\n if ($post_type == "product"){\n\n /*GET THE PRODUCT*/\n global $product;\n $product_id=$product->id;\n\n /*GET CURRENT PRICE*/\n $currentPrice=$product->get_price_html();\n\n /*SET NEW PRICE*/\n /* HERE.... \xc2\xbf\xc2\xbf how do I set my new price */\n $newPrice = 100\xe2\x82\xac;\n\n /*SAVE THE PRODUCT*/ \n $product = wc_get_product($product_id);\n $product->save();\n }\n}; \n\n// add the action \nadd_action( \'loop_start\', \'changeAndSave\', 10, 1 ); \nRun Code Online (Sandbox Code Playgroud)\n\n更新:我尝试过这个,但不起作用:
\n\nfunction changeAndSave( $array ) { \n\n $post_type = get_post_type();\n if ($post_type == "product"){\n\n …Run Code Online (Sandbox Code Playgroud) 我\xc2\xb4m正在研究一种解决方案,以根据客户所在国家/地区\xc2\xa0以及他是否可以提供增值税ID来更改税级。
\n\n该商店位于欧盟,为 B2B 和 B2C 提供服务。\n因此,对于所有可以提供增值税 ID 的欧盟公司,除非来源国家/地区相同,否则不会收取税费。\n对于所有无法提供增值税 ID 的客户,我们将添加当地税费。\n依此类推...
\n\n我找到的所有解决方案仅与购物车交互,但当我以编程方式创建订单时,我需要另一个解决方案。
\n\n有没有办法(可能是一个钩子)在创建订单时更改tax_class ?
\n\n为了进行测试,我尝试添加此方法,但没有成功。它会触发过滤器,但不会更改订单中的税级。
\n\nfunction wc_change_tax_class( $tax_class, $product ) {\n\n $tax_class = 'Zero rate';\n\n return $tax_class;\n\n}\nadd_filter( 'woocommerce_product_get_tax_class', 'wc_change_tax_class', 1, 2 );\nadd_filter( 'woocommerce_product_variation_get_tax_class', 'wc_change_tax_class', 1, 2 );\nRun Code Online (Sandbox Code Playgroud)\n\n我还在创建订单的函数中尝试了这种方法,但结果相同。什么也没发生。
\n\n$woocommerce->customer->set_is_vat_exempt( true );\nRun Code Online (Sandbox Code Playgroud)\n\n谢谢你的帮助!
\n我确信这个问题已经被问过很多次了。我什至得到了很少的答案,但我真的不知道如何在 Wordpress + WooCommerce 中编辑任何“操作”或“过滤器”挂钩。
以结账页面为例,底部通常是购物车的摘要,该信息是由以下代码生成的:
<?php do_action( 'woocommerce_thankyou', $order->get_id() ); ?>
Run Code Online (Sandbox Code Playgroud)
我尝试了不同的方式在其他文件中搜索“woocommerce_thankyou”,但我找不到任何内容。那么,输出代码是如何生成的呢?必须有一个文件来调用它。
hook-woocommerce ×10
woocommerce ×10
wordpress ×9
php ×8
orders ×2
product ×2
cart ×1
checkout ×1